使用hibernate-core 5.0配置hibernate的问题

时间:2017-12-13 18:02:52

标签: hibernate jpa

更新至this。 如果我使用

,我无法使用hibernate(Configuration.buildSessionFactory)或base jpa(Persistence.createEntityManagerFactory)配置持久性
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.12.Final</version>
</dependency>

在hibernate的情况下,我有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/ifinances</property>
    <property name="connection.username">infinite</property>
    <property name="connection.password">skills</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>


    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <mapping class="com.infiniteskills.data.entities.User"/>
    <mapping class="com.infiniteskills.data.entities.Bank"/>

</session-factory>

</hibernate-configuration>

放在src/main/resources下。类以这种方式映射

<mapping class="com.infiniteskills.data.entities.User"/>
<mapping class="com.infiniteskills.data.entities.Bank"/>

如果我跑

Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
return configuration
    .buildSessionFactory(new StandardServiceRegistryBuilder()
    .applySettings(configuration.getProperties()).build()).openSession();

每当我尝试使用调用已配置类的会话方法时,它会给我 org.hibernate.UnknownEntityTypeException:无法找到persister:[class-name]

如果我使用版本4.3.6.Final,它的效果非常好。

如果我使用persistence.xml

下的src/main/resources/META-INF进行了jpa配置
 <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version="2.1">

<persistence-unit name="infinite-finances">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="javax.persistence.jdbc.user" value="infinite"/>
        <property name="javax.persistence.jdbc.password" value="skills"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/ifinances"/>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>

        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
        <property name="hibernate.show_sql" value="true"/>
    </properties>
</persistence-unit>

我用

配置它
EntityManager em = Persistence.createEntityManagerFactory("infinite-finances").createEntityManager();

我在线程&#34; main&#34;中有异常javax.persistence.PersistenceException:没有名为infinite-finances的EntityManager的持久性提供程序5.2.12.Final,并且它再次配置并与4.3.6.Final完美配合。

0 个答案:

没有答案