线程" main"中的例外情况org.hibernate.UnknownEntityTypeException:无法找到持久性

时间:2015-12-07 07:12:31

标签: java hibernate annotations hibernate-mapping

我不得不问,我几乎尝试了一切。

实体类

@Entity
@Table(name="UserInfo")
public class User {

    @Id@Column(name="user_name")
    private String userName;

    @Column(name="user_id")
    private Integer userId;
}
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=BankingApplication</property>
        <property name="hibernate.connection.username">sa</property>
        <property name="hibernate.connection.password">2OmniWay</property>

        <!-- org.hibernate.HibernateException: No CurrentSessionContext configured! -->
        <property name="hibernate.current_session_context_class">thread</property>
         <property name="hibernate.default_schema">dbo</property>
        <!-- Mapping with model class containing annotations -->
        <mapping class="pojo.User"/>
    </session-factory>  
</hibernate-configuration>

SeesionFactory

// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure("hibernate-annotation.cfg.xml");
System.out.println("Hibernate Annotation Configuration loaded");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().                                    applySettings(configuration.getProperties()).build();
 System.out.println("Hibernate Annotation serviceRegistry created");             
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

return sessionFactory;

将对象保存在DB

SessionFactory sessionFactory = HibernateUtil.getSessionAnnotationFactory();
        Session session = sessionFactory.openSession();
        Account account = null;
        try{
        account = (Account) session.get(Account.class, id);

我在主题中提到异常。我只是三倍检查了一切,但它不起作用。enter image description here

任何建议都会有所帮助。

___________ Stack Trace _________

 Exception in thread "main" org.hibernate.UnknownEntityTypeException: Unable to locate persister: pojo.User
at org.hibernate.internal.SessionFactoryImpl.locateEntityPersister(SessionFactoryImpl.java:792)
at org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2652)
at org.hibernate.internal.SessionImpl.access$2500(SessionImpl.java:164)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2590)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2577)
at org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1044)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:955)
at business.ManageAccount.getAccountDetails(ManageAccount.java:18)
at Utils.TestConnecttion.main(TestConnecttion.java:16)

6 个答案:

答案 0 :(得分:1)

您需要在配置中专门添加实体类。 默认情况下,它会加载类,但不会初始化实体类。 对于那个配置.addAnnotatedClass()或addResource将解决这个问题。

答案 1 :(得分:1)

替换你的代码

“ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()。applySettings(configuration.getProperties())。build();”

从SessionFactory到

“ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()。applySettings(             configuration.getProperties())。 buildServiceRegistry();“

并尝试在类

中使sessionfactory和serviceRegistry成为静态

可能适合你。

答案 2 :(得分:0)

您的用户未实现Serializable,请尝试使用:

 public class User implements Serializable{
   }

答案 3 :(得分:0)

//I am using Hib5,I have created SF like this and it is working
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure().build();
            Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder().build();
            sessionFactory = metadata.getSessionFactoryBuilder().build();
        }
        return sessionFactory;
    }

答案 4 :(得分:-1)

您没有将Dialect添加到您的Hibernate配置文件中。在配置文件中添加此行。

<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>

答案 5 :(得分:-2)

您的语法在Hibernate 4.x中有效,但导致Hibernate 5.x出错。

配置文件导致此问题。 在配置XML文件中,您添加了一个映射文件,例如:

mapping resource="hibernate.map.xml"

删除它并以编程方式添加:

configure.addResource(hibernate.map.xml);

一定会有用。