java中的Hibernate MappingException

时间:2013-12-11 10:31:57

标签: java hibernate

我很好奇这个错误

org.hibernate.MappingException: Unknown entity: xyz

我是hibernate的新手。欢迎任何建议。提前谢谢。

5 个答案:

答案 0 :(得分:2)

Hibernate将您的数据库表映射到您创建的项目中的类。为了使用这些类加载和更新DB中的值,您需要告诉Hibernate哪个类被映射到哪个表。这就是hibernate配置文件和Hibernate映射文件的用武之地。

映射可以使用注释或映射文件完成,并在hibernate配置文件中包含映射文件名。

阅读here,了解有关在开始运行项目之前设置环境的这些初始步骤的详细信息。

答案 1 :(得分:1)

我认为您可能正在尝试存储/加载未使用xyz注释正确映射的类@Entity的对象。您希望与Hibernate一起使用的任何类都应该使用注释或使用XML描述符进行映射。

其他可能性是您的映射是正确的,但您既没有在xyz文件中明确列出hibernate.cfg.xml也没有启用实体的自动检测。

如上所述,如果没有看到一些实际的代码,很难给出明确的答案。

答案 2 :(得分:0)

@Entity放入您的班级。

@Entity
@Table(name="tableName")
public class XYZ {


}

答案 3 :(得分:0)

如果使用hbm文件,则需要检查映射,然后检查是否已包含在配置文件中。如果使用注释,则检查是否已放置正确的注释类型。提供精确解决方案所需的更多细节。

答案 4 :(得分:0)

    Dec 11, 2013 4:03:07 PM org.hibernate.annotations.common.Version <clinit>
    INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}

Dec 11, 2013 4:03:07 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.8.Final}
Dec 11, 2013 4:03:07 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Dec 11, 2013 4:03:07 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Dec 11, 2013 4:03:07 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Dec 11, 2013 4:03:07 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Dec 11, 2013 4:03:07 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Dec 11, 2013 4:03:07 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Dec 11, 2013 4:03:07 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
Dec 11, 2013 4:03:07 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
Dec 11, 2013 4:03:07 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/testdb]
Dec 11, 2013 4:03:07 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****}
Dec 11, 2013 4:03:08 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Dec 11, 2013 4:03:08 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Dec 11, 2013 4:03:08 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Dec 11, 2013 4:03:08 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Dec 11, 2013 4:03:08 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
org.hibernate.MappingException: Unknown entity: com.sanjay.UserDetails
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1145)
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1358)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:116)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:683)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:675)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)
    at com.sanjay.UserTest.main(UserTest.java:21)