Skip to main content

Secondary tables +optimistic locking

1 reply [Last post]
magott
Offline
Joined: 2006-11-27

Hi,

Let's say I have to tables: EMPLOYEE(emp_id, name, version) and ADDRESS(adr_id, street, emp_id, version), both have a version field.

I have the following Employee-object, using the secondarytable annotation to get the address fields as well:

@Entity
@SecondaryTable(name="ADDRESS", pkJoinColumns=@PrimaryKeyJoinColumn(name="EMP_ID"))

public class Employee{
@Id
private String emp_id;

@Column(table="street")
private String street;

private String name;

...
}

QUESTION 1: How do I handle multiple version fields? (one for each table, assuming that the address table might be updated in other ways that through the employee-object)

QUESTION 2: what if the relation was the other way around, with the following database:
EMPLOYEE(emp_id, name, version, adr_id)
ADDRESS(adr_id, street, version)
Is there a way to solve this, without making the Address table the primary table, and the employee the secondary?

QUESTION 3: what if the relation was set up through a mapping table, giving the following database:
EMPLOYEE(emp_id, name, version)
ADDRESS(adr_id, street, version)
EMP_ADR(emp_adr_id, emp_id, adr_id)

Message was edited by: magott (Made the questions more clear, added a new question)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
james_sutherland
Offline
Joined: 2006-11-07

You can only have a single version column. It would probably be best to define an Employee and an Address object and define a 1-1 relationship between the two.

Otherwise you could attempt to maintain the secondary version field manually or through an update event, but mapping the Address as a separate object would be best. If you want the Address to always be read with the Employee you could enable joining on the TopLink OneToOneMapping through a code TopLink DescriptorCustomizer.

#2 - You could still define a 1-1 mapping in this case. If you really want to define a multiple table mapping, you could do this through using the TopLink code API and a DescriptorCustomizer as I do not believe this is covered directly in JPA annotations.

#3 - You could define this 1-1 join table as part of the Employee's multiple tables if it always exists, or define it as a m-m mapping and just ensure that only a single element is put in the collection.