我是 Hiberante 3 的新手。当我尝试使用以下关系映射三个表时遇到问题:
ITEM_MASTER (Table):
ITEM_ID-PK
LOCATION_ID
ITEM_MASTER_ATTR (Table):
ITEM_ID - FK
ITEM_ATTR_ID - FK
ITEM_ATTR_VALUE - STRING
ITEM_ATTR (Table):
ITEM_ATTR_ID - PK
ITEM_ATTR_NAME - STRING
每个都可以在表Item
中具有多个属性。Item_master_attr
每个Item Attribute name
都将存储在Item_Attr
表中。
详细说明:
Item 1 ----> Attr 1 -----> Attr Name 1
Item 1 ----> Attr 2 -----> Attr Name 2
Item 2 ----> Attr 1 -----> Attr Name 1
任何帮助都非常可观。
我现在以相同的方式创建了这个类,但不清楚在执行创建操作时如何设置数据。
@Entity
@Table(name = "item_attributes")
public class ItemAttributeDTO implements Serializable {
/**
*
*/
private static final long serialVersionUID = 4678662910814121175L;
private int itemAttributeID;
private String itemAttrName;
@OneToMany(mappedBy = "itemAttributeMapDTO")
private Collection<ItemAttributeMapDTO> itemAttributeMap = new ArrayList<ItemAttributeMapDTO>();
@Id
@GeneratedValue
@Column(name = "ITEM_ATTR_ID", unique = true, nullable = false)
public int getItemAttributeID() {
return itemAttributeID;
}
public void setItemAttributeID(int itemAttributeID) {
this.itemAttributeID = itemAttributeID;
}
@Column(name = "ATTR_NAME")
public String getItemAttrName() {
return itemAttrName;
}
public void setItemAttrName(String itemAttrName) {
this.itemAttrName = itemAttrName;
}
public Collection<ItemAttributeMapDTO> getItemAttributeMap() {
return itemAttributeMap;
}
public void setItemAttributeMap(
Collection<ItemAttributeMapDTO> itemAttributeMap) {
this.itemAttributeMap = itemAttributeMap;
}
}
@Entity
@Table(name = "item_master")
public class ItemMasterDTO implements Serializable {
/**
*
*/
private static final long serialVersionUID = 4678662910814121175L;
private int itemID;
private int locationId;
@OneToMany(mappedBy = "itemMasterDTO", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Collection<ItemAttributeDTO> itemAttributeDTO = new ArrayList<ItemAttributeDTO>();
@Id
@Column(name = "ITEM_ID", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
public int getItemID() {
return itemID;
}
public void setItemID(int itemID) {
this.itemID = itemID;
}
@Column(name = "LOCATION_ID")
public int getLocationId() {
return locationId;
}
public void setLocationId(int locationId) {
this.locationId = locationId;
}
public Collection<ItemAttributeDTO> getItemAttributeDTO() {
return itemAttributeDTO;
}
public void setItemAttributeDTO(
Collection<ItemAttributeDTO> itemAttributeDTO) {
this.itemAttributeDTO = itemAttributeDTO;
}
}
@Entity
@Table(name = "ITEM_MASTER_ATTRIBUTES")
public class ItemAttributeMapDTO {
@ManyToOne
@JoinColumn(name = "ITEM_ID")
private ItemMasterDTO itemMasterDTO;
@ManyToOne
@JoinColumn(name = "ITEM_ATTR_ID")
private ItemAttributeDTO itemAttributeDTO;
private int item_master_id;
public ItemMasterDTO getItemMasterDTO() {
return itemMasterDTO;
}
public void setItemMasterDTO(ItemMasterDTO itemMasterDTO) {
this.itemMasterDTO = itemMasterDTO;
}
public ItemAttributeDTO getItemAttributeDTO() {
return itemAttributeDTO;
}
public void setItemAttributeDTO(ItemAttributeDTO itemAttributeDTO) {
this.itemAttributeDTO = itemAttributeDTO;
}
@Id
@Column(name = "ITEM_MASTER_ATTRIBUTE_ID", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
public int getItem_master_id() {
return item_master_id;
}
public void setItem_master_id(int item_master_id) {
this.item_master_id = item_master_id;
}
}
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
//Set the Item Master
ItemMasterDTO itmDto = new ItemMasterDTO();
itmDto.setLocationId(100);
//Set the Item Attributes
ItemAttributeDTO itmAttDto = new ItemAttributeDTO();
????.......