欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hibernate JPA注解一对多

程序员文章站 2022-04-23 15:41:26
...

下面演示JPA注解方式的一对多关系映射。以客户 和 订单 为例。

更多一对多的需求可以参考XML版本的一对多映射:

http://www.yiidian.com/hibernate/hibernate-one2many.html

一、实体类

Customer:

/**
 * 客户(一方)
 * @author http://www.yiidian.com
 * 
 */
@Entity
@Table(name="t_customer")
public class Customer {
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="id")
	private Integer id;
	@Column(name="name")
	private String name;
	@Column(name="gender")
	private String gender;
	
	@Transient // 临时字段,不反映到数据库中
	private Boolean isMarried;
	public Boolean getIsMarried() {
		return isMarried;
	}
	
	//关联订单
	@OneToMany(targetEntity=Order.class,mappedBy="customer")
	private Set<Order> orders = new HashSet<Order>();
	
	
	public Set<Order> getOrders() {
		return orders;
	}
	public void setOrders(Set<Order> orders) {
		this.orders = orders;
	}
	public void setIsMarried(Boolean isMarried) {
		this.isMarried = isMarried;
	}
	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 String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}

}

Order:

/**
 * 订单(多方)
 * @author http://www.yiidian.com
 *
 */
@Entity
@Table(name="t_order")
public class Order implements Serializable{

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="id")
	private Integer id;
	
	@Column(name="orderno")
	private String orderno;
	
	//关联客户
	@ManyToOne(targetEntity=Customer.class)
	@JoinColumn(name="cust_id")
	private Customer customer;

	public Integer getId() {
		return id;
	}

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

	public String getOrderno() {
		return orderno;
	}

	public void setOrderno(String orderno) {
		this.orderno = orderno;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
}

二、配置hibernate.cfg.xml

<?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>
	<!-- 1.连接数据库参数 -->
	<property name="hibernate.connection.driver_class">
		com.mysql.jdbc.Driver
	</property>
	<property name="hibernate.connection.url">
		jdbc:mysql://localhost:3306/hibernate
	</property>
	<property name="hibernate.connection.username">root</property>
	<property name="hibernate.connection.password">root</property>

	<!-- 整合c3p0 -->
	<property name="hibernate.connection.provider_class">
		org.hibernate.c3p0.internal.C3P0ConnectionProvider
	</property>
	<!-- c3p0详细配置 -->
	<property name="c3p0.min_size">10</property>
	<property name="c3p0.max_size">20</property>

	<!-- hibernate方言 -->
	<property name="hibernate.dialect">
		org.hibernate.dialect.MySQLDialect
	</property>

	<!-- 2.hibernate扩展参数 -->
	<property name="hibernate.show_sql">true</property>
	<property name="hibernate.format_sql">true</property>
	<property name="hibernate.hbm2ddl.auto">update</property>

	<!-- 让session被TheadLocal管理 -->
	<property name="hibernate.current_session_context_class">
		thread
	</property>
	<property name="dialect"></property>

	<mapping class="com.yiidian.domain.Customer" />
	<mapping class="com.yiidian.domain.Order" />

</session-factory>
</hibernate-configuration>	

三、编写测试类

/**
 * 演示一对多的映射
 * @author lenovo
 *
 */
public class Demo {

	
	@Test
	public void test1(){
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		
		Customer cust = new Customer();
		cust.setName("王五");
		session.save(cust);
	
		Order o1 = new Order();
		o1.setOrderno("201709001");
		
		Order o2 = new Order();
		o2.setOrderno("201709002");
		
		cust.getOrders().add(o1);
		cust.getOrders().add(o2);
		o1.setCustomer(cust);
		o2.setCustomer(cust);
		
		session.save(cust);
		session.save(o1);
		session.save(o2);
		
		tx.commit();
	}
	
}

执行程序,控制台输出:

Hibernate: 
    insert 
    into
        t_customer
        (gender, name) 
    values
        (?, ?)
Hibernate: 
    insert 
    into
        t_order
        (cust_id, orderno) 
    values
        (?, ?)
Hibernate: 
    insert 
    into
        t_order
        (cust_id, orderno) 
    values
        (?, ?)