Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJB 3.0 - Error while persisting one-one unidirectional relation

 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Benoît de Chateauvieux
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Thanks so much. @OneToOne(cascade={CascadeType.ALL}) solved the problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic