hibernate-1对多-customer和order-demo基于注解

一个顾客,可以有多个订单
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&amp;serverTimezone=UTC&amp;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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值