-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Problem Mapping ManyToOne with Composite Key and foreign key
PostPosted: Mon Aug 25, 2008 7:06 am 
Newbie

Joined: Mon Aug 25, 2008 6:43 am
Posts: 3
Location: Italy
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]


Top
 Profile  
 
 Post subject: Hibernate lack?
PostPosted: Mon Aug 25, 2008 9:21 am 
Newbie

Joined: Mon Aug 25, 2008 6:43 am
Posts: 3
Location: Italy
Hi Following the similar threads described in
http://forum.hibernate.org/viewtopic.php?t=973585&highlight=repeated+column+mapping+composite+fk
and
http://forum.hibernate.org/viewtopic.php?p=2325014#2325014, where it seems that only the workaround described below is possible,
I've added a private property that permits hibernate to insert/update the value for the field.



private Integer leggitipiId;

@Column(name="LE_FKLTID")
private Integer getLeggitipiId() {
if (null!=this.getLeggitipi()){
if (null!=this.getLeggitipi().getId()){
this.leggitipiId = getLeggitipi().getId().getCodice();
return this.leggitipiId;
}
}
return null;
}

private void setLeggitipiId(Integer leggitipiId) {
if (null!=this.getLeggitipi()){
if (null!=this.getLeggitipi().getId()){
this.leggitipiId = getLeggitipi().getId().getCodice();
}
}
}


This is obviously a workaround and not very elegant because force me to have a dirty domain object.
My question is: is that a lack of hibernate or is there a better way of configure/resolve the matter?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 25, 2008 3:38 pm 
Beginner
Beginner

Joined: Fri Aug 05, 2005 3:36 am
Posts: 28
Do us a favor: use the 'code' format tags, to make it easier to read the code you've pasted into the message.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.