aspose file tools*
The moose likes Object Relational Mapping and the fly likes JPA2.0 CascadeType.ALL doesn't cascade persist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA2.0 CascadeType.ALL doesn Watch "JPA2.0 CascadeType.ALL doesn New topic
Author

JPA2.0 CascadeType.ALL doesn't cascade persist

Jelo Nehuptra
Ranch Hand

Joined: Oct 08, 2011
Posts: 36

I have the following code which doesn't cascade persist




Line 9-11 cascades ok but when it reaches line 12 it causes an error.


Person.java


Examinee.java


And I'm getting the following stack trace
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553


Your mappings do not make any sense. You have examinee mappedBy = "person" and person mappedBy = "idPerson3". You cannot have multiple mapped by, I think you meant to define PERSON_ID as the join column?

But perhaps include your schema.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Jelo Nehuptra
Ranch Hand

Joined: Oct 08, 2011
Posts: 36

James Sutherland wrote:
But perhaps include your schema.


This is a part of my schema. The cascade persist works on the recursive relationship but not on the other relationship.
The code was generated by Netbeans7.1, i used the 'create entity classes from database'


[Thumbnail for sis.png]

James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
Oh, I see Examinee is a different class. Please include the code for it.

Where does examResults come from? Is it part of the persistence context? Is it new or existing, perhaps you need to merge it, or persist it separately.

Do you have a cyclic relationship? One of the foreign keys may need to be nullable to resolve a cycle, setting logging to finest will give some insight on what is going on.
Jelo Nehuptra
Ranch Hand

Joined: Oct 08, 2011
Posts: 36

this is the code for the examinee class

Jelo Nehuptra
Ranch Hand

Joined: Oct 08, 2011
Posts: 36

James Sutherland wrote:
Where does examResults come from? Is it part of the persistence context? Is it new or existing, perhaps you need to merge it, or persist it separately.


I think it is coming from the same persistence context. When i persist the two entity classes together with their corresponding facades, it works, but when i try the cascade it doesn't.
How do i know if it is new or existing? the persistence context or the injected facade class?

this is the code for the injected '@EJB ExamineeFacade examResults;'


And the '@EJB PersonFacade person;'

Jelo Nehuptra
Ranch Hand

Joined: Oct 08, 2011
Posts: 36

James Sutherland wrote:
Do you have a cyclic relationship? One of the foreign keys may need to be nullable to resolve a cycle, setting logging to finest will give some insight on what is going on.


I do have a cyclic/recursive relationship in the Person entity,
because of:
Person examinee;
Person father;
Person mother;
Person guardian;

If i will have the Examinee table to have the nullable foreign key, the 1 to 1 identifying relationship will be gone, thus, a examinee could have many examResults which shouldn't be.

I'll set the logging to finest, thanks for the advice.
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553


You have "insertable = false" on the foreign key, so if you did not set your basic idPerson field value, then it will be null. You need to set the idPerson field value.

You may also want to have insertable/updatable = false on the Id and true on the OneToOne, then it will pick up the value from the reference.

You could also remove the basic idPerson and just put @Id on the OneToOne.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JPA2.0 CascadeType.ALL doesn't cascade persist