Map Relation key problem
aidan_b5 Feb 28, 2006 11:59 AMI'm trying to get a Map relation to work with a referenced Key....i.e the Key is another entity bean:
@Entity @Table (name="ship") @NamedQuery(name="ship.getAll", query="from Ship sh") public class Ship { ..... @CollectionOfElements (fetch = FetchType.EAGER) @MapKey(name="nameid") public Map<AttributeName,Attribute> getAttributes() { if((attributes) == null) attributes = new HashMap<AttributeName,Attribute>(); return attributes; } public void setAttributes(Map<AttributeName,Attribute> attributes) { this.attributes = attributes; } private int id; private Map<AttributeName,Attribute> attributes; }
public class Attribute { @Id @GeneratedValue(strategy= GenerationType.AUTO) public int getId() { return id; } public void setId(int id) { this.id = id; } @ManyToOne (cascade = {CascadeType.ALL}) public AttributeName getNameid() { return nameid; } public void setNameid(AttributeName nameid) { this.nameid = nameid; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } @ManyToOne (cascade = {CascadeType.PERSIST}) public Ship getShip(){ return ship; } public void setShip(Ship ship) { this.ship = ship; } public long getStartdate() { return startdate; } public void setStartdate(long startdate) { this.startdate = startdate; } public long getEnddate() { return enddate; } public void setEnddate(long enddate) { this.enddate = enddate; } private Ship ship; private int id; private AttributeName nameid; private String value; private long startdate; private long enddate; }
@Entity @Table (name="attributename") public class AttributeName { @Id @GeneratedValue(strategy= GenerationType.AUTO) public int getId() { return id; } public void setId(int id) { this.id = id; } //Name of the attribute public String getName() { return name; } public void setName(String name) { this.name = name; } @CollectionOfElements public Collection<Attribute> getAttr() { if((attr) == null) attr = new ArrayList<Attribute>(); return attr; } public void setAttr(Collection<Attribute> attr) { this.attr = attr; } private Collection<Attribute> attr; private int id; private String name; }
and I get the following exception on deploy:
Caused by: org.hibernate.MappingException: Could not determine type for: com.sms.srs.entity.AttributeName, for columns: [org.hibernate.mapping.Formula( nameid_id )] at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266) at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253) at org.hibernate.mapping.IndexedCollection.validate(IndexedCollection.java:68) at org.hibernate.cfg.Configuration.validate(Configuration.java:988) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1169) at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:414) at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:575) at org.hibernate.ejb.Ejb3Configuration.createContainerEntityManagerFactory(Ejb3Configuration.java:245) at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:108) at org.jboss.ejb3.entity.PersistenceUnitDeployment.start(PersistenceUnitDeployment.java:260)
To clarify, a ship should have a map of attributename -> attribute
The attributename class has to be used rather than a string so that key uniqueness is maintained (I tested it with String and it worked fine....it seems to be something to do with AttributeName).
I don't see where I'm missing notion, help appreciated!!
Thanks.