hibernate多对一双向关联

一、创建实体类

package com.wr.hibernate.entity;

import java.util.HashSet;
import java.util.Set;

public class Custom {

	private int customId;
	private String customName;
	private Set<Order> orders = new HashSet<Order>();

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

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

	public int getCustomId() {
		return customId;
	}

	public void setCustomId(int customId) {
		this.customId = customId;
	}

	public String getCustomName() {
		return customName;
	}

	public void setCustomName(String customName) {
		this.customName = customName;
	}

	public Custom(int customId, String customName) {
		super();
		this.customId = customId;
		this.customName = customName;
	}

	public Custom() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public String toString() {
		return "Custom [customId=" + customId + ", customName=" + customName + "]";
	}

}

 

package com.wr.hibernate.entity;

public class Order {

	private int orderId;
	private String orderName;
	private Custom custom;

	public Custom getCustom() {
		return custom;
	}

	public void setCustom(Custom custom) {
		this.custom = custom;
	}

	public int getOrderId() {
		return orderId;
	}

	public void setOrderId(int orderId) {
		this.orderId = orderId;
	}

	public String getOrderName() {
		return orderName;
	}

	public void setOrderName(String orderName) {
		this.orderName = orderName;
	}

	@Override
	public String toString() {
		return "Order [orderId=" + orderId + ", orderName=" + orderName + ", custom=" + custom + "]";
	}

}

 

二、POJO配置文件

 

<?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 package="com.wr.hibernate.entity">

	<class name="Custom" table="custom">

		<id name="customId" type="java.lang.Integer">
			<column name="custom_id" />
			<!-- 指定主键的生成方式, native: 使用数据库本地方式 -->
			<generator class="native" />
		</id>

		<property name="customName" type="java.lang.String">
			<column name="custom_name" />
		</property>
		
		<!-- 配置多一端集合, 配置inver让多一端维护关联关系 -->
		<set name="orders" inverse="true">
			<key column="custom_id"></key>
			<one-to-many class="Order"/>
		</set>

	</class>

</hibernate-mapping>

 

package com.wr.hibernate.entity;

public class Order {

	private int orderId;
	private String orderName;
	private Custom custom;

	public Custom getCustom() {
		return custom;
	}

	public void setCustom(Custom custom) {
		this.custom = custom;
	}

	public int getOrderId() {
		return orderId;
	}

	public void setOrderId(int orderId) {
		this.orderId = orderId;
	}

	public String getOrderName() {
		return orderName;
	}

	public void setOrderName(String orderName) {
		this.orderName = orderName;
	}

	@Override
	public String toString() {
		return "Order [orderId=" + orderId + ", orderName=" + orderName + ", custom=" + custom + "]";
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值