java 线程“main”org.hibernate.UnknownEntityTypeException 中的异常:无法定位持久器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34128224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception in thread "main" org.hibernate.UnknownEntityTypeException: Unable to locate persister
提问by vineeshchauhan
I have to ask it, I almost tried everything.
我不得不问它,我几乎什么都试过了。
Entity Class
实体类
@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;
Getting Object saved in DB
获取保存在数据库中的对象
SessionFactory sessionFactory = HibernateUtil.getSessionAnnotationFactory();
Session session = sessionFactory.openSession();
Account account = null;
try{
account = (Account) session.get(Account.class, id);
I am getting exception mentioned in subject. I just tripled checked everything but it is just not working.
Any suggestion would be helpful.
任何建议都会有所帮助。
___________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.access00(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)
回答by KayV
You need to specifically add the entity classes in the configuration. By default it loads the classes but do not initialize the entity classes. For that configuration.addAnnotatedClass() or addResource will solve this out.
您需要在配置中专门添加实体类。默认情况下,它加载类但不初始化实体类。对于那个 configuration.addAnnotatedClass() 或 addResource 将解决这个问题。
回答by Kandy
replace your code
替换你的代码
"ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();"
“ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();”
from SessionFactory to
从会话工厂到
"ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings( configuration.getProperties()). buildServiceRegistry();"
“ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();”
and try to make your sessionfactory and serviceRegistry static in class
并尝试使您的 sessionfactory 和 serviceRegistry 在课堂上保持静态
May it works for you.
愿它对你有用。
回答by Biswasini
//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;
}
回答by Abdelhak
your User not implemented Serializable, try with this:
你的用户没有实现可序列化,试试这个:
public class User implements Serializable{
}
回答by suman tipparapu
You did not add Dialect to your Hibernate config file.Add this line in your config file.
您没有将方言添加到您的 Hibernate 配置文件中。在您的配置文件中添加这一行。
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
回答by Hemanth kumar.D
Your syntax works in Hibernate 4.x, but causes an error with Hibernate 5.x.
您的语法适用于 Hibernate 4.x,但会导致 Hibernate 5.x 出错。
The configuration file causes this problem. In the configuration XML file, you added a mapping file, such as:
配置文件导致此问题。在配置 XML 文件中,您添加了一个映射文件,例如:
mapping resource="hibernate.map.xml"
Remove this and add this programatically instead:
删除它并以编程方式添加它:
configure.addResource(hibernate.map.xml);
It will surely work.
它肯定会起作用。