Вот мой файл конфигурации hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/bdd_disc?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT</property>
<property name="connection.username">root</property>
<property name="connection.password">MP18711922</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<mapping class="org.o7planning.tutorial.hibernate.entities.Artists" />
<mapping class="org.o7planning.tutorial.hibernate.entities.Disc" />
<mapping class="org.o7planning.tutorial.hibernate.entities.Song" />
</session-factory>
</hibernate-configuration>
И класс для получения SessionFactory:
public class HibernateUtils {
private static final SessionFactory sessionFactory = buildSessionFactory();
// Hibernate 5:
private static SessionFactory buildSessionFactory() {
try {
// Create the ServiceRegistry from hibernate.cfg.xml
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()//
.configure("hibernate.cfg.xml").build();
// Create a metadata sources using the specified service registry.
Metadata metadata = new MetadataSources(serviceRegistry).getMetadataBuilder().build();
return metadata.getSessionFactoryBuilder().build();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
Я получаю сообщение об ошибке:
Причина: org.hibernate.internal.util.config.ConfigurationException: Невозможно выполнить unmarshalling в строке номер 0 и столбце 0 в RESOURCE hibernate.cfg.xml.Сообщение: null
Класс, выполняющий основную работу:
private static void getTagsFromFile (File f) {
String artist = null;
String disc = null;
String song = null;
Tag tag = null;
AudioFile af;
Artists artistInBase = null;
Disc discInBase = null;
Song songInBase = null;
SessionFactory factory = HibernateUtils.getSessionFactory();
Session session = factory.getCurrentSession();
try {
// All the action with DB via Hibernate
// must be located in one transaction.
// Start Transaction.
session.getTransaction().begin();
af = AudioFileIO.read(f);
tag = af.getTag();
artist = tag.getFirst(FieldKey.ARTIST);
artist = artist.replaceAll("'", " ");
artist = artist.replaceAll("\"", " ");
disc = tag.getFirst(FieldKey.ALBUM);
disc = disc.replaceAll("'", " ");
disc = disc.replaceAll("\"", "'");
song = tag.getFirst(FieldKey.TITLE);
song = song.replaceAll("\"", "'");
int idArtist = -1;
int idDisc = -1;
Transaction tx = null;
try {
tx = session.beginTransaction();
String queryAsString = "SELECT artists.name FROM artists WHERE artists.name LIKE " + "'" + artist + "'";
Query<Artists> query = session.createQuery(queryAsString);
List<Artists> artists = query.getResultList();
if (artists.isEmpty()) {
artistInBase = new Artists(artist);
session.save(artistInBase);
session.flush();
idArtist = artistInBase.getId();
}
else {
artistInBase = artists.get(0);
idArtist = artistInBase.getId();
}
tx.commit();
}
catch (Exception e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
} catch (CannotReadException | IOException | TagException | ReadOnlyFileException
| InvalidAudioFrameException e) {
System.err.println("Cannot parse " + f.getName());
}
}