1

I am curious about this error

org.hibernate.MappingException: Unknown entity: xyz

I am new to hibernate. Any suggestions are welcome. Thanks in advance.

4
  • 1
    There is no entity class in the name of xyz Commented Dec 11, 2013 at 10:33
  • Showing some code would really help. The error is straightforward, as @Gk pointed out, but if you want help fixing it try posting some code Commented Dec 11, 2013 at 10:34
  • Thanks for reply but "Xyz" is my bean name and going to save it is database Commented Dec 11, 2013 at 10:35
  • If your entity is xyz you need to have a table with the same name in the DB. If the column name is different, you have to tell spring to map the xyz class on a certain table. You can do this with annotations: @Table(name="tableNameHere") Commented Dec 11, 2013 at 10:37

5 Answers 5

2

Hibernate maps your DB tables to the classes in your project that you have created. In order to load and update values in the DB using these classes, you need to tell Hibernate wich class is mapped to which table. This is where the hibernate configuration file and the Hibernate mapping file comes into picture.

Mapping can be done using annotations or with with a mapping file and include the mapping file name in the hibernate config file.

Read here for more information about these initial steps to setup your environment before you start running your project.

Sign up to request clarification or add additional context in comments.

Comments

1

I think you might be trying to store/load an object of class xyz which is not properly mapped with @Entity annotation. Any class that you want to use with Hibernate should be mapped either with annotations or using an XML descriptor.

Other possibility is that your mapping is correct, but you didn't neither explicitly list xyz in hibernate.cfg.xml file nor enabled autodetection of entities.

And as mentioned above, without seeing some actual code it's really hard to give an definitive answer.

Comments

0

Put @Entity in your class.

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


}

2 Comments

hi,is it necessory to put @Table(name="tableName"),
It is necessary if the class name does not match the DB Table name
0

you need to check mappings if using hbm files then check you have included then in config file.and if using annotations then check if you have placed correct annotation type. More details required to provide exact solution.

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.