0

我是 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();

            ????.......
4

1 回答 1

0

下面是一个例子,如果你使用 hibernate 注释,你的类会是什么样子:

ITEM_MASTER_ATT 类 - 基于外键注释 ManyToOne

@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>();

}

ITEM_ATTR 类使用 mappedBy 字段根据逆向注释 OneToMany。

@Entity
public class ITEM_ATTR {

    @OneToMany(mappedBy = "itemAttr")
    private Collection<ITEM_MASTER_ATTR> masterAttr = new    ArrayList<ITEM_MASTER_ATTR>();

}
于 2013-06-20T19:37:47.057 回答