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);
Hibernate映射之OneToOne(第二篇)的更多相关文章
- hibernate映射文件one-to-one
one-to-one 元素 属性: name:映射类属性的名字 class:映射的目标类 cascade:设置操作中的级联策略 可选值为 all所有操作情况均进行级联.none所有操作情况均不进行级联 ...
- 【SSH系列】hibernate映射 -- 一对一双向关联映射
开篇前言 上篇博文[SSH进阶之路]hibernate映射--一对一单向关联映射,小编介绍了一对一的单向关联映射,单向是指只能从人(Person)这端加载身份证端(IdCard),但是反过来,不能从身 ...
- 【SSH进阶之路】Hibernate映射——一对一单向关联映射(五)
[SSH进阶之路]Hibernate基本原理(一) ,小编介绍了Hibernate的基本原理以及它的核心,採用对象化的思维操作关系型数据库. [SSH进阶之路]Hibernate搭建开发环境+简单实例 ...
- Hibernate —— 映射关联关系
一.映射多对一关联关系. 1.单向的多对一 (1)以 Customer 和 Order 为例:一个用户可以发出多个订单,而一个订单只能属于一个客户.从 Order 到 Customer 是多对一关联关 ...
- Hibernate映射文件如何配置触发器
Hibernate映射文件之触发器生成(generated属性.database-object元素) (2013-02-27 12:28:49) 转载▼ 标签: it 分类: JAVA学习笔记 这里分 ...
- hibernate映射的 关联关系:有 一对多关联关系,一对一关联关系,多对多关联关系,继承关系
hibernate环境配置:导包.... 单向n-1:单向 n-1 关联只需从 n 的一端可以访问 1 的一端 <many-to-one> 元素来映射组成关系: name: 设定待映射的持 ...
- 第二篇 Integration Services:SSIS数据泵
本篇文章是Integration Services系列的第二篇,详细内容请参考原文. 简介SSIS用于移动数据.数据流任务提供此功能.因为这个原因,当介绍SSIS时我喜欢从数据流任务开始.数据流任务的 ...
- 第二篇 SQL Server安全验证
本篇文章是SQL Server安全系列的第二篇,详细内容请参考原文. 验证是检验主体的过程.主体需要唯一标识,那样SQL Server可以确定主体有哪些权限.正确的验证是提供安全访问数据库对象的必要的 ...
- Hibernate映射解析——七种映射关系
首先我们了解一个名词ORM,全称是(Object Relational Mapping),即对象关系映射.ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现,这样开发人员就可以把对 ...
随机推荐
- 如何用js刷新aspxgridviw
//写在js中 ASPxGridView1.Refresh();
- ArcGIS生成根据点图层生成等值面并减小栅格锯齿的操作步骤
一.打开ArcMAP并加载上相应的点图层和边界面图层 二.ArcToolbox--Spatial Analyst工具--差值分析--克里金法(根据不同的情况选择不同的算法,这次的处理实际上使用的是样条 ...
- {Reship}{ListView}C# ListView用法详解
======================================================================== This aritcle came from http ...
- codeforces 361 E - Mike and Geometry Problem
原题: Description Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him ...
- Repeater展示表格
1.可以不用table展示数据 <asp:Repeater ID="Repeater1" runat="server"> <ItemTempl ...
- php 学习使用
https://netbeans.org/kb/docs/php/wish-list-lesson1_zh_CN.html#register-mysql http://www.php100.com/h ...
- Tomcat启动过程中找不到JAVA_HOME解决方法
在XP上明明已经安装了JDK1.5并设置好了JAVA_HOME,可偏偏Tomcat在启动过程中找不到. 报错信息如下:Neither the JAVA_HOME nor the JRE_HOME en ...
- Selenium2+python自动化23-富文本(自动发帖)
前言 富文本编辑框是做web自动化最常见的场景,有很多小伙伴遇到了不知道无从下手,本篇以博客园的编辑器为例,解决如何定位富文本,输入文本内容 一.加载配置 1.打开博客园写随笔,首先需要登录,这里为了 ...
- ELK日志管理之——elasticsearch部署
1.配置官方yum源 [root@localhost ~]# rpm --import http://packages.elasticsearch.org/GPG-KEY-elasticsearch ...
- java基础之:堆排序
最近做题目饱受打击,愈发觉得打好基础的重要性,于是乎,决心把基本的排序算法还有数组操作一一实现,目的在于一方面能够得到对JAVA基础的巩固,另一面在实现的过程中发现不足. 今天所实现的堆排序(最大堆) ...