[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-5091) Unidirectional One-To-Many relationship with foreign key mapping doesn't work
JOSÉ AUGUSTO CERQUEIRA CONDÉ (JIRA)
noreply at atlassian.com
Wed May 26 10:05:54 EDT 2010
[ http://opensource.atlassian.com/projects/hibernate/browse/HHH-5091?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=37277#action_37277 ]
JOSÉ AUGUSTO CERQUEIRA CONDÉ commented on HHH-5091:
---------------------------------------------------
I have this problem too. I´m using a similar unidirectional OneToMany relationship with a foreign key. Hibernate tries to executes the insert with a null foreign key and after that it execute updates with the foreign key value. To workround this we change the foreign key to nullable in database.
> Unidirectional One-To-Many relationship with foreign key mapping doesn't work
> -----------------------------------------------------------------------------
>
> Key: HHH-5091
> URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5091
> Project: Hibernate Core
> Issue Type: Bug
> Affects Versions: 3.5.0-Final
> Reporter: Juergen Zimmermann
>
> I've the following 2 tables (in PostgreSQL) in a one-to-many relationship with foreign key mapping:
> CREATE TABLE bestellung(
> b_id BIGSERIAL NOT NULL PRIMARY KEY,
> version INTEGER DEFAULT 0,
> kunde_fk BIGINT NOT NULL REFERENCES kunde(k_id)
> );
> CREATE TABLE bestellposition(
> bp_id BIGSERIAL NOT NULL PRIMARY KEY,
> version INTEGER DEFAULT 0,
> artikel_fk BIGINT NOT NULL REFERENCES artikel(a_id),
> anzahl SMALLINT NOT NULL,
> bestellung_fk BIGINT NOT NULL REFERENCES bestellung(b_id)
> );
> Now the 2 JPA entity classes:
> // owner class
> @Entity
> @Table(name="bestellung")
> public class Bestellung implements java.io.Serializable {
> @Id
> @GeneratedValue(generator="bestellung_sequence_name")
> @SequenceGenerator(name="bestellung_sequence_name", sequenceName="bestellung_b_id_seq", allocationSize=1)
> @Column(name="b_id", nullable=false)
> private Long id = null;
> @Version
> private int version = 0;
>
> // bidirectional
> @ManyToOne(optional=false)
> @JoinColumn(name="kunde_fk")
> @NotNull(message="{bestellverwaltung.bestellung.kunde.notNull}")
> private Kunde kunde;
> // unidirectional and cascading persist
> @OneToMany(fetch=EAGER, cascade={PERSIST, REMOVE}, orphanRemoval=true)
> @JoinColumn(name="bestellung_fk")
> @OrderBy("id ASC")
> @NotEmpty(message="{bestellverwaltung.bestellung.bestellpositionen.notEmpty}")
> private List<Bestellposition> bestellpositionen;
> }
> // dependant class
> @Entity
> @Table(name="bestellposition")
> public class Bestellposition implements java.io.Serializable {
> @Id
> @GeneratedValue(generator="bestellposition_sequence_name")
> @SequenceGenerator(name="bestellposition_sequence_name", sequenceName="bestellposition_bp_id_seq", allocationSize=1)
> @Column(name="bp_id", nullable=false)
> private Long id = null;
>
> @Version
> private int version = 0;
>
> @Column(nullable=false)
> @Min(value=ANZAHL_MIN, message="{bestellverwaltung.bestellposition.anzahl.min}")
> private short anzahl = 1;
>
> @ManyToOne(optional=false)
> @JoinColumn(name="artikel_fk")
> @NotNull(message="{bestellverwaltung.bestellposition.artikel.notNull}")
> private Artikel artikel;
> }
> Inside the owner class the dependent class is declared with cascade=PERSIST (see above). When I try to create a persistent entity of class Bestellung (owner class) then the generated SQL statement is wrong because the foreign key ("bestellung_fk") for the owner record is missing.
> insert into bestellposition(anzahl, artikel_fk, version, bp_id)
> values(?, ?, ?, ?)
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
More information about the hibernate-issues
mailing list