Hi -- I have a persistent many- to-many map where the key is an entity. Is this supported? I have found _no_ examples of this, but I don't get mapping errors so my initial assumption was that it would be possible.
The parent entity:
Code:
@Entity
public class Portfolio {
@Id @GeneratedValue
private Long ID;
@ManyToMany(cascade=CascadeType.ALL) @OrderBy
private Map<Profile,ProfileWeight> profiles = new LinkedHashMap<Profile,ProfileWeight>();
}
The "key" child (Profile) is just another @Entity, I don't think it really matters what is in that. And the ProfileWeight is just a Double wrapped in an @Entity POJO.
Really, what this is modelling is a set of performance profiles, each with a corresponding weight. So a single profile can be in many Portfolios, with different weights.
Hibernate generates the tables correctly when the app starts up, but when I add values to the map and try to save the Portfolio, I get the following exception:
Code:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: Profile
which is generated after I call entityManager.persist(portfolio) and entityManager.flush(). As far as I can tell this shouldn't be happening because I have the cascasde set to ALL
If I manually try to save the Profile keys first, I don't get any exception but it looks like the map values are not persisted.
Here is the ProfileWeight class:
Code:
@Entity
@Table(name="profile_weight")
public class ProfileWeight {
@Id @GeneratedValue
private Integer ID;
@Version
private Integer version;
private double weight;
@Override
public boolean equals(Object obj) {
return super.equals(obj) ||
(obj instanceof ProfileWeight && ((ProfileWeight)obj).ID == this.ID );
}
@Override
public int hashCode() {
if( ID != null ) return ID;
return super.hashCode();
}
public ProfileWeight(double weight) {
this.weight = weight;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
Hibernate is auto-generating the actual DB schema. Any help would be appreciated -- thanks!