• Post Reply Bookmark Topic Watch Topic
  • New Topic

unable to persist an entity using JPA

 
Suresh Magi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I resolved all my problems but not able to solve this issue. When i am calling the persist method of the entity manager the entity is not getting persisted.

I am getting following exception,

Exception in thread "main" javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.edu.entity.EmployeeEntity
at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:63)
at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:83)
at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:95)

I am pasting the code that tries to persist the entity in a bean,

EmployeeEntity objDTO = new EmployeeEntity();

objDTO.setEmpId(2);
objDTO.setStrEmpName("abc");
objDTO.setStrEmpDesig("abc");

entityMgr.persist(objDTO);

please help me in solving this issue.

Thanks,
Suresh B
 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suresh,

The likely cause is that the employee ID is a generated value that should not be manually set like you are doing in the code.

Hope it helps,
Reza
 
Suresh Magi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Reza,

I tried commenting that line also but i was getting the same error. So is that means we cannot insert any entity into the database which has the column for which the id is generated by the database. I have the ID generated sequence is AUTO in my entity class.

How can we solve this issue??

Thanks,
Suresh B
 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suresh,

You probably have another mapping/API issue. You cannot manually set fields that are set to be auto-generated. I would start by checking if you have set ID generation correctly.

Regards,
Reza
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!