In Hibernate when you want to have a collection representing one-to-many relationship by default Hibernate maps primary key-foreign key mapping for the collection.
In case you want to map an non-foreign key field you could do it by using 'property-ref' attribute of <key> element.
Example:
<class name="com.sample.Item" table="ITEM"
dynamic-update="false" dynamic-insert="false" select-before-update="false">
<id name="id" type="int" column="ID">
<generator class="native">
</generator>
</id>
<property name="itemId" >
<column name="ITEM_ID" />
</property>
<property name="name" >
<column name="NAME" />
</property>
<bag name="bidList" lazy="false" order-by="BID_ID" >
<key column = "BID_ID" property-ref="bidId"></key>
<one-to-many class="com.example.Bid"/>
</bag>
</class>
public class Item implements Serializable{
private static final long serialVersionUID = 8991813750127304998L;
private int id;
private int itemId;
private String name;
private Collection<Bid> bidList = new ArrayList<Bid>();
public void setId(int id){
this.id = id;
}
public String getId(){
return id;
}
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public Collection<Bid> getBidList() {
return bidList;
}
public void setBidList(Collection<Bid> bidList) {
this.bidList = bidList;
}
}
public class Bid implements Serializable {
private static final long serialVersionUID = 7991813750127304558L;
private int id;
private int bidId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getBidId() {
return bidId;
}
public void setBidId(int bidId) {
this.bidId = bidId;
}
}
In case you want to map an non-foreign key field you could do it by using 'property-ref' attribute of <key> element.
Example:
<class name="com.sample.Item" table="ITEM"
dynamic-update="false" dynamic-insert="false" select-before-update="false">
<id name="id" type="int" column="ID">
<generator class="native">
</generator>
</id>
<property name="itemId" >
<column name="ITEM_ID" />
</property>
<property name="name" >
<column name="NAME" />
</property>
<bag name="bidList" lazy="false" order-by="BID_ID" >
<key column = "BID_ID" property-ref="bidId"></key>
<one-to-many class="com.example.Bid"/>
</bag>
</class>
public class Item implements Serializable{
private static final long serialVersionUID = 8991813750127304998L;
private int id;
private int itemId;
private String name;
private Collection<Bid> bidList = new ArrayList<Bid>();
public void setId(int id){
this.id = id;
}
public String getId(){
return id;
}
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public Collection<Bid> getBidList() {
return bidList;
}
public void setBidList(Collection<Bid> bidList) {
this.bidList = bidList;
}
}
public class Bid implements Serializable {
private static final long serialVersionUID = 7991813750127304558L;
private int id;
private int bidId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getBidId() {
return bidId;
}
public void setBidId(int bidId) {
this.bidId = bidId;
}
}
No comments:
Post a Comment