Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

unidirectional ManyToOne nullable relation

  Asked By: Jada    Date: Oct 31    Category: Java    Views: 1038

There is a question; I would be tank full if anybody helps to solve it.
There is an unidirectional Many to one relation that can be null, I tried to use @ManyToOne(optional=true) or @JoinColumn(nullable=true) annotations or even both at the same time in my entity class.

private Contract contract;

Although I used these annotations, still it does not work and giving me following error:
During synchronization a new object was found through a relationship that was not marked cascade PERSIST: null.



3 Answers Found

Answer #1    Answered By: Skye Hughes     Answered On: Oct 31

Try this:

@ManyToOne(optional=true, cascade={PERSIST, MERGE})
@JoinTable(name="CONTRACT_TABLE", joinColumn=@JoinColumn(name="FOREIGN_KEY_IN_CONT_TABLE"))
private Contract contract

As you said this is a unidirectional relationship so you need to persist or merge the other side of this relationship in a way, to do this you have to use 'cascade' attribute as above

Answer #2    Answered By: Funsani Chalthoum     Answered On: Oct 31

Thank you for your replying, I had been tried your solution but it made me in trouble, when I leave FK field blank (null)and set cascade=PERSIST, when it persist,it will persist a row with all column value (except id) fill with null  for other side of relationship.

Answer #3    Answered By: Randall Franklin     Answered On: Oct 31

Ok that's good news, please show us how could you resolve the problem

Didn't find what you were looking for? Find more on unidirectional ManyToOne nullable relation Or get search suggestion and latest updates.