Hibernate 多对多

本文介绍了一个简单的订单模型设计,包括订单(Order)和产品(Product)两个类的定义及其相互关系。通过使用Java实现,并利用Hibernate框架进行ORM映射,实现了订单与产品的多对多关联。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
//订单
public class Order implements Serializable {

 
 private static final long serialVersionUID = 1L;
 
 private Integer id;
 private Double total;
 private String realname;
 private String address;
 private String postcode;
 private String phone;
 private Set<Product> products=new HashSet<Product>();
 
 public Order(){
  
 }

 public Integer getId() {
  return id;
 }

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

 public Double getTotal() {
  return total;
 }

 public void setTotal(Double total) {
  this.total = total;
 }

 public String getRealname() {
  return realname;
 }

 public void setRealname(String realname) {
  this.realname = realname;
 }

 public String getAddress() {
  return address;
 }

 public void setAddress(String address) {
  this.address = address;
 }

 public String getPostcode() {
  return postcode;
 }

 public void setPostcode(String postcode) {
  this.postcode = postcode;
 }

 public String getPhone() {
  return phone;
 }

 public void setPhone(String phone) {
  this.phone = phone;
 }

 public Set<Product> getProducts() {
  return products;
 }

 public void setProducts(Set<Product> products) {
  this.products = products;
 }
 

}

 

order.hbm.xml如下:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
 
 <class name="model.Order" table="orders" >
  <id name="id" type="java.lang.Integer">
   <column name="ID" precision="8"></column>
   <generator class="increment">
   </generator>
  </id>
  <property name="total" column="total" type="java.lang.Double"></property>
  <property name="realname" column="realname" type="java.lang.String"></property>
  <property name="address" column="address" type="java.lang.String"></property>
  <property name="postcode" column="postcode" type="java.lang.String"></property>
  <property name="phone" column="phone" type="java.lang.String"></property>
  <set name="products" table="orderitem" outer-join="true">
   <key column="order_id"></key>
   <many-to-many class="model.Product" column="product_id"></many-to-many>
  </set>
 </class>
</hibernate-mapping>

package model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
//产品
public class Product implements Serializable {

 
 private static final long serialVersionUID = 1L;
 
 private Integer id;
 private String  name;
 private Double  price;
 private String  description;
 private Set<Order> orders=new HashSet<Order>(0);
 
 public Product(){
  
 }

 public Integer getId() {
  return id;
 }

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

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public Double getPrice() {
  return price;
 }

 public void setPrice(Double price) {
  this.price = price;
 }

 public String getDescription() {
  return description;
 }

 public void setDescription(String description) {
  this.description = description;
 }

 public Set<Order> getOrders() {
  return orders;
 }

 public void setOrders(Set<Order> orders) {
  this.orders = orders;
 }
 
 

}

Product.hbm.xml如下:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
 
 <class name="model.Product" table="product" >
  <id name="id" type="java.lang.Integer">
   <column name="ID" precision="8"></column>
   <generator class="increment">
   </generator>
  </id>
  <property name="name" column="name" length="200"></property>
  <property name="price" column="price" precision="8" scale="2"></property>
  <property name="description" column="description" length="2000"></property>
  <set name="orders" table="orderitem" outer-join="true">
    <key column="product_id"></key>
    <many-to-many class="model.Order" column="ORDER_ID"></many-to-many>
  </set>
 </class>
</hibernate-mapping>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值