How do map one-to-many properly?
andyleung May 13, 2006 10:17 PMI have a scenario, which is very easy: Gender 1 --- * Singer
So in Singer class, I have a Gender object where in Gender class, I have list of Singer objects. Here are the source codes:
public class Gender implements Serializable { private long id; private String type; private String description; private List singer; public Gender() { super(); // TODO Auto-generated constructor stub } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getType() { return type; } public void setType(String type) { this.type = type; } public List getSinger() { return singer; } public void setSinger(List singer) { this.singer = singer; } }
public class Singer implements Serializable { private long id; private String firstname; private String lastname; private Gender gender; public Singer() { super(); // TODO Auto-generated constructor stub } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public Gender getGender() { return gender; } public void setGender(Gender gender) { this.gender = gender; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } }
These are their mapping files:
<hibernate-mapping> <class name="net.andyleung.multimedia.mp3.entity.Gender" table="gender"> <id name="id" column="id"> <generator class="native"/> </id> <property name="description" column="description" /> <property name="type" column="type" /> <list name="singer"> <key column="id" not-null="true"/> <list-index column="gender"/> <one-to-many class="Singer"/> </list> </class> </hibernate-mapping>
<hibernate-mapping> <class name="net.andyleung.multimedia.mp3.entity.Singer" table="singer"> <id name="id" column="id"> <generator class="native"/> </id> <property name="firstname"/> <property name="lastname"/> <many-to-one name="gender" class="Gender" column="gender" not-null="true" update="false" insert="false"/> <!-- <many-to-one name="country" class="Country" column="country" not-null="true" update="false" insert="false"/> <list name="album"> <key column="singer" not-null="true"/> <list-index column="singer"/> <one-to-many class="Album"/> </list>--> </class> </hibernate-mapping>
But somehow it says my Singer is mapping incorrectly. Any clue?
Thanks.