本文主要是介绍Hibernate和JPA使用连接表处理多对一映射,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在项目中,原有的持久化操作时使用JPA进行的,通过注解多对一映射被映射成中间表和两个数据库表,其代码如下:
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.NamedQueries;
- import javax.persistence.NamedQuery;
- import javax.persistence.Table;
- /**
- * 产品实体类
- * @author 李文锴
- */
- @Entity
- @Table(name = "yunda_product")
- @NamedQueries({
- @NamedQuery(name = "getProduct", query = "SELECT p FROM OrderProduct AS p"),
- @NamedQuery(name = "getProductByName", query = "SELECT p FROM OrderProduct AS p WHERE p.name=:name")
- })
- public class OrderProduct implements java.io.Serializable {
- private static final long serialVersionUID = 1L;
- /**
- * 货物id
- */
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private Long id;
- /**
- * 货物名称
- */
- @Column(name = "p_name", length = 80, nullable = false)
- private String name;
- /**
- * 货物类型
- */
- @Column(name = "p_type", length = 80, nullable = false)
- private String type;
- /**
- * 货物数量
- */
- @Column(name = "p_quantity", nullable = false)
- private int quantity;
- public OrderProduct() {
- }
- public OrderProduct(String name, String type, int quantity) {
- setName(name);
- setType(type);
- setQuantity(quantity);
- }
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getQuantity() {
- return quantity;
- }
- public void setQuantity(int quantity) {
- this.quantity = quantity;
- }
- public String getType() {
- return type;
- }
- public void setType(String type) {
- this.type = type;
- }
- @Override
- public boolean equals(Object obj) {
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- final OrderProduct other = (OrderProduct) obj;
- if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
- return false;
- }
- if ((this.type == null) ? (other.type != null) : !this.type.equals(other.type)) {
- return false;
- }
- if (this.quantity != other.quantity) {
- return false;
- }
- return true;
- }
- @Override
- public int hashCode() {
- int hash = 7;
- hash = 61 * hash + (this.name != null ? this.name.hashCode() : 0);
- hash = 61 * hash + (this.type != null ? this.type.hashCode() : 0);
- hash = 61 * hash + this.quantity;
- return hash;
- }
- }
- import java.util.Date;
- import java.util.Set;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.JoinTable;
- import javax.persistence.OneToMany;
- import javax.persistence.Table;
- import javax.persistence.Temporal;
- /**
- * 存储订单实体类,其中和产品实体进行关联
- * @author 李文锴
- */
- @Entity
- @Table(name = "stock_order")
- public class StockOrder implements java.io.Serializable {
- private static final long serialVersionUID = 1L;
- /**
- * 订单id
- */
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private Long id;
- /**
- * 客户编号
- */
- @Column(name = "customer_no", length = 20, nullable = false)
- private String customerNO;
- /**
- * 客户名称
- */
- @Column(name = "customer_name", length = 80, nullable = false)
- private String customerName;
- /**
- * 产品
- */
- @OneToMany(cascade = CascadeType.ALL)
- @JoinTable(name = "stock_order_product", joinColumns = {@JoinColumn(name = "stock_order_id")}, inverseJoinColumns = {@JoinColumn(name = "product_id")})
- private Set<OrderProduct> prdoucts;
- /**
- * 到达日期
- */
- @Column(name = "arrival_date", nullable = false)
- @Temporal(javax.persistence.TemporalType.DATE)
- private Date arrivalDate;
- public StockOrder() {
- }
- public StockOrder(String customerNO, String customerName, Set<OrderProduct> prdoucts, Date arrivalDate) {
- setCustomerNO(customerNO);
- setCustomerName(customerName);
- setPrdoucts(prdoucts);
- setArrivalDate(arrivalDate);
- }
- public Date getArrivalDate() {
- return arrivalDate;
- }
- public void setArrivalDate(Date arrivalDate) {
- this.arrivalDate = arrivalDate;
- }
- public String getCustomerNO() {
- return customerNO;
- }
- public void setCustomerNO(String customerNO) {
- this.customerNO = customerNO;
- }
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public Set<OrderProduct> getPrdoucts() {
- return prdoucts;
- }
- public void setPrdoucts(Set<OrderProduct> prdoucts) {
- this.prdoucts = prdoucts;
- }
- public String getCustomerName() {
- return customerName;
- }
- public void setCustomerName(String customerName) {
- this.customerName = customerName;
- }
- @Override
- public boolean equals(Object obj) {
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- final StockOrder other = (StockOrder) obj;
- if ((this.customerNO == null) ? (other.customerNO != null) : !this.customerNO.equals(other.customerNO)) {
- return false;
- }
- if ((this.customerName == null) ? (other.customerName != null) : !this.customerName.equals(other.customerName)) {
- return false;
- }
- if (this.prdoucts != other.prdoucts && (this.prdoucts == null || !this.prdoucts.equals(other.prdoucts))) {
- return false;
- }
- if (this.arrivalDate != other.arrivalDate && (this.arrivalDate == null || !this.arrivalDate.equals(other.arrivalDate))) {
- return false;
- }
- return true;
- }
- @Override
- public int hashCode() {
- int hash = 5;
- hash = 59 * hash + (this.customerNO != null ? this.customerNO.hashCode() : 0);
- hash = 59 * hash + (this.customerName != null ? this.customerName.hashCode() : 0);
- hash = 59 * hash + (this.prdoucts != null ? this.prdoucts.hashCode() : 0);
- hash = 59 * hash + (this.arrivalDate != null ? this.arrivalDate.hashCode() : 0);
- return hash;
- }
- }
- import java.util.Date;
- import java.util.Set;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.JoinTable;
- import javax.persistence.OneToMany;
- import javax.persistence.Table;
- import javax.persistence.Temporal;
- /**
- * 运输订单实体类,并于产品实体进行关联
- * @author 李文锴
- */
- @Entity
- @Table(name = "trans_order")
- public class TransOrder implements java.io.Serializable {
- private static final long serialVersionUID = 1L;
- /**
- * 订单id
- */
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private Long id;
- /**
- * 客户编号
- */
- @Column(name = "customer_no", length = 20, nullable = false)
- private String customerNO;
- /**
- * 客户名称
- */
- @Column(name = "customer_name", length = 80, nullable = false)
- private String customerName;
- /**
- * 产品
- */
- @OneToMany(cascade = CascadeType.ALL)
- @JoinTable(name = "trans_order_product", joinColumns = {@JoinColumn(name = "trans_order_id")}, inverseJoinColumns = {@JoinColumn(name = "product_id")})
- private Set<OrderProduct> prdoucts;
- /**
- * 其实地址
- */
- @Column(name = "start_address", length = 80, nullable = false)
- private String startAddress;
- /**
- * 目的地址
- */
- @Column(name = "end_address", length = 80, nullable = false)
- private String endAddress;
- /**
- * 运输日期
- */
- @Column(name = "trans_date", nullable = false)
- @Temporal(javax.persistence.TemporalType.DATE)
- private Date transportationDate;
- public TransOrder() {
- }
- public TransOrder(String customerNO, String customerName, Set<OrderProduct> prdoucts, String startAddress, String endAddress, Date transportationDate) {
- setCustomerNO(customerNO);
- setCustomerName(customerName);
- setPrdoucts(prdoucts);
- setStartAddress(startAddress);
- setEndAddress(endAddress);
- setTransportationDate(transportationDate);
- }
- public Date getTransportationDate() {
- return transportationDate;
- }
- public void setTransportationDate(Date transportationDate) {
- this.transportationDate = transportationDate;
- }
- public String getCustomerNO() {
- return customerNO;
- }
- public void setCustomerNO(String customerNO) {
- this.customerNO = customerNO;
- }
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public Set<OrderProduct> getPrdoucts() {
- return prdoucts;
- }
- public void setPrdoucts(Set<OrderProduct> prdoucts) {
- this.prdoucts = prdoucts;
- }
- public String getCustomerName() {
- return customerName;
- }
- public void setCustomerName(String customerName) {
- this.customerName = customerName;
- }
- public String getEndAddress() {
- return endAddress;
- }
- public void setEndAddress(String endAddress) {
- this.endAddress = endAddress;
- }
- public String getStartAddress() {
- return startAddress;
- }
- public void setStartAddress(String startAddress) {
- this.startAddress = startAddress;
- }
- @Override
- public boolean equals(Object obj) {
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- final TransOrder other = (TransOrder) obj;
- if ((this.customerNO == null) ? (other.customerNO != null) : !this.customerNO.equals(other.customerNO)) {
- return false;
- }
- if ((this.customerName == null) ? (other.customerName != null) : !this.customerName.equals(other.customerName)) {
- return false;
- }
- if (this.prdoucts != other.prdoucts && (this.prdoucts == null || !this.prdoucts.equals(other.prdoucts))) {
- return false;
- }
- if ((this.startAddress == null) ? (other.startAddress != null) : !this.startAddress.equals(other.startAddress)) {
- return false;
- }
- if ((this.endAddress == null) ? (other.endAddress != null) : !this.endAddress.equals(other.endAddress)) {
- return false;
- }
- if (this.transportationDate != other.transportationDate && (this.transportationDate == null || !this.transportationDate.equals(other.transportationDate))) {
- return false;
- }
- return true;
- }
- @Override
- public int hashCode() {
- int hash = 3;
- hash = 79 * hash + (this.customerNO != null ? this.customerNO.hashCode() : 0);
- hash = 79 * hash + (this.customerName != null ? this.customerName.hashCode() : 0);
- hash = 79 * hash + (this.prdoucts != null ? this.prdoucts.hashCode() : 0);
- hash = 79 * hash + (this.startAddress != null ? this.startAddress.hashCode() : 0);
- hash = 79 * hash + (this.endAddress != null ? this.endAddress.hashCode() : 0);
- hash = 79 * hash + (this.transportationDate != null ? this.transportationDate.hashCode() : 0);
- return hash;
- }
- }
先要使用Hibernate对其进行映射,由于产生了中间表,所以它的配置文件有些不同,如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="com.yunda.dao.domain.Product" table="yunda_product">
- <id name="id" type="long">
- <generator class="native"/>
- </id>
- <property name="name" column="p_name" type="string" length="80" not-null="true"/>
- <property name="type" column="p_type" type="string" length="80" not-null="true"/>
- <property name="quantity" column="p_quantity" type="integer" length="80" not-null="true"/>
- <!-- 使用join来配置多对一的连接,以table属性来表示连接表的多对一,连接表为stock_order_id -->
- <!-- optional属性表示这是一个外连接,inverse属性可以出现在一端和多端,这里选择出现在多端,效果相同 -->
- <join table="stock_order_product" optional="true" inverse="true">
- <!-- 该key的字段为连接表中的字段,作为外键 -->
- <key column="product_id" />
- <!-- 连接表中配置多对一,对应的字段为stock_order_id -->
- <many-to-one name="stockOrder" column="stock_order_id" not-null="true" />
- </join>
- <!-- 使用join来配置多对一的连接,以table属性来表示连接表的多对一,连接表为trans_order_id -->
- <!-- optional属性表示这是一个外连接,inverse属性可以出现在一端和多端,这里选择出现在多端,效果相同 -->
- <join table="trans_order_product" optional="true" inverse="true">
- <!-- 该key的字段为连接表中的字段,作为外键 -->
- <key column="product_id" />
- <!-- 连接表中配置多对一,对应的字段为trans_order_id -->
- <many-to-one name="transOrder" column="trans_order_id" not-null="true" />
- </join>
- </class>
- </hibernate-mapping>
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="com.yunda.dao.domain.StockOrder" table="stock_order">
- <id name="id" type="long">
- <generator class="native"/>
- </id>
- <property name="customerNO" column="customer_no" type="string" length="20" not-null="true"/>
- <property name="customerName" column="customer_name" type="string" length="80" not-null="true"/>
- <property name="arrivalDate" column="arrival_date" type="date" not-null="true"/>
- <!-- 通过连接表的一端,因此需要table属性为stock_order_product -->
- <set name="prdoucts" cascade="all" table="stock_order_product" lazy="false">
- <!-- 该key的字段为连接表中的字段,作为外键 -->
- <key column="stock_order_id" />
- <!-- 配置多对多 -->
- <!-- 但是unique属性为true,表示product_id字段为不可重复,保证一对多关系 -->
- <many-to-many class="com.yunda.dao.domain.Product" column="product_id" unique="true" />
- </set>
- </class>
- </hibernate-mapping>
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="com.yunda.dao.domain.TransOrder" table="trans_order">
- <id name="id" type="long">
- <generator class="native"/>
- </id>
- <property name="customerNO" column="customer_no" type="string" length="20" not-null="true"/>
- <property name="customerName" column="customer_name" type="string" length="80" not-null="true"/>
- <property name="startAddress" column="start_address" type="string" length="80" not-null="true"/>
- <property name="endAddress" column="end_address" type="string" length="80" not-null="true"/>
- <property name="transportationDate" column="trans_date" type="date" not-null="true"/>
- <!-- 通过连接表的一端,因此需要table属性为stock_order_product -->
- <set name="prdoucts" cascade="all" table="trans_order_product" lazy="false">
- <!-- 该key的字段为连接表中的字段,作为外键 -->
- <key column="trans_order_id" />
- <!-- 配置多对多 -->
- <!-- 但是unique属性为true,表示product_id字段为不可重复,保证一对多关系 -->
- <many-to-many class="com.yunda.dao.domain.Product" column="product_id" unique="true" />
- </set>
- </class>
- </hibernate-mapping>
这篇关于Hibernate和JPA使用连接表处理多对一映射的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!