Hi All,
I'm facing a problem regarding mapping to tables with a unidirectional relation of ManyToOne.
The tables are built with composite primary key, so the foreign key when the mapping is defined involves the specification of the columns.
here are the two table definition.
leggi( le_id number not null, idcomune varchar(6) not null, le_description varchar(50), le_fkltid number primary key (le_id,idcomune) }
leggitipi( lt_id number not null, idcomune varchar(6) not null, lt_description varchar(50), primary key (lt_id,idcomune) }
leggitipi is a category for leggi
and the code of the classes is
Leggitipi.java
@Entity @Table(name = "LEGGITIPI") public class Leggitipi implements java.io.Serializable {
/** * */ private static final long serialVersionUID = 2723652995788954217L; private PkId id; private String ltDescrizione;
public Leggitipi() { }
@EmbeddedId @GenericGenerator( name="pkGenerator", strategy="CustomHiLoIdGenerator", parameters={@Parameter(name=CustomHiLoIdGenerator.MAX_LO,value="2")} ) @GeneratedValue(generator="pkGenerator") @AttributeOverrides( { @AttributeOverride(name = "idcomune", column = @Column(name = "IDCOMUNE", nullable = false, length = 6)), @AttributeOverride(name = "codice", column = @Column(name = "LT_ID", nullable = false, precision = 2, scale = 0)) }) public PkId getId() { return this.id; }
public void setId(PkId id) { this.id = id; }
@Column(name = "LT_DESCRIZIONE", length = 20) public String getLtDescrizione() { return this.ltDescrizione; }
public void setLtDescrizione(String ltDescrizione) { this.ltDescrizione = ltDescrizione; } }
for Leggi.java
@Entity @Table(name = "LEGGI") public class Leggi implements java.io.Serializable {
/** * */ private static final long serialVersionUID = 4305934341748408913L;
private PkId id; private String leDescrizione; private Leggitipi leggitipi; public Leggi() { }
@EmbeddedId @GenericGenerator( name="pkGenerator", strategy="CustomHiLoIdGenerator", parameters={@Parameter(name=CustomHiLoIdGenerator.MAX_LO,value="2")} ) @GeneratedValue(generator="pkGenerator") @AttributeOverrides( { @AttributeOverride(name = "idcomune", column = @Column(name = "IDCOMUNE", nullable = false, length = 6)), @AttributeOverride(name = "codice", column = @Column(name = "LE_ID", nullable = false, precision = 4, scale = 0)) }) public PkId getId() { return this.id; }
public void setId(PkId id) { this.id = id; }
@Column(name = "LE_DESCRIZIONE", length = 50) public String getLeDescrizione() { return this.leDescrizione; }
public void setLeDescrizione(String leDescrizione) { this.leDescrizione = leDescrizione; } @ManyToOne(fetch=FetchType.EAGER) @JoinColumns({ @JoinColumn(name="LE_FKLTID", referencedColumnName="LT_ID",nullable=false ,insertable=false,updatable=false) , @JoinColumn(name="IDCOMUNE" , referencedColumnName="IDCOMUNE", nullable=false,insertable=false,updatable=false) }) public Leggitipi getLeggitipi() { return leggitipi; }
public void setLeggitipi(Leggitipi leggitipi) { this.leggitipi = leggitipi; }
}
with the mapping so defined the relation is done correctly, for the query hit by the database to retrieve records works fine.
select this_.LE_ID as LE1_3_1_, this_.IDCOMUNE as IDCOMUNE3_1_, this_.CODICEOGGETTO as CODICEOG3_3_1_, this_.LE_ALLEGATO as LE4_3_1_, this_.LE_DESCRIZIONE as LE5_3_1_, this_.LE_LINK as LE6_3_1_, this_.LE_FKLTID as LE7_3_1_, leggitipi2_.LT_ID as LT1_4_0_, leggitipi2_.IDCOMUNE as IDCOMUNE4_0_, leggitipi2_.LT_DESCRIZIONE as LT3_4_0_ from LEGGI this_, LEGGITIPI leggitipi2_ where this_.LE_FKLTID=leggitipi2_.LT_ID and this_.IDCOMUNE=leggitipi2_.IDCOMUNE and this_.IDCOMUNE=?
the problem occurs when I try to update or insert the Object.
the update statement doesn't hit the foreign key even it is populated.
This I think Occurs because I defined
insertable and updatable = false in @ManyToOne relation.
Otherwise if I remove Inserstable and updatable (default=true) in the @ManyToOne relation another error occurs i.e.:
Repeated column in mapping for entity: Leggi column: IDCOMUNE (should be mapped with insert="false" update="false")
any help would be appreciated please.
Hibernate version:
Hibernate 3.3.0.GA
Hibernate Annotations 3.4.0.GA
Hibernate Commons Annotations 3.1.0.GA
Name and version of the database you are using:
Oracle 9i[/b]
|