You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Jason Hanna <ja...@gmail.com> on 2009/02/18 00:29:35 UTC

Entity Version Fields and Bi-Directional One-to-Many Relationships with Cascade Merge

Hi All,

I've got a question about standard entity versioning behavior with
OpenJPA 1.2.0 and what I should be seeing in my tables. I'm working on
a project with detached entities (for Servlet and Flex/Flash Remoting)
and I can't seem to find the information I'm looking for. Not sure if
I've got some logic issues or what I'm experiencing is expected
behavior.

Let's assume I have two entities, Person and Address. Each has its own
database table, and there is a one-to-many relationship between my
address and person tables (i.e. many people can reside at one
address). No join tables, simply a foreign-key column
"primary_address_id" on the person table.

The entities maintain bi-directional relations (i.e. Address entity
has SortedSet of people and my Person entity has an address
attribute). Cascade merge and persist are enabled on both sides of the
relationship. Each table has a version column of type int.

Additionally, I have DAO objects to perform common add, update,
delete, and get operations.

Now on to my question(s):

* If I add a new Person to the (SortedSet) people attribute on Address
entity x, then merge Address x using my AddressDAO, should the version
column value in row x of my address table be incremented?
* What if I merge from the other side of the relationship? (i.e.
reference an existing Address from a new Person object y and use my
PersonDAO to merge the change).

Just trying to get a sense for how the version increment rules are
supposed to work with respect to related objects.

Regards,
-jmh

Re: Entity Version Fields and Bi-Directional One-to-Many Relationships with Cascade Merge

Posted by Miłosz Tylenda <mt...@o2.pl>.
Jason,

I have done some testing and saw version fields behaving like I supposed earlier.

- If you persist a new Person, its Address version does not change.
- If you modify Person's property, its Address version does not change. It does not matter whether you em.find() the Person or you em.find() the Address and then navigate to the Person. The Person version does change.
- If you reassign a Person to a different Address, the versions of Addresses do not change. The Person version does change.
- If you modify Address' property, the Address version does change.

Milosz


> Hi Jason,
> 
> I spoke too early. Now I see I might have confused version checks with version updates. Will do some tests when I have some time.
> 
> Regards,
> Milosz
> 
> 
> > Hi Jason,
> > 
> > This is what the JPA 1 spec says on this subject:
> > 
> > "The version attribute is updated by the persistence provider runtime when the object is written to the
> > database. All non-relationship fields and properties and all relationships owned by the entity are
> > included in version checks."
> > 
> > To answer your questions:
> > 
> > > * If I add a new Person to the (SortedSet) people attribute on Address
> > > entity x, then merge Address x using my AddressDAO, should the version
> > > column value in row x of my address table be incremented?
> > > * What if I merge from the other side of the relationship? (i.e.
> > > reference an existing Address from a new Person object y and use my
> > > PersonDAO to merge the change).
> > 
> > It does not matter which DAO you use. Address version will not change, only Person version will change because this is the Person who is the owner of the relationship. In a bi-directional one-to-many relationship, the "many" side always owns the relationship (as it has the foreign key column).
> >  
> > This is how I understand it but did not check whether it actually works like this.
> > 
> > Greetings,
> > Milosz
> > 
> > 
> > > Hi All,
> > > 
> > > I've got a question about standard entity versioning behavior with
> > > OpenJPA 1.2.0 and what I should be seeing in my tables. I'm working on
> > > a project with detached entities (for Servlet and Flex/Flash Remoting)
> > > and I can't seem to find the information I'm looking for. Not sure if
> > > I've got some logic issues or what I'm experiencing is expected
> > > behavior.
> > > 
> > > Let's assume I have two entities, Person and Address. Each has its own
> > > database table, and there is a one-to-many relationship between my
> > > address and person tables (i.e. many people can reside at one
> > > address). No join tables, simply a foreign-key column
> > > "primary_address_id" on the person table.
> > > 
> > > The entities maintain bi-directional relations (i.e. Address entity
> > > has SortedSet of people and my Person entity has an address
> > > attribute). Cascade merge and persist are enabled on both sides of the
> > > relationship. Each table has a version column of type int.
> > > 
> > > Additionally, I have DAO objects to perform common add, update,
> > > delete, and get operations.
> > > 
> > > Now on to my question(s):
> > > 
> > > * If I add a new Person to the (SortedSet) people attribute on Address
> > > entity x, then merge Address x using my AddressDAO, should the version
> > > column value in row x of my address table be incremented?
> > > * What if I merge from the other side of the relationship? (i.e.
> > > reference an existing Address from a new Person object y and use my
> > > PersonDAO to merge the change).
> > > 
> > > Just trying to get a sense for how the version increment rules are
> > > supposed to work with respect to related objects.
> > > 
> > > Regards,
> > > -jmh
> > > 
> > 
> 

Re: Entity Version Fields and Bi-Directional One-to-Many Relationships with Cascade Merge

Posted by Miłosz Tylenda <mt...@o2.pl>.
Hi Jason,

I spoke too early. Now I see I might have confused version checks with version updates. Will do some tests when I have some time.

Regards,
Milosz


> Hi Jason,
> 
> This is what the JPA 1 spec says on this subject:
> 
> "The version attribute is updated by the persistence provider runtime when the object is written to the
> database. All non-relationship fields and properties and all relationships owned by the entity are
> included in version checks."
> 
> To answer your questions:
> 
> > * If I add a new Person to the (SortedSet) people attribute on Address
> > entity x, then merge Address x using my AddressDAO, should the version
> > column value in row x of my address table be incremented?
> > * What if I merge from the other side of the relationship? (i.e.
> > reference an existing Address from a new Person object y and use my
> > PersonDAO to merge the change).
> 
> It does not matter which DAO you use. Address version will not change, only Person version will change because this is the Person who is the owner of the relationship. In a bi-directional one-to-many relationship, the "many" side always owns the relationship (as it has the foreign key column).
>  
> This is how I understand it but did not check whether it actually works like this.
> 
> Greetings,
> Milosz
> 
> 
> > Hi All,
> > 
> > I've got a question about standard entity versioning behavior with
> > OpenJPA 1.2.0 and what I should be seeing in my tables. I'm working on
> > a project with detached entities (for Servlet and Flex/Flash Remoting)
> > and I can't seem to find the information I'm looking for. Not sure if
> > I've got some logic issues or what I'm experiencing is expected
> > behavior.
> > 
> > Let's assume I have two entities, Person and Address. Each has its own
> > database table, and there is a one-to-many relationship between my
> > address and person tables (i.e. many people can reside at one
> > address). No join tables, simply a foreign-key column
> > "primary_address_id" on the person table.
> > 
> > The entities maintain bi-directional relations (i.e. Address entity
> > has SortedSet of people and my Person entity has an address
> > attribute). Cascade merge and persist are enabled on both sides of the
> > relationship. Each table has a version column of type int.
> > 
> > Additionally, I have DAO objects to perform common add, update,
> > delete, and get operations.
> > 
> > Now on to my question(s):
> > 
> > * If I add a new Person to the (SortedSet) people attribute on Address
> > entity x, then merge Address x using my AddressDAO, should the version
> > column value in row x of my address table be incremented?
> > * What if I merge from the other side of the relationship? (i.e.
> > reference an existing Address from a new Person object y and use my
> > PersonDAO to merge the change).
> > 
> > Just trying to get a sense for how the version increment rules are
> > supposed to work with respect to related objects.
> > 
> > Regards,
> > -jmh
> > 
> 

Re: Entity Version Fields and Bi-Directional One-to-Many Relationships with Cascade Merge

Posted by Miłosz Tylenda <mt...@o2.pl>.
Hi Jason,

This is what the JPA 1 spec says on this subject:

"The version attribute is updated by the persistence provider runtime when the object is written to the
database. All non-relationship fields and properties and all relationships owned by the entity are
included in version checks."

To answer your questions:

> * If I add a new Person to the (SortedSet) people attribute on Address
> entity x, then merge Address x using my AddressDAO, should the version
> column value in row x of my address table be incremented?
> * What if I merge from the other side of the relationship? (i.e.
> reference an existing Address from a new Person object y and use my
> PersonDAO to merge the change).

It does not matter which DAO you use. Address version will not change, only Person version will change because this is the Person who is the owner of the relationship. In a bi-directional one-to-many relationship, the "many" side always owns the relationship (as it has the foreign key column).
 
This is how I understand it but did not check whether it actually works like this.

Greetings,
Milosz


> Hi All,
> 
> I've got a question about standard entity versioning behavior with
> OpenJPA 1.2.0 and what I should be seeing in my tables. I'm working on
> a project with detached entities (for Servlet and Flex/Flash Remoting)
> and I can't seem to find the information I'm looking for. Not sure if
> I've got some logic issues or what I'm experiencing is expected
> behavior.
> 
> Let's assume I have two entities, Person and Address. Each has its own
> database table, and there is a one-to-many relationship between my
> address and person tables (i.e. many people can reside at one
> address). No join tables, simply a foreign-key column
> "primary_address_id" on the person table.
> 
> The entities maintain bi-directional relations (i.e. Address entity
> has SortedSet of people and my Person entity has an address
> attribute). Cascade merge and persist are enabled on both sides of the
> relationship. Each table has a version column of type int.
> 
> Additionally, I have DAO objects to perform common add, update,
> delete, and get operations.
> 
> Now on to my question(s):
> 
> * If I add a new Person to the (SortedSet) people attribute on Address
> entity x, then merge Address x using my AddressDAO, should the version
> column value in row x of my address table be incremented?
> * What if I merge from the other side of the relationship? (i.e.
> reference an existing Address from a new Person object y and use my
> PersonDAO to merge the change).
> 
> Just trying to get a sense for how the version increment rules are
> supposed to work with respect to related objects.
> 
> Regards,
> -jmh
>