I'm developing a Play 2.1 webapp (using EBean as the ORM), and I'm having some trouble with my models one-to-one-relationships.
I have two models. A person:
@Entity
@Table(name="PERSON")
public class Person extends Model {
@Id
@GeneratedValue
public long id;
@Version
public long version;
@OneToOne(mappedBy="owner",
cascade=CascadeType.ALL,
optional = true)
@Valid
public MiscInformation miscInformation;
...
}
And a MiscInformation:
@Entity
@Table(name="MISC_INFORMATION")
public class MiscInformation extends Model {
@Id
@GeneratedValue
public long id;
@OneToOne(optional = false)
@JoinColumn(name = "person_id", unique = true)
public Person owner;
@Version
public long version;
...
}
And this is the code that does the save/update of the Person:
Person foundPerson = getPersonFromDatabase(p);
if(foundPerson != null) {
p.update(p.id);
} else {
p.save();
}
When I create a new Person, foundPerson in this code snippet becomes null (since the id is negative) and everything works fine. Logs from EBean shows that inserts are performed all around.
When I use a view in Play to do a bindFromRequest into a Person and run the code to save/update again, an existing Person is found in the database and the p.update(
p.id) is run.
The EBean SQL logs show that the Person is correctly updated, and has it's version increased and everything. The MiscInformation is however not being updated, but instead inserted!
[debug] c.j.b.PreparedStatementHandle - insert into MISC_INFORMATION (id, gender, percent_research, docent_qualification, docent_degree_year, version, person_id) values(1,'male','80',true,'1995',1,1)
Which causes:
javax.persistence.PersistenceException: ERROR executing DML bindLog[] error[ERROR: duplicate key value violates unique constraint "pk_misc_information"\n Detail: Key (id)=(1) already exists.]
I also have several models marked with @OneToMany in the Person-class, which works fine when performing the p.update(..).
Any ideas?
Thanks!