下面是我的配置文件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引起:无法在RESOURCE hibernate.cfg.xml中的行号0和列0处执行解组。留言内容:null
做主要工作的类是:
public static void printStackTrace(){
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());
}
}
3条答案
按热度按时间46qrfjad1#
我在maven构建生命周期中遇到过类似的问题。我使用了maven插件
maven-processor-plugin
,如here所述。maven日志包含以下warning
:在我切换到maven 3.6.3和JDK 11之后,我删除了
maven-processor-plugin
并将hibernate-jpamodelgen
添加到依赖项部分。这解决了warning
。静态元模型类仍然会生成。3df52oht2#
好的,我明白了,我的pom.xml有不同版本的hibernate。现在它工作。
fnatzsnv3#
建议将
org.hibernate:hibernate-jpamodelgen
直接添加到<dependencies>
部分的问题是,您现在包含了一个在运行时实际上并不需要的运行时依赖项。您可以将<scope>
设置为provided
,但这在概念上仍然是错误的,您可能会在源代码中意外地使用该依赖项中的类。更好的解决方案是将依赖项作为注解处理器添加到maven-compiler-plugin中: