hibernate如何决定更新/保存关联对象

时间:2014-08-18 10:35:20

标签: java hibernate jpa

任何人都可以帮我解释一下hibernate。保存/更新父对象时如何保存/更新子对象。特别是,带@EmbeddedId的子对象看起来像我的映射

@Embeddable
public class BomLevelKey implements Serializable{
/**
 * 
 */
private static final long serialVersionUID = 1L;
Item item;
long organizationId;
Item componentItem;

@Column(name = "organization_id")
public long getOrganizationId() {
    return organizationId;
}
public void setOrganizationId(long organizationId) {
    this.organizationId = organizationId;
}

@ManyToOne
@JoinColumn(name = "inventory_item_id")
public Item getItem() {
    return item;
}
public void setItem(Item item) {
    this.item = item;
}

@ManyToOne
@JoinColumn(name = "component_item_id")
public Item getComponentItem() {
    return componentItem;
}
public void setComponentItem(Item componentItem) {
    this.componentItem = componentItem;
}

@Override
public boolean equals(Object obj) {
    if (obj == null || !(obj instanceof BomLevelKey)) {
        return false;
    }
    BomLevelKey key = (BomLevelKey) obj;
    if (key.getItem().getInventoryItemID() == this.getItem()
            .getInventoryItemID()
            && key.getComponentItem().getInventoryItemID() == this
                    .getComponentItem().getInventoryItemID()
            && key.getOrganizationId() == this.getOrganizationId()) {
        return true;
    }
    return false;
}
}

@Entity
@Table(name = "es_bom_levels", schema = "xx_vms")
public class ItemConversionAttributes implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Double componentRoutingYield;
    private Double forwardRoutingYield;
    private Double conversionFactor;
    private Double cumulativeLeadTime;
    private Integer organizationID;
    private Item componentItem;
    private BomLevelKey id;

@EmbeddedId
public BomLevelKey getId() {
    return id;
}

public void setId(BomLevelKey id) {
    this.id = id;
}

/**
* @hibernate.property 
* column="component_routing_yield"
*
**/
@Column(name="component_routing_yield")
public Double getComponentRoutingYield(){ return componentRoutingYield; }

public void setComponentRoutingYield(Double componentRoutingYield){ this.componentRoutingYield= componentRoutingYield; }

/**
* @hibernate.property 
* column="forward_routing_yield"
*
**/
@Column(name="forward_routing_yield")
public Double getForwardRoutingYield(){ return forwardRoutingYield; }

public void setForwardRoutingYield(Double forwardRoutingYield){ this.forwardRoutingYield = forwardRoutingYield; }

/**
* @hibernate.property 
* column="conversion_factor"
*
**/
@Column(name="conversion_factor")
public Double getConversionFactor(){ return conversionFactor; }

public void setConversionFactor(Double conversionFactor){ this.conversionFactor = conversionFactor; }

/**
* @hibernate.property 
* column="cumulative_lead_time"
*
**/
@Column(name="cumulative_lead_time")
public Double getCumulativeLeadTime(){ return cumulativeLeadTime; }

public void setCumulativeLeadTime(Double cumulativeLeadTime){ this.cumulativeLeadTime = cumulativeLeadTime; }

/**
 * @hibernate.property
 * column="organization_id"
 *
 **/
@Column(name="organization_id", insertable = false, updatable = false)
public Integer getOrganizationID(){ return organizationID; }

public void setOrganizationID(Integer organizationID){ this.organizationID = organizationID; }

/**
* @hibernate.many-to-one
* column="component_item_id"
* insert="false"
* update="false"
*
**/
@ManyToOne(targetEntity=Item.class)
@JoinColumn(name="component_item_id", insertable=false, updatable=false)
@NotFound(action=NotFoundAction.IGNORE)
public Item getComponentItem(){ return componentItem; }

public void setComponentItem(Item componentItem){ this.componentItem = componentItem; }

}

在课程项目中,我有这个映射:

@OneToMany(mappedBy = "id.item")
@MapKey(name = "id.componentItem")
public Map<Item, ItemConversionAttributes> getConversionAttributes(){ return conversionAttributes; }

public void setConversionAttributes(Map<Item, ItemConversionAttributes> conversionAttributes) {
    this.conversionAttributes = conversionAttributes;
}

保存/更新不执行任何操作。

1 个答案:

答案 0 :(得分:0)

它基于DAO Objects主键的值。

如果设置了默认值,hibernate将添加否则更新