Hibernate3注释映射问题
我不熟悉Hibernate 3。当我试图绘制三个具有以下关系的表时,我遇到了一些问题:Hibernate3注释映射问题,hibernate,hibernate-mapping,Hibernate,Hibernate Mapping,我不熟悉Hibernate 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 每个项目可以在项目_master
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
每个项目
可以在项目_master_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();
????.......
@实体
@表(name=“项目属性”)
实现可序列化的公共类ItemAttributed{
/**
*
*/
私有静态最终长serialVersionUID=4678662910814121175L;
私有int-itemAttributeID;
私有字符串itemAttrName;
@OneToMany(mappedBy=“itemAttributeMapTo”)
private Collection itemAttributeMap=new ArrayList();
@身份证
@生成值
@列(name=“ITEM\u ATTR\u ID”,unique=true,nullable=false)
public int getItemAttributeID(){
返回itemAttributeID;
}
public void setItemAttributeID(int-itemAttributeID){
this.itemAttributeID=itemAttributeID;
}
@列(name=“ATTR\u name”)
公共字符串getItemAttrName(){
返回itemAttrName;
}
public void setItemAttrName(字符串itemAttrName){
this.itemAttrName=itemAttrName;
}
公共集合getItemAttributeMap(){
返回itemAttributeMap;
}
公共void setItemAttributeMap(
集合项属性映射){
this.itemAttributeMap=itemAttributeMap;
}
}
@实体
@表(name=“项目\主控”)
公共类ItemMasterDTO实现可序列化{
/**
*
*/
私有静态最终长serialVersionUID=4678662910814121175L;
私有int-itemID;
私有地址ID;
@OneToMany(mappedBy=“itemMasterDTO”,cascade=CascadeType.ALL,fetch=FetchType.EAGER)
private Collection itemAttributeDTO=new ArrayList();
@身份证
@列(name=“ITEM_ID”,unique=true,nullable=false)
@GeneratedValue(策略=GenerationType.AUTO)
public int getItemID(){
返回itemID;
}
public void setItemID(int itemID){
this.itemID=itemID;
}
@列(name=“LOCATION\u ID”)
public int getLocationId(){
返回位置ID;
}
public void setLocationId(int locationId){
this.locationId=locationId;
}
公共集合GetItemAttributeTo(){
返回项目AttributeTo;
}
public void setItemAttributeDTO(
集合项属性(属性为){
this.itemAttributeDTO=itemAttributeDTO;
}
}
@实体
@表(name=“物料主属性”)
公共类ItemAttributeMapDTO{
@许多酮
@JoinColumn(name=“ITEM\u ID”)
私人ItemMasterDTO ItemMasterDTO;
@许多酮
@JoinColumn(name=“ITEM\u ATTR\u ID”)
私有项attributedto项attributedto;
专用int项目\u主\u id;
public itemsmasterdto getitemsmasterdto(){
返回itemMasterDTO;
}
公共无效设置ItemMasterDTO(ItemMasterDTO ItemMasterDTO){
this.itemMasterDTO=itemMasterDTO;
}
public ItemAttributeDTO getItemAttributeDTO(){
返回项目AttributeTo;
}
public void setItemAttributeDTO(ItemAttributeDTO ItemAttributeDTO){
this.itemAttributeDTO=itemAttributeDTO;
}
@身份证
@列(name=“ITEM\u MASTER\u ATTRIBUTE\u ID”,unique=true,nullable=false)
@GeneratedValue(策略=GenerationType.AUTO)
public int getItem_master_id(){
返回项目\u主\u id;
}
公共无效设置项主项id(内部项主项id){
this.item\u master\u id=item\u master\u id;
}
}
Session Session=HibernateUtil.getSessionFactory().openSession();
事务tx=会话.beginTransaction();
//设置项目主控形状
ItemMasterDTO itmDto=新的ItemMasterDTO();
itmDto.setLocationId(100);
//设置项目属性
ItemAttributeDTO itmAttDto=新的ItemAttributeDTO();
????.......
下面是一个示例,说明了如果使用hibernate注释,类的外观:
ITEM\u MASTER\u ATT类-在外键中注释一个多音
@Entity
public class ITEM_MASTER_ATTR {
@ManyToOne
@JoinColumn(name = "ITEM_ID")
private ITEM_MASTER itemMaster;
@ManyToOne
@JoinColumn(name = "ITEM_ATTR_ID")
private ITEM_ATTR itemAttr;
}
ITEM_MASTER类-使用mappedBy字段以相反的形式注释一个OneToMany
@Entity
public class ITEM_MASTER {
@OneToMany(mappedBy = "itemMaster")
private Collection<ITEM_MASTER_ATTR> masterAttr = new ArrayList<ITEM_MASTER_ATTR>();
}
@Entity
public class ITEM_ATTR {
@OneToMany(mappedBy = "itemAttr")
private Collection<ITEM_MASTER_ATTR> masterAttr = new ArrayList<ITEM_MASTER_ATTR>();
}
@实体
公共类项目管理器{
@OneToMany(mappedBy=“itemMaster”)
私有集合masterAttr=new ArrayList();
}
ITEM_ATTR类使用mappedBy字段以相反的形式注释一个OneToMany
@Entity
public class ITEM_MASTER {
@OneToMany(mappedBy = "itemMaster")
private Collection<ITEM_MASTER_ATTR> masterAttr = new ArrayList<ITEM_MASTER_ATTR>();
}
@Entity
public class ITEM_ATTR {
@OneToMany(mappedBy = "itemAttr")
private Collection<ITEM_MASTER_ATTR> masterAttr = new ArrayList<ITEM_MASTER_ATTR>();
}
@实体
公共类项目属性{
@OneToMany(mappedBy=“itemAttr”)
私有集合masterAttr=new ArrayList();
}
检查onetomany注释onetomany我们可以映射2个表。但是如何将项目\u master\u attr映射回项目\u attr。你能说得更具体些吗?我只是发布了一个答案,检查这是否能让你保持发布一个答案,检查这是否能帮助你。您现在可以帮助我在创建操作时如何设置数据吗。我将有预定义的数据项\u ATTR表。因此,在创建操作期间,我需要在主表中插入1条记录,并需要根据Attr名称提取Item_Attr Id,然后需要在映射表中使用多个Attr映射该项。希望你现在明白了。