Java “线程中的异常”;“主要”;org.hibernate.UnknowmentityTypeException:找不到持久器
我不得不问,我几乎什么都试过了 实体类Java “线程中的异常”;“主要”;org.hibernate.UnknowmentityTypeException:找不到持久器,java,hibernate,annotations,hibernate-mapping,Java,Hibernate,Annotations,Hibernate Mapping,我不得不问,我几乎什么都试过了 实体类 获取保存在数据库中的对象 我在主题中提到了一个例外。我只是把所有的东西都检查了三倍,但就是不起作用 任何建议都会有帮助 ___________堆栈跟踪_________ Exception in thread "main" org.hibernate.UnknownEntityTypeException: Unable to locate persister: pojo.User at org.hibernate.internal.SessionF
获取保存在数据库中的对象
我在主题中提到了一个例外。我只是把所有的东西都检查了三倍,但就是不起作用 任何建议都会有帮助 ___________堆栈跟踪_________
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)
线程“main”org.hibernate.UnknownEntityTypeException中的异常:找不到persister:pojo.User
位于org.hibernate.internal.SessionFactoryImpl.locateEntityypersister(SessionFactoryImpl.java:792)
位于org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2652)
访问org.hibernate.internal.SessionImpl.access$2500(SessionImpl.java:164)
位于org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl。(SessionImpl.java:2590)
位于org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl。(SessionImpl.java:2577)
位于org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1044)
位于org.hibernate.internal.SessionImpl.get(SessionImpl.java:955)
位于business.ManageAccount.getAccountDetails(ManageAccount.java:18)
位于Utils.testconnection.main(testconnection.java:16)
您没有将方言添加到Hibernate配置文件中。请在配置文件中添加此行
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
org.hibernate.dialogue.sqlserverdialogue
您的用户未实现可序列化,请尝试以下操作:
public class User implements Serializable{
}
您需要在配置中专门添加实体类。 默认情况下,它加载类,但不初始化实体类。 对于该配置,addAnnotatedClass()或addResource将解决此问题。替换您的代码 “ServiceRegistry ServiceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();” 从SessionFactory到 “ServiceRegistry ServiceRegistry=new ServiceRegistryBuilder()。应用程序设置( configuration.getProperties()).buildServiceRegistry();“ 并尝试使sessionfactory和serviceRegistry在类中保持静态
希望它对您有用。您的语法在Hibernate 4.x中有效,但在Hibernate 5.x中会导致错误 配置文件导致此问题。 在配置XML文件中,您添加了一个映射文件,例如:
mapping resource="hibernate.map.xml"
删除此项,然后以编程方式添加此项:
configure.addResource(hibernate.map.xml);
它肯定会工作。stacktrace在哪里?它会自动加载。我可以在日志中看到它。其他地方出了问题。我也添加了,但没有成功。这是编译时异常。addAnnotatedClass是配置对象的方法,我添加了configuration.addAnnotatedClass(Account.class);configure(“hibernate annotation.cfg.xml”);它是有效的。但我想知道为什么它不从cfg.xml文件中选择映射?您只向cfg.xml文件添加了User.java,其他实体呢?我已经在cfg.xml中添加了所有实体。Hibernate没有选择任何一个,无论是用户还是帐户。我已经尽可能简短地展示了cfg.xml。我希望Hibernate从cfg.xml中选择映射,而不是从上面提供的解决方案中选择映射。有什么看法吗?@youshchauhan如果你找到了答案,你能和我们分享吗?非常感谢你使用addAnnotatedClass,它正在工作。但是,如果在创建SessionFactory时必须指定每个实体,那么这不是一个好的设计。我希望这是配置的一部分。想法??为此,您需要为每个实体创建.hbm,并使用hibernate.cfg.xml配置该hbm文件。我认为这是使其成为配置的一部分的唯一方法。您使用的是什么版本的hibernate
//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;
}
public class User implements Serializable{
}
mapping resource="hibernate.map.xml"
configure.addResource(hibernate.map.xml);
//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;
}