permaculture playing cards*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes EJB 3.0 - Error while persisting one-one unidirectional relation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "EJB 3.0 - Error while persisting one-one unidirectional relation" Watch "EJB 3.0 - Error while persisting one-one unidirectional relation" New topic
Author

EJB 3.0 - Error while persisting one-one unidirectional relation

Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Hi

I have 2 tables, EMPLOYEE and ADDRESS. Table EMPLOYEE has ADDRESSID column which has a foreign key relationship with ADDRESS whose primary key is ADDRESSID. Using EJB3.0 java persisatance API I wanted to implement one-to-one unidirectional relationship. Hence I created 2 entity classes, Employee and Address. In the Employee class I putting below the relevant code:


In the Address class, there is no code to access the Employee.

I have created a session bean which tries to create an employee along with an Address. Putting the relevant code below:


As mentioned above, error is being thrown at em.persist(emp);. The error is



I am unable to make out anything from the exception stack trace. Any help in this would be greatly appreciated.


Thanks and Regards
Benoît de Chateauvieux
Ranch Hand

Joined: Aug 10, 2007
Posts: 183
Hi Satya,

I think your problem comes from the cascadeType.
You've got to mark your OneToOne with a cascade type at least PERSIST.
If you don't, you'll have an error of type:

"During synchronization a new object was found through a relationship that was not marked cascade PERSIST."

Can you tell me if this corrects your problem ?

Beno�t


SCJP5 | SCBCD5 | SCEA5 Part 1
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Yes. Thanks so much. @OneToOne(cascade={CascadeType.ALL}) solved the problem.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: EJB 3.0 - Error while persisting one-one unidirectional relation