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

hibernate一对多单向关联关系

程序员文章站 2022-03-08 13:22:03
...

前一段时间一直在忙于一个演示项目,近日完工了,就回过看来看了看hibernate,hibernate中一对多的关联最为常见,就简单的谈谈这个,言语不对,忘指点:

下面我以Father 和 Child为例为说明:

先来看看两个实体类

1)Father.java

package com.syp.pojo;

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

/**
 * Father entity.
 * 
 * @author MyEclipse Persistence Tools
 */

public class Father implements java.io.Serializable {

	// Fields

	private Integer id;
	private String name;
	private Set childs = new HashSet(0);

	// Constructors

	/** default constructor */
	public Father() {
	}

	/** full constructor */
	public Father(String name, Set childs) {
		this.name = name;
		this.childs = childs;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

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

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set getChilds() {
		return this.childs;
	}

	public void setChilds(Set childs) {
		this.childs = childs;
	}

}

 对应的hbm.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
	Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
	<class name="com.syp.pojo.Father" table="father" catalog="test">
		<id name="id" type="java.lang.Integer">
			<column name="id" />
		</id>
		<property name="name" type="java.lang.String">
			<column name="name" length="30" />
		</property>
		<set name="childs" inverse="true" cascade="all">
			<key>
				<column name="father_id" />
			</key>
			<one-to-many class="com.syp.pojo.Child" />
		</set>
	</class>
</hibernate-mapping>

 

2)Child.java

package com.syp.pojo;

/**
 * Child entity.
 * 
 * @author MyEclipse Persistence Tools
 */

public class Child implements java.io.Serializable {

	// Fields

	private Integer id;
	private Father father;
	private String name;

	// Constructors

	/** default constructor */
	public Child() {
	}

	/** full constructor */
	public Child(Father father, String name) {
		this.father = father;
		this.name = name;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

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

	public Father getFather() {
		return this.father;
	}

	public void setFather(Father father) {
		this.father = father;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

 对应的hbm.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.syp.pojo.Child" table="child" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
          
        </id>
        <many-to-one name="father" class="com.syp.pojo.Father" fetch="select">
            <column name="father_id" />
        </many-to-one>
        <property name="name" type="java.lang.String">
            <column name="name" length="30" />
        </property>
    </class>
</hibernate-mapping>

 

3)关于hibernate.cfg.xml的配置如下:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

	<session-factory>
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
		</property>
		<property name="connection.url">
			jdbc:mysql://localhost:3306/test
		</property>
		<property name="connection.username">root</property>
		<property name="connection.password">root</property>
		<property name="connection.driver_class">
			com.mysql.jdbc.Driver
		</property>
		<property name="myeclipse.connection.profile">
			mysqltest
		</property>
		
		<property name="show_sql">true</property>
		<mapping resource="com/syp/pojo/Father.hbm.xml" />
		<mapping resource="com/syp/pojo/Child.hbm.xml" />


	</session-factory>

</hibernate-configuration>

4) HibernateSessionFactory.java如下,小弟是用E直接生成的,对E不齿的就请原谅了

package com.syp.hib;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

/**
 * Configures and provides access to Hibernate sessions, tied to the current
 * thread of execution. Follows the Thread Local Session pattern, see
 * {@link http://hibernate.org/42.html }.
 */
public class HibernateSessionFactory {

	/**
	 * Location of hibernate.cfg.xml file. Location should be on the classpath
	 * as Hibernate uses #resourceAsStream style lookup for its configuration
	 * file. The default classpath location of the hibernate config file is in
	 * the default package. Use #setConfigFile() to update the location of the
	 * configuration file for the current session.
	 */
	private static String CONFIG_FILE_LOCATION = "/com/syp/hib/hibernate.cfg.xml";
	private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
	private static Configuration configuration = new Configuration();
	private static org.hibernate.SessionFactory sessionFactory;
	private static String configFile = CONFIG_FILE_LOCATION;

	static {
		try {
//			configuration.configure(configFile);
			 configuration.configure();
			sessionFactory = configuration.buildSessionFactory();
		} catch (Exception e) {
			System.err.println("%%%% Error Creating SessionFactory %%%%");
			e.printStackTrace();
		}
	}

	private HibernateSessionFactory() {
	}

	/**
	 * Returns the ThreadLocal Session instance. Lazy initialize the
	 * <code>SessionFactory</code> if needed.
	 * 
	 * @return Session
	 * @throws HibernateException
	 */
	public static Session getSession() throws HibernateException {
		Session session = (Session) threadLocal.get();

		if (session == null || !session.isOpen()) {
			if (sessionFactory == null) {
				rebuildSessionFactory();
			}
			session = (sessionFactory != null) ? sessionFactory.openSession()
					: null;
			threadLocal.set(session);
		}

		return session;
	}

	/**
	 * Rebuild hibernate session factory
	 * 
	 */
	public static void rebuildSessionFactory() {
		try {
			configuration.configure(configFile);
			// configuration.configure();
			sessionFactory = configuration.buildSessionFactory();
		} catch (Exception e) {
			System.err.println("%%%% Error Creating SessionFactory %%%%");
			e.printStackTrace();
		}
	}

	/**
	 * Close the single hibernate session instance.
	 * 
	 * @throws HibernateException
	 */
	public static void closeSession() throws HibernateException {
		Session session = (Session) threadLocal.get();
		threadLocal.set(null);

		if (session != null) {
			session.close();
		}
	}

	/**
	 * return session factory
	 * 
	 */
	public static org.hibernate.SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * return session factory
	 * 
	 * session factory will be rebuilded in the next call
	 */
	public static void setConfigFile(String configFile) {
		HibernateSessionFactory.configFile = configFile;
		sessionFactory = null;
	}

	/**
	 * return hibernate configuration
	 * 
	 */
	public static Configuration getConfiguration() {
		return configuration;
	}

}

 

5)以下测试的类:Test.java

package com.syp.test;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.syp.hib.HibernateSessionFactory;
import com.syp.pojo.Child;
import com.syp.pojo.Father;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Session session;
		Father f = new Father();
		f.setId(1);
		f.setName("likui");
		Child c = new Child();
		c.setName("c");
		c.setId(1);
		c.setFather(f);
		f.getChilds().add(c);

		session = HibernateSessionFactory.getSession();
		 session.beginTransaction();
		 session.save(f);
		 session.getTransaction().commit();

//		Father f2 = (Father) session.get(Father.class, 1);
//
//		System.out.println(f2.getName());
		Query query = session.createQuery("from Father");
		List l = query.list();
		for (int i = 0; i < l.size(); i++) {
			Father f3 = (Father) l.get(i);
			System.out.println(f3.getName());
		}

	}

}

 

在hibernate 中一对多关键点于对"inverse", "cascade"的设置,首先来说明一下这两个属性有什么作用,"inverse"的中文意思是:adj.
倒转的, 反转的
n.
反面
v.
倒转

 是一个多属性的词,我想你已经明白了,它表示与此类关联的关系由谁来维护,通俗的讲就是,此类及与与此类关联的类,这两个类之间的关系由哪一方来维护。它的取值只有两个“true"or"false",默认值为"false".

其次是"cascade",这个是一个很重要的属性,如果你想体会hibernate的强大,你一定要注意它了,因它它会给你带来意想不到的惊喜。

它的取值:

none:在保存,删除或修改当前对象时,不对其附属对象(关联对象)进行级联
hibernate一对多单向关联关系
            
    
    博客分类: 原创 HibernateMyeclipseXMLMySQLC#           操作。它是默认值。
hibernate一对多单向关联关系
            
    
    博客分类: 原创 HibernateMyeclipseXMLMySQLC#     save-update:在保存,更新当前对象时,级联保存,更新附属对象(临时对象,
hibernate一对多单向关联关系
            
    
    博客分类: 原创 HibernateMyeclipseXMLMySQLC#           游离对象)。
hibernate一对多单向关联关系
            
    
    博客分类: 原创 HibernateMyeclipseXMLMySQLC#     delete:在删除当前对象时,级联删除附属对象。
hibernate一对多单向关联关系
            
    
    博客分类: 原创 HibernateMyeclipseXMLMySQLC#     all:所有情况下均进行级联操作,即包含save-update和delete操作。
hibernate一对多单向关联关系
            
    
    博客分类: 原创 HibernateMyeclipseXMLMySQLC#     delete-orphan:删除和当前对象解除关系的附属对象。

,请各位注意一下Father.hbm.xml文件,为了便于说明,在贴出来

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
	Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
	<class name="com.syp.pojo.Father" table="father" catalog="test">
		<id name="id" type="java.lang.Integer">
			<column name="id" />
		</id>
		<property name="name" type="java.lang.String">
			<column name="name" length="30" />
		</property>
		<set name="childs" inverse="true" cascade="all">
                                    在这里设置了“inverse=true",它表里当在操作Father与Child时,再者之间的关系由对方去管理,
就是由Chil来管理,“casCade=all",就是我们在对此对象进行有的操作时,与此关联的附属对象均会被级联操作,如果是保存的话,
附属对象也会被保存,其它操作也是如此。
			<key>
				<column name="father_id" />
			</key>
			<one-to-many class="com.syp.pojo.Child" />
		</set>
	</class>
</hibernate-mapping>

  在测试的代码中我们看到只保存了Father ,没有保存Child,Child也会被保存,就是CasCade的作用。

 

在说到这,欢迎各位大牛指点。

源码可下载: