1

I am new to Hibernate and JPA .Right now I am using EntityManagerFactory Instance to create EntityManager then I have UnWrap this EntityManager into Session using

entityManager = entityManagerFactory.createEntityManager();
session = entityManager.unwrap(Session.class);

By using EntityManagerFacotry we no need to create Hibernateconfig.xml we can use Persistence.xml.But My problem is I am not able to Hibernate Mapping file to map the Entity Class when I am using Peristenece.xml

Can use Hibernate Mapping file to to map the entity class using EntityManagerFactory?

user1679378
  • 1,370
  • 3
  • 17
  • 23
  • Why would you want to use XML mapping? Use annotations. – Ryan Stewart Mar 06 '13 at 05:23
  • 1
    You will get clear idea.Refer below link. http://stackoverflow.com/questions/5640778/hibernate-sessionfactory-vs-entitymanagerfactory – Chakri Chakradhar Mar 06 '13 at 05:29
  • My requirement is I need to use single class without annotations to map different tables dynamically.Its possible only using Hibernate mapping file using session.save(entityName,object).But I am not able to use in EntityManagerFactory..but Its working in sessionFactory – user1679378 Mar 06 '13 at 05:37

2 Answers2

1

The short answer is yes, you can use hibernate.cfg.xml with JPA. Just pretend that you are using hibernate directly when creating the mapping. But, why are you using the Session? Don't you use EntityManager#persist()? or #createQuery()? (btw, EntityManager is JPA, Session is Hibernate)

Zagrev
  • 2,000
  • 11
  • 8
  • I configured hibernate-cfg.xml in persistence.xml and hibernate-hbm.xml in hibernate-cfg.xml.But I tried to deploy this code I am getting Hibernate mapping Exception. – user1679378 Mar 06 '13 at 05:50
1

It's kind of strange that you use jpa while you still want to use hibernate session. But for you question, yes that you can do that. here is another question about this:How to load a Hibernate 'xxx.hbm.cfg' file in a JPA 2.0 project?. And also there is a tag , it can import the hbm.xml file.

Community
  • 1
  • 1
OQJF
  • 1,350
  • 9
  • 12
  • Are We need to use @Entity annotations for that class? – user1679378 Mar 06 '13 at 06:10
  • actually if you import the hbm.xml you don't need to annotated the domain class with @Entity. since you mentioned this, I also highly recommend you to just annotated the class with Entity and remove the hbm.xml files, CUZ it's more convenient and can be better for the further work. – OQJF Mar 06 '13 at 06:20
  • My requirement is I need to use single class without annotations to map different tables dynamically..Its not possible in using annotations. – user1679378 Mar 06 '13 at 06:21
  • what's dynamically? you mean the table relationship will be changed for some reasons in the class? – OQJF Mar 06 '13 at 06:24
  • I have 5 tables.Table name should be different but Schema should be common for all the tables.But I need to use only one class to map all the tables dynamically. – user1679378 Mar 06 '13 at 06:36