`

hibernate 外键做联合主键

 
阅读更多

使用Hibernate的外键作为联合主键:

如:有三张表

product:其字段有 id,proColor,proName。id是主键

orders:字段有 id,amount,date。id是主键

order_details:字段有 proId,orderId,quantity,price。proId和orderId为联合主键,并且proId是product表的外键,orderId是orders表的外键

 

CREATE TABLE `product` (
  `id` int(11) NOT NULL,
  `proColor` varchar(255) DEFAULT NULL,
  `proName` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) 

CREATE TABLE `orders` (
  `id` int(11) NOT NULL,
  `amount` int(11) DEFAULT NULL,
  `date` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
)



CREATE TABLE `order_details` (
  `price` double DEFAULT NULL,
  `quantity` int(11) DEFAULT NULL,
  `orderId` int(11) NOT NULL DEFAULT '0',
  `proId` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`orderId`,`proId`),
  CONSTRAINT `FK_q2kymfpksk4xk8etdur1cv3kj` FOREIGN KEY (`proId`) REFERENCES `product` (`id`),
  CONSTRAINT `FK_msk76lwy2lb5nj4w78nfjhye6` FOREIGN KEY (`orderId`) REFERENCES `orders` (`id`)
)

 一: 使用@onetoone注解

使用hibernate配置:

1、product实体:

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Product {
	private Integer id;
	private String proName;
	private String proColor;
	
	@Id
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getProName() {
		return proName;
	}
	public void setProName(String proName) {
		this.proName = proName;
	}
	public String getProColor() {
		return proColor;
	}
	public void setProColor(String proColor) {
		this.proColor = proColor;
	}
	
}

  

2、orders实体:

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Orders {
	private Integer Id;
	private Integer amount;
	private Date date;
	@Id
	public Integer getId() {
		return Id;
	}
	public void setId(Integer id) {
		Id = id;
	}
	public Integer getAmount() {
		return amount;
	}
	public void setAmount(Integer amount) {
		this.amount = amount;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
}

 

3、order_details实体:

import javax.persistence.Entity;
import javax.persistence.Id;


@Entity
public class Order_Details {
	private Order_Pro_PK id;
	private Integer quantity;
	private Double price;
	
	@Id
	public Order_Pro_PK getId() {
		return id;
	}
	public void setId(Order_Pro_PK id) {
		this.id = id;
	}
	public Integer getQuantity() {
		return quantity;
	}
	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
	
	
	
}

 

4、因为在order_details表中的id,是联合主键,所以有了:

import java.io.Serializable;

import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

@Embeddable  
public class Order_Pro_PK implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private Product product ;
	private Orders order ;
	
	@OneToOne
	@JoinColumn(name="orderId")
	public Orders getOrder() {
		return order;
	}
	public void setOrder(Orders order) {
		this.order = order;
	}
	
	@OneToOne
	@JoinColumn(name="proId")
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	
	@Override
	public int hashCode() {
		return super.hashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Order_Pro_PK){
			Order_Pro_PK cpk = (Order_Pro_PK) obj;
			if(cpk.getOrder().equals(this.getOrder()) && cpk.getProduct().equals(this.getProduct()) ){
				return true;
			}
		}
		return false;
	}
	
	
	
}

 

二、使用@manytoone注解

1、order实体

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Orders {
	private Integer Id;
	private Integer amount;
	private Date date;
	@Id
	public Integer getId() {
		return Id;
	}
	public void setId(Integer id) {
		Id = id;
	}
	public Integer getAmount() {
		return amount;
	}
	public void setAmount(Integer amount) {
		this.amount = amount;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	
	
	
	
}

2、product实体

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Product {
	private Integer id;
	private String proName;
	private String proColor;
	
	@Id
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getProName() {
		return proName;
	}
	public void setProName(String proName) {
		this.proName = proName;
	}
	public String getProColor() {
		return proColor;
	}
	public void setProColor(String proColor) {
		this.proColor = proColor;
	}
	
	
}

  

3、order_details实体

import javax.persistence.Entity;
import javax.persistence.Id;


@Entity
public class Order_Details {
	private Order_Pro_PK id;
	private Integer quantity;
	private Double price;
	
	@Id
	public Order_Pro_PK getId() {
		return id;
	}
	public void setId(Order_Pro_PK id) {
		this.id = id;
	}
	public Integer getQuantity() {
		return quantity;
	}
	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
	
	
	
}

 

4、联合主键

import java.io.Serializable;

import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Embeddable  
public class Order_Pro_PK implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private Product product ;
	private Orders order ;
	
	@ManyToOne
	@JoinColumn(name="orderId")
	public Orders getOrder() {
		return order;
	}
	public void setOrder(Orders order) {
		this.order = order;
	}
	
	@ManyToOne
	@JoinColumn(name="proId")
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	
	@Override
	public int hashCode() {
		return super.hashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Order_Pro_PK){
			Order_Pro_PK cpk = (Order_Pro_PK) obj;
			if(cpk.getOrder().equals(this.getOrder()) && cpk.getProduct().equals(this.getProduct()) ){
				return true;
			}
		}
		return false;
	}
	
	
	
}

 

 

其中在Order_Pro_PK类要重写equals和hashCode方法 

 

 

分享到:
评论

相关推荐

    Hibernate一对一单向外键关联 (联合主键annotation)

    NULL 博文链接:https://cdxs2.iteye.com/blog/1932507

    Hibernate注解

    * 9.increnment 插入数据的时候hibernate会给主键添加一个自增的主键,但是一个hibernate实例就维护一个计数器,所以在多个实例运行的时候不能使用这个方法。 * 例:@GeneratedValue(generator = ...

    hibernate学习笔记

    复合(联合)主键映射(hibernate_composite) 27 集合(collection)映像 (hibernate_collection) 28 Hibernate 对数据库的并发支持 30 悲观锁(hibernate_pessimistic) 30 乐观锁(hibernate_optimistic) 32 HQL...

    Hibernate注释大全收藏

    Hibernate注释大全收藏 声明实体Bean @Entity public class Flight implements Serializable { Long id; @Id public Long getId() { return id; } public void setId(Long id) { this.id = id; } } @Entity ...

    Hibernate学习笔记_songjignhao_1

    包括:一对多双向关联关系、一对多双向自身关联关系、一对一关联关系(主键关联、外键关联)、一对多与一对一结合、多对多关联关系、Map、Set、List与Bag映射关系、查询排序(内存排序和数据库排序)、联合主键的映射...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part2

     15.1.1 按照外键映射   15.1.2 按照主键映射  15.2 映射单向多对多关联  15.3 映射双向多对多关联关系  15.3.1 关联两端使用元素  15.3.2 在inverse端使用元素  15.3.3 使用组件类集合  15.3.4 把多对多...

    Hibernate实战(第2版 中文高清版)

     7.1.2 一对一的外键关联   7.1.3 用联结表映射   7.2 多值的实体关联   7.2.1 一对多关联   7.2.2 多对多关联   7.2.3 把列添加到联结表   7.2.4 映射map   7.3 多态关联   7.3.1 多态的多对一...

    Hibernate 中文 html 帮助文档

    6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. 索引集合类(Indexed collections) 6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) ...

    最全Hibernate 参考文档

    6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. 索引集合类(Indexed collections) 6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) ...

    hibernate 体系结构与配置 参考文档(html)

    触发器实现的主键生成器(Primary keys assigned by triggers) 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对...

    Hibernate教程

    7.2.1. 集合外键(Collection foreign keys) 7.2.2. 集合元素(Collection elements) 7.2.3. 索引集合类(Indexed collections) 7.2.4. 值集合于多对多关联(Collections of values and many-to-many associations)...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part4

     15.1.1 按照外键映射   15.1.2 按照主键映射  15.2 映射单向多对多关联  15.3 映射双向多对多关联关系  15.3.1 关联两端使用元素  15.3.2 在inverse端使用元素  15.3.3 使用组件类集合  15.3.4 把多对多...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part3

     15.1.1 按照外键映射   15.1.2 按照主键映射  15.2 映射单向多对多关联  15.3 映射双向多对多关联关系  15.3.1 关联两端使用元素  15.3.2 在inverse端使用元素  15.3.3 使用组件类集合  15.3.4 把多对多...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part1.rar

     15.1.1 按照外键映射   15.1.2 按照主键映射  15.2 映射单向多对多关联  15.3 映射双向多对多关联关系  15.3.1 关联两端使用元素  15.3.2 在inverse端使用元素  15.3.3 使用组件类集合  15.3.4 把多对多...

    Hibernate3的帮助文档

    7.2.1. 集合外键(Collection foreign keys) 7.2.2. 集合元素(Collection elements) 7.2.3. 索引集合类(Indexed collections) 7.2.4. 值集合于多对多关联(Collections of values and many-to-many associations)...

    oracle 数据按主键删除慢问题的解决方法

    需求是删除一个主表A,另有两个附表建有此表的主键ID的外键。删除A表的数据级联删除另两个表的关联数据。增删改查使用hibernate实现。 一开始一直以为是hibernate的内部处理上有关联操作导致的删除和更新数据缓慢。...

    hibernate3.04中文文档.chm

    7.2.1. 集合外键(Collection foreign keys) 7.2.2. 集合元素(Collection elements) 7.2.3. 索引集合类(Indexed collections) 7.2.4. 值集合于多对多关联(Collections of values and many-to-many associations)...

    Hibernate3+中文参考文档

    6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. 索引集合类(Indexed collections) 6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) ...

    hibernate 框架详解

    触发器实现的主键生成器(Primary keys assigned by triggers) 6.1.5. composite-id 6.1.6. 鉴别器(discriminator) 6.1.7. 版本(version)(可选) 6.1.8. timestamp (optional) 6.1.9. property 6.1.10. ...

Global site tag (gtag.js) - Google Analytics