Hibernate映射之OneToOne(第二篇)
这是在项目中实际遇到的一个问题,纠结了很久。一开始参考mkyong的 ,两边都写OneToOne ,后来查看了一下项目经理在原来一些模块中的写法。 学习一下:
首先是E-R图: 一个货品可以进行多次调价。调价记录保存在productPrice表里面。
而Product表保存了最新一次调价的productPrice_id .第一次见这种表设计,感觉很怪。
Product类:
package net.myspring.blue.modules.crm.entity; import javax.persistence.Cacheable; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToOne; import javax.persistence.Table; import javax.persistence.Transient; import org.junit.Ignore; import net.myspring.blue.common.persistence.DataEntity; @Entity @Table(name="crm_product") @Cacheable public class Product extends DataEntity { private String name; private String namePinyin; private Brand brand; //manyToOne private ProductPrice productPrice; private Long brandId; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNamePinyin() { return namePinyin; } public void setNamePinyin(String namePinyin) { this.namePinyin = namePinyin; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "brand_id") public Brand getBrand() { return brand; } public void setBrand(Brand brand) { this.brand = brand; } @Ignore @OneToOne(fetch=FetchType.LAZY) @JoinColumn(name="product_price_id") public ProductPrice getProductPrice() { return productPrice; } public void setProductPrice(ProductPrice productPrice) { this.productPrice = productPrice; } @Transient public Long getBrandId() { if(brandId==null && brand!=null) { brandId=brand.getId(); } return brandId; } public void setBrandId(Long brandId) { this.brandId = brandId; } }
ProductPrice类:
package net.myspring.blue.modules.crm.entity; import java.math.BigDecimal; import javax.persistence.Cacheable; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import net.myspring.blue.common.persistence.DataEntity; @Entity @Table(name="crm_product_price") @Cacheable public class ProductPrice extends DataEntity{ private BigDecimal oldPrice; private BigDecimal newPrice; private Product product; //ManyToOne public ProductPrice(BigDecimal oldPrice,BigDecimal newPrice,Product product){ this.oldPrice=oldPrice; this.newPrice=newPrice; this.product=product; } public ProductPrice(){ } public BigDecimal getOldPrice() { return oldPrice; } public void setOldPrice(BigDecimal oldPrice) { this.oldPrice = oldPrice; } public BigDecimal getNewPrice() { return newPrice; } public void setNewPrice(BigDecimal newPrice) { this.newPrice = newPrice; } @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="product_id") public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; } }
注意Product和ProductPrice表之间的映射注解:
product.productPrice是这样的
@OneToOne(fetch=FetchType.LAZY) @JoinColumn(name="product_price_id") public ProductPrice getProductPrice() { return productPrice; }
而productPrice.product是这样的:
@ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="product_id") public Product getProduct() { return product; }
持久化操作:
productPriceDao.save(productPrice); //先save productPrice?有点看不懂
product.setProductPrice(productPrice);
productDao.save(product);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。