Hibernate的学习之路十九(一对多的JavaBean编写和映射的配置文件的编写)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a4171175/article/details/78700868

前言

     Hibernate属于ORM框架,它基本不适用sql语句,但是设计到多表查询的时候,就有些麻烦了。
     当一对多的时候,一是集合,多是对象。
     一里是多的对象集合,多里是一的对象。


分析


以下是一的JavaBean代码,记住千万不要new。Hibernate会通过映射自动实例化,如果new了,那么一直都是一个对象,会出问题。

package com.itheima.domain;

/**
 * 客户的联系人
 * 多方
 * @author Administrator
 */
public class Linkman {
	
	private Long lkm_id;
	private String lkm_name;
	private String lkm_gender;
	private String lkm_phone;
	private String lkm_mobile;
	private String lkm_email;
	private String lkm_qq;
	private String lkm_position;
	private String lkm_memo;
	
	// 编写一个对象,不要自己new
	private Customer customer;
	
	public Long getLkm_id() {
		return lkm_id;
	}
	public void setLkm_id(Long lkm_id) {
		this.lkm_id = lkm_id;
	}
	public String getLkm_name() {
		return lkm_name;
	}
	public void setLkm_name(String lkm_name) {
		this.lkm_name = lkm_name;
	}
	public String getLkm_gender() {
		return lkm_gender;
	}
	public void setLkm_gender(String lkm_gender) {
		this.lkm_gender = lkm_gender;
	}
	public String getLkm_phone() {
		return lkm_phone;
	}
	public void setLkm_phone(String lkm_phone) {
		this.lkm_phone = lkm_phone;
	}
	public String getLkm_mobile() {
		return lkm_mobile;
	}
	public void setLkm_mobile(String lkm_mobile) {
		this.lkm_mobile = lkm_mobile;
	}
	public String getLkm_email() {
		return lkm_email;
	}
	public void setLkm_email(String lkm_email) {
		this.lkm_email = lkm_email;
	}
	public String getLkm_qq() {
		return lkm_qq;
	}
	public void setLkm_qq(String lkm_qq) {
		this.lkm_qq = lkm_qq;
	}
	public String getLkm_position() {
		return lkm_position;
	}
	public void setLkm_position(String lkm_position) {
		this.lkm_position = lkm_position;
	}
	public String getLkm_memo() {
		return lkm_memo;
	}
	public void setLkm_memo(String lkm_memo) {
		this.lkm_memo = lkm_memo;
	}
	public Customer getCustomer() {
		return customer;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	
}


以下是一的配置文件
其中name值是javabean里的参数名。
其中class值是对象的路径。
其中clumn值是外键值。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	
	<class name="com.itheima.domain.Linkman" table="cst_linkman">
		<id name="lkm_id" column="lkm_id">
			<generator class="native"/>
		</id>
		
		<property name="lkm_name" column="lkm_name"/>
		<property name="lkm_gender" column="lkm_gender"/>
		<property name="lkm_phone" column="lkm_phone"/>
		<property name="lkm_mobile" column="lkm_mobile"/>
		<property name="lkm_email" column="lkm_email"/>
		<property name="lkm_qq" column="lkm_qq"/>
		<property name="lkm_position" column="lkm_position"/>
		<property name="lkm_memo" column="lkm_memo"/>
		
		<!-- 先配置多方 
			name	当前JavaBean中的属性
			class	属性的全路径
			column	外键的字段
		-->
		<many-to-one name="customer" class="com.itheima.domain.Customer" column="lkm_cust_id" cascade="save-update"/>
		
	</class>
	
</hibernate-mapping>











以下是多的javabean,默认记得用set的集合。


package com.itheima.domain;

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

/**
 * 客户的JavaBean
 * 一方
 * @author Administrator
 */
public class Customer {
	
	private Long cust_id;
	private String cust_name;
	private Long cust_user_id;
	private Long cust_create_id;
	private String cust_source;
	private String cust_industry;
	private String cust_level;
	private String cust_linkman;
	private String cust_phone;
	private String cust_mobile;
	
	// Hibernate框架默认的集合是set集合,集合必须要自己手动的初始化
	private Set<Linkman> linkmans = new HashSet<Linkman>();
	
	public Long getCust_id() {
		return cust_id;
	}
	public void setCust_id(Long cust_id) {
		this.cust_id = cust_id;
	}
	public String getCust_name() {
		return cust_name;
	}
	public void setCust_name(String cust_name) {
		this.cust_name = cust_name;
	}
	public Long getCust_user_id() {
		return cust_user_id;
	}
	public void setCust_user_id(Long cust_user_id) {
		this.cust_user_id = cust_user_id;
	}
	public Long getCust_create_id() {
		return cust_create_id;
	}
	public void setCust_create_id(Long cust_create_id) {
		this.cust_create_id = cust_create_id;
	}
	public String getCust_source() {
		return cust_source;
	}
	public void setCust_source(String cust_source) {
		this.cust_source = cust_source;
	}
	public String getCust_industry() {
		return cust_industry;
	}
	public void setCust_industry(String cust_industry) {
		this.cust_industry = cust_industry;
	}
	public String getCust_level() {
		return cust_level;
	}
	public void setCust_level(String cust_level) {
		this.cust_level = cust_level;
	}
	public String getCust_linkman() {
		return cust_linkman;
	}
	public void setCust_linkman(String cust_linkman) {
		this.cust_linkman = cust_linkman;
	}
	public String getCust_phone() {
		return cust_phone;
	}
	public void setCust_phone(String cust_phone) {
		this.cust_phone = cust_phone;
	}
	public String getCust_mobile() {
		return cust_mobile;
	}
	public void setCust_mobile(String cust_mobile) {
		this.cust_mobile = cust_mobile;
	}
	public Set<Linkman> getLinkmans() {
		return linkmans;
	}
	public void setLinkmans(Set<Linkman> linkmans) {
		this.linkmans = linkmans;
	}
	
}





















以下是多的配置文件
其中的name是javabean中集合参数的名称。
其中cloumn是数据库中外建的字段。
其中的class是集合对象泛型的路径。


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	
	<class name="com.itheima.domain.Customer" table="cst_customer">
		<id name="cust_id" column="cust_id">
			<generator class="native"/>
		</id>
		
		<property name="cust_name" column="cust_name"/>
		<property name="cust_user_id" column="cust_user_id"/>
		<property name="cust_create_id" column="cust_create_id"/>
		<property name="cust_source" column="cust_source"/>
		<property name="cust_industry" column="cust_industry"/>
		<property name="cust_level" column="cust_level"/>
		<property name="cust_linkman" column="cust_linkman"/>
		<property name="cust_phone" column="cust_phone"/>
		<property name="cust_mobile" column="cust_mobile"/>
		
		<!-- 配置一方 -->
		<!--
			set标签name属性:表示集合的名称
		-->
		<set name="linkmans" inverse="true">
			<!-- 需要出现子标签 -->
			<!-- 外键的字段 -->
			<key column="lkm_cust_id"/>
			<one-to-many class="com.itheima.domain.Linkman"/>
		</set>
		
	</class>
	
</hibernate-mapping>    


展开阅读全文

没有更多推荐了,返回首页