CascadeType.PERSIST question
ttrepper Sep 8, 2007 5:50 AMHi all,
I am running into troubles with the cascdetype, Please imagine the following szenario:
SQL (tables)
CREATE TABLE Party ( PartyID BIGINT DEFAULT nextval('seq_PartyID') NOT NULL, CONSTRAINT PK_Party PRIMARY KEY (PartyID) ); CREATE TABLE PartyIsPerson ( PartyIDPerson BIGINT NOT NULL, Forename CHARACTER VARYING(80) NOT NULL, Lastname CHARACTER VARYING(80) NOT NULL, CONSTRAINT PK_PartyIsPerson PRIMARY KEY (PartyIDPerson) ); CREATE TABLE PersonHasCustomization ( PartyIDPerson BIGINT NOT NULL, Locale CHARACTER VARYING(5), CONSTRAINT PK_PersonHasCustomization PRIMARY KEY (PartyIDPerson) );
The Party-class ist declared with @Inheritance(strategy=InheritanceType.JOINED)
and the PartyIsPerson extends from it.
package com.auctonova.ejb.persistence; import java.io.Serializable; import java.sql.Date; import javax.persistence.Entity; import javax.persistence.OneToOne; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.CascadeType; @Entity @PrimaryKeyJoinColumn(name="partyidperson", referencedColumnName="partyid") public class PartyIsPerson extends Party implements Serializable { private static final long serialVersionUID = 1L; private String forename; private String lastname; private PersonHasCustomization customization; //-------------------------------------------------------------------------- public String getForename() { return this.forename; } //-------------------------------------------------------------------------- public void setForename(String forename) { this.forename = forename; } //-------------------------------------------------------------------------- public String getLastname() { return this.lastname; } //-------------------------------------------------------------------------- public void setLastname(String lastname) { this.lastname = lastname; } //-------------------------------------------------------------------------- @OneToOne(cascade=CascadeType.PERSIST) @PrimaryKeyJoinColumn(name="partyidperson", referencedColumnName="partyidperson") public PersonHasCustomization getCustomization() { return this.customization; } //-------------------------------------------------------------------------- public void setCustomization(PersonHasCustomization customization) { this.customization = customization; } }
When I am now trying to execute the following code in a bean
PartyIsPerson p = new PartyIsPerson(); p.setUsername("Guest"); //Guest p.setPassword("Guest"); //Guest p.setForename("Guest"); //Guest p.setLastname("Guest"); //Guest p.setPartyId(100); PersonHasCustomization phc = new PersonHasCustomization(); phc.setLocale("DE_de"); p.setCustomization(phc); EntityManager.persist(p)
than I get the following error:
11:19:28,953 ERROR [JDBCExceptionReporter] Batch-Eintrag 0 insert into PersonHasCustomization (locale, pm_forward_to, partyidperson) values (DE_de, NULL, 0) wurde abgebrochen. Rufen Sie 'getNextException' auf, um die Ursache zu erfahren. 11:19:28,953 WARN [JDBCExceptionReporter] SQL Error: 0, SQLState: 23503 11:19:28,953 ERROR [JDBCExceptionReporter] ERROR: insert or update on table "personhascustomization" violates foreign key constraint "partyisperson_personhascustomization" Detail: Key (partyidperson)=(0) is not present in table "partyisperson". 11:19:28,953 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
In my opinion this part of the exception
Detail: Key (partyidperson)=(0) is not present in table "partyisperson"
means, that something is wrong with the Primary-keys, of course there is no person with the id=0, but i want the EntityManager automatically to assign the customisation the SAME id (in this case 100), before inserting both.
Does anybody has an idea, what i am doing wrong?
Thank you very much for help and kind regards,
Thomas