- 浏览: 4170572 次
最新评论
【hibernate实例】赵雅智_双项一对多(多对一)增加删除
新建包www.hbsi.net.one2mantboth
新建持久化类
Department.java
package www.hbsi.net.one2manyboth; import java.util.Set; public class Department { private int id; private String name; private Set<Employee> emps; 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<Employee> getEmps() { return emps; } public void setEmps(Set<Employee> emps) { this.emps = emps; } }
Employee.java
package www.hbsi.net.one2manyboth; public class Employee { private int id; private String username; private Department dept; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public void setDept(Department dept) { this.dept = dept; } public Department getDept() { return dept; } }
新建持久化类配置文件
Department.hbm.xml
<?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="www.hbsi.net.one2manyboth"> <class name="Department" table="department"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name" column="name" type="string"/> <set name="emps" table="employee"> <key column="dept_id"/> <one-to-many class="Employee"/> </set> </class> </hibernate-mapping>
Employee.hbm.xml
<?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="www.hbsi.net.one2manyboth"> <class name="Employee" table="employee"> <id name="id" column="id"> <generator class="native"/> </id> <property name="username" column="username" type="string"/> <many-to-one name="dept" column="dept_id"></many-to-one> </class> </hibernate-mapping>
更改hibernate配置文件
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/hibernate </property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="www/hbsi/net/one2manyboth/Department.hbm.xml" /> <mapping resource="www/hbsi/net/one2manyboth/Employee.hbm.xml" /> </session-factory> </hibernate-configuration>
增加
One2manyboth.java(1)一方放弃维护
invarse属性的意思是:你要放弃维护吗,设置成true,默认为false
Department.hbm.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/hibernate </property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="www/hbsi/net/one2manyboth/Department.hbm.xml" /> <mapping resource="www/hbsi/net/one2manyboth/Employee.hbm.xml" /> </session-factory> </hibernate-configuration>
One2manyboth.java
package www.hbsi.net.one2manyboth; import org.hibernate.Session; import org.junit.Test; import www.hbsi.net.util.HibernateSessionFactory; public class One2manyboth { @Test public void add() { Session session = HibernateSessionFactory.getSession(); session.beginTransaction(); //创建部门 Department dept = new Department(); dept.setName("部门1"); //创建雇员1 Employee emp1 = new Employee(); emp1.setUsername("雇员1"); //告诉hibernate部员是哪个部门 emp1.setDept(dept); //创建雇员2 Employee emp2 = new Employee(); emp2.setUsername("雇员2"); emp2.setDept(dept); session.save(emp1); session.save(emp2); session.save(dept); session.getTransaction().commit(); HibernateSessionFactory.closeSession(); } }
One2manyboth.java(2)级联增加
Department.hbm.xml
<?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="www.hbsi.net.one2manyboth"> <class name="Department" table="department"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name" column="name" type="string"/> <set name="emps" table="employee"inverse="true"> <key column="dept_id"/> <one-to-many class="Employee"/> </set> </class> </hibernate-mapping>
Employee.hbm.xml
<?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="www.hbsi.net.one2manyboth"> <class name="Employee" table="employee"> <id name="id" column="id"> <generator class="native"/> </id> <property name="username" column="username" type="string"/> <many-to-one name="dept" column="dept_id" cascade="all"></many-to-one> </class> </hibernate-mapping>
One2manyboth.java
package www.hbsi.net.one2manyboth; import org.hibernate.Session; import org.junit.Test; import www.hbsi.net.util.HibernateSessionFactory; public class One2manyboth { @Test public void add() { Session session = HibernateSessionFactory.getSession(); session.beginTransaction(); // 创建部门 Department dept = new Department(); dept.setName("部门1"); // 创建雇员1 Employee emp1 = new Employee(); emp1.setUsername("雇员1"); // 告诉hibernate部员是哪个部门 emp1.setDept(dept); // 创建雇员2 Employee emp2 = new Employee(); emp2.setUsername("雇员2"); emp2.setDept(dept); session.save(emp1); session.save(emp2); session.getTransaction().commit(); HibernateSessionFactory.closeSession(); } }
One2manyboth.java(3)级联删除_删除雇员时删除部门
Department.hbm.xml
<?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="www.hbsi.net.one2manyboth"> <class name="Department" table="department"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name" column="name" type="string"/> <set name="emps" table="employee" cascade="save-update"> <key column="dept_id"/> <one-to-many class="Employee"/> </set> </class> </hibernate-mapping>
Employee.hbm.xml
<?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="www.hbsi.net.one2manyboth"> <class name="Employee" table="employee"> <id name="id" column="id"> <generator class="native"/> </id> <property name="username" column="username" type="string"/> <many-to-one name="dept" column="dept_id" cascade="delete"></many-to-one> </class> </hibernate-mapping>
One2manyboth.java
package www.hbsi.net.one2manyboth; import org.hibernate.Session; import org.junit.Test; import www.hbsi.net.util.HibernateSessionFactory; public class One2manyboth { @Test public void add() { Session session = HibernateSessionFactory.getSession(); session.beginTransaction(); // 创建部门 Department dept = new Department(); dept.setName("部门1"); // 创建雇员1 Employee emp1 = new Employee(); emp1.setUsername("雇员1"); // 告诉hibernate部员是哪个部门 emp1.setDept(dept); // 创建雇员2 Employee emp2 = new Employee(); emp2.setUsername("雇员2"); emp2.setDept(dept); session.save(emp1); session.save(emp2); session.getTransaction().commit(); HibernateSessionFactory.closeSession(); } //级联删除 @Test public void delete(){ Session session = HibernateSessionFactory.getSession(); session.beginTransaction(); Employee emp = (Employee) session.get(Employee.class,45); session.delete(emp); session.getTransaction().commit(); HibernateSessionFactory.closeSession(); } }
删除前:
删除后:
One2manyboth.java(4)级联删除_删除部门时删除雇员
Department.hbm.xml
<?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="www.hbsi.net.one2manyboth"> <class name="Department" table="department"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name" column="name" type="string"/> <set name="emps" table="employee" cascade="save-update,delete"> <key column="dept_id"/> <one-to-many class="Employee"/> </set> </class> </hibernate-mapping>
Employee.hbm.xml
<?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="www.hbsi.net.one2manyboth"> <class name="Employee" table="employee"> <id name="id" column="id"> <generator class="native"/> </id> <property name="username" column="username" type="string"/> <many-to-one name="dept" column="dept_id"></many-to-one> </class> </hibernate-mapping>
One2manyboth.java
package www.hbsi.net.one2manyboth; import org.hibernate.Session; import org.junit.Test; import www.hbsi.net.util.HibernateSessionFactory; public class One2manyboth { @Test public void add() { Session session = HibernateSessionFactory.getSession(); session.beginTransaction(); // 创建部门 Department dept = new Department(); dept.setName("部门1"); // 创建雇员1 Employee emp1 = new Employee(); emp1.setUsername("雇员1"); // 告诉hibernate部员是哪个部门 emp1.setDept(dept); // 创建雇员2 Employee emp2 = new Employee(); emp2.setUsername("雇员2"); emp2.setDept(dept); session.save(emp1); session.save(emp2); session.getTransaction().commit(); HibernateSessionFactory.closeSession(); } //级联删除 @Test public void delete(){ Session session = HibernateSessionFactory.getSession(); session.beginTransaction(); //Employee emp = (Employee) session.get(Employee.class,45); //session.delete(emp); Department dept = (Department) session.get(Department.class,26); session.delete(dept); session.getTransaction().commit(); HibernateSessionFactory.closeSession(); } }
删除前:
删除后:
相关推荐
Hibernate一对一,一对多,多对多实例
Hibernate_3.2.0_Reference_zh_CN.rar hibernate中文api
hibernate实例(一对多,多对一)hibernate实例(一对多,多对一)hibernate实例(一对多,多对一)
hibernate_validator_reference 官方文档,路过不要错过
Hibernate_3.2.0_Reference_zh_CN比较好用,欢迎大家下载
Hibernate的多对一和一对多操作实例
Hibernate教程1_从入门到精通Hibernate教程1_从入门到精通Hibernate教程1_从入门到精通Hibernate教程1_从入门到精通
struts_hibernate_velocity_mysql 开发实例 ................
学习hibernate_cache_level二级缓存项目
Hibernate_PDF_超好 Hibernate_PDF_超好 Hibernate_PDF_超好
hibernate_3.5.0_final EntityManager 参考文档 pdf hibernate_3.5.0_final EntityManager 参考文档 pdf
hibernate_jar_2hibernate_jar_2
Hibernate_3.2.0_Reference_zh_CN.chm Hibernate_3.2.0_Reference_zh_CN.chm
Hibernate一对多实例,包括一对多插入,一对多查询。
spring_hibernate整合实例 spring_hibernate整合实例
hibernate jar包_1hibernate jar包_1
hibernate一对一实例
hibernate_note_new_tarhibernate_note_new_tar