一个顾客,可以有多个订单
order
package cn.hgu.domain;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="t_order",catalog = "student")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private Double money;
private String address;
@ManyToOne(targetEntity = Custom.class,cascade = CascadeType.ALL)
@JoinColumn(name="c_cus_id")//外建
private Custom c;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Custom getC() {
return c;
}
public void setC(Custom c) {
this.c = c;
}
}
custom
package cn.hgu.domain;
import java.util.HashSet;
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.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="t_customer",catalog = "student")
public class Custom {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name="c_name",length = 30, nullable = true)
private String name;
@OneToMany(targetEntity = Order.class,cascade = CascadeType.ALL)
private Set<Order> orders=new HashSet<Order>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Order> getOrders() {
return orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
}
test
package cn.hgu.domain;
import java.util.Date;
import org.hibernate.Session;
import cn.hgu.util.HbnUtil;
public class Test2 {
public static void main(String[] args) {
Session session = HbnUtil.getSession();
try{
session.beginTransaction();
//建立客户
Custom c=new Custom();
c.setName("zhang");
//建立两个订单
Order or1=new Order();
Order or2=new Order();
or1.setAddress("111");
or1.setMoney(12.3);
or2.setAddress("222");
or2.setMoney(13.5);
//客户关联订单
c.getOrders().add(or1);
c.getOrders().add(or2);
//订单关联客户
or1.setC(c);
or2.setC(c);
//1保存客户
session.save(c);
// //2保存客户
// session.save(or1);
// session.save(or2);
//
session.getTransaction().commit();
}catch(Exception ex){
ex.printStackTrace();
session.getTransaction().rollback();
}
}
}
hibernate.cfg中
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 连接四要素 -->
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">12345678</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="cn.hgu.domain.Student"/>
<mapping class="cn.hgu.domain.Teacher"/>
<mapping class="cn.hgu.domain.Custom"/>
<mapping class="cn.hgu.domain.Order"/>
</session-factory>
</hibernate-configuration>