]
Gail Badner resolved HHH-5091.
------------------------------
Assignee: Gail Badner
Resolution: Cannot Reproduce
Jürgen, thanks for letting us know this is fixed!
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
Assignee: Gail Badner
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: