Hi, I am having problems using the annotation @ManyToOne twice for the same target entity from the same source class (both source and target have composite pk). I have an entity that can be ditributable usging two diferent distributors, but each distributor can be associated with a lot of these entities. So I model this situation with 2 @ManyToOne, being the one side the Distributor object. I use naif name of fields for the sake of simplicity: a, b, c, etc.
Distributor pk = a, b, c, d (also part of the source entity pk)
@Entity
...
public class Distributable {
@ManyToOne
@JoinColumns({@JoinColumn(name="a", referencedColumnName="a"), @JoinColumn(name="b", referencedColumnName="b"), @JoinColumn(name="c", referencedColumnName="c"), @JoinColumn(name="d1", referencedColumnName="d")})
public Distributor getDistributor1() {
return distributor1;
}
public void setDistributor1(Distributor distributor1) {
this.distributor1 = distributor1;
}
@ManyToOne
//@JoinColumn(insertable = false, updatable = false)
@JoinColumns({@JoinColumn(name="a", referencedColumnName="a"), @JoinColumn(name="b", referencedColumnName="b"), @JoinColumn(name="c", referencedColumnName="c"), @JoinColumn(name="d2", referencedColumnName="d")})
public Distributor getDistributor2() {
return distributor2;
}
public void setDistributor2(Distributor distributor2) {
this.distributor2 = distributor2;
}
Problems:
When trying to deploy the following error is reported:
1)
javax.persistence.PersistenceException: org.hibernate.MappingException: Repeated column in mapping for entity: foo.Distributable column: b (should be mapped with insert="false" update="false")
Don't understand why, but in any case I then uncomment the line with "insertable = false..." and then get another error:
2)
org.hibernate.AnnotationException: A Foreign key refering foo.Distributor from foo.Distributable has the wrong number of column. should be 4
I double check each entity of the relation and their pks and they all have the requested 4 fields (i.e. the target composite primary key number of fields)
********************************************************
The only way I could go on with development was to full comment one of the @ManyToOne relationships
Any sugestions?
|