Hibernate 一对一关联映射对象 级联增删改查示例
2017-10-17 12:36
477 查看
这里主要用用户和身份证做示例开发,一个用户只能拥有一张身份证,一张身份证只能对应一个用户,所以二者是一对一的映射关系,这里我写一个例子,作级联增删改查操作.
代码:
#数据模型层
Person.java
Person.hbm.xml
Card.java
Card.hbm.xml
#数据访问层
PersonDao.java
#业务逻辑层
PersonBiz.java
#测试类
Test.java(级联添加)
DeleteTest.java
UpdateTest.java(改)
QueryTest.java(级联查找)
代码:
#数据模型层
Person.java
package com.sunline.entity; /** * Person entity. @author MyEclipse Persistence Tools */ public class Person implements java.io.Serializable { // Fields private Integer PId; private String name; private Card Cards; // Constructors /** default constructor */ public Person() { } /** minimal constructor */ public Person(String name) { this.name = name; } /** full constructor */ public Person(String name, Card Cards) { this.name = name; this.Cards = Cards; } // Property accessors public Integer getPId() { return this.PId; } public void setPId(Integer PId) { this.PId = PId; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Card getCards() { return this.Cards; } public void setCards(Card Cards) { this.Cards = Cards; } }
Person.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.sunline.entity.Person" table="person" catalog="association"> <id name="PId" type="java.lang.Integer"> <column name="p_id" /> <generator class="increment"></generator> </id> <property name="name" type="java.lang.String"> <column name="name" length="50" not-null="true" /> </property> <!-- 一对一映射: 没有外键方 --> <one-to-one name="Cards" class="com.sunline.entity.Card" cascade="all" lazy="false" constrained="true"></one-to-one> </class> </hibernate-mapping>
Card.java
package com.sunline.entity; /** * Card entity. @author MyEclipse Persistence Tools */ public class Card implements java.io.Serializable { // Fields private Integer CId; private String place; private Person Persons; private Integer PId; // Constructors /** default constructor */ public Card() { } /** minimal constructor */ public Card(String place, Integer PId) { this.place = place; this.PId = PId; } /** full constructor */ public Card(String place, Person Persons, Integer PId) { this.place = place; this.Persons = Persons; this.PId = PId; } // Property accessors public Integer getCId() { return this.CId; } public void setCId(Integer CId) { this.CId = CId; } public String getPlace() { return this.place; } public void setPlace(String place) { this.place = place; } public Person getPersons() { return this.Persons; } public void setPersons(Person Persons) { this.Persons = Persons; } public Integer getPId() { return this.PId; } public void setPId(Integer PId) { this.PId = PId; } }
Card.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.sunline.entity.Card" table="card" catalog="association"> <id name="CId" type="java.lang.Integer"> <column name="c_id" /> <generator class="increment"></generator> </id> <property name="place" type="java.lang.String"> <column name="place" length="80" not-null="true" /> </property> <!-- 一对一映射,有外键方 unique="true" 给外键字段添加唯一约束 --> <!-- 注:unique="ture" column="p_id"表示为engine表中的外健p_id加上唯一约束,使之一对多关系强制转化为一对一关系。 --> <many-to-one name="Persons" class="com.sunline.entity.Person" unique="true" column="p_id"></many-to-one> <property name="PId" type="java.lang.Integer" insert="false" update="false"> <column name="p_id" not-null="true"/> </property> </class> </hibernate-mapping>
#数据访问层
PersonDao.java
package com.sunline.dao; import javax.annotation.Resource; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; import com.sunline.entity.Person; @Repository(value="personDao") public class PersonDao extends HibernateDaoSupport { /* * 使用注解必须添加以下方式 */ @Resource public void setSessionFacotry(SessionFactory sessionFacotry) { super.setSessionFactory(sessionFacotry); } /* * 添加人员 */ public void AddPerson(Person person){ System.out.println("成功添加人员信息"); this.getHibernateTemplate().save(person); } /* * 查询id为XX的所有人员和证件信息 */ public Person findById(int id) { Person person=(Person)this.getHibernateTemplate().get(Person.class,id); return person; } /* * 修改个人信息 */ public void UpdatePerson(String name, int p_id){ System.out.println("成功修改数据!"); this.getHibernateTemplate().bulkUpdate("update Person set name = ? where PId = ?",new Object[]{name,p_id}) ; } /* * 根据id级联删除个人信息和证件信息 */ public void DeletePerson(int id){ System.out.println("成功删除数据!"); this.getHibernateTemplate().delete(findById(id)); } }
#业务逻辑层
PersonBiz.java
package com.sunline.biz; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import com.sunline.dao.PersonDao; import com.sunline.entity.Person; @Service(value="personBiz") public class PersonBiz { /* * 添加人员信息 */ @Autowired @Qualifier("personDao") //使用@Qualifier注解来说明使用哪一个实现类 PersonDao personDao; public void AddPerson(Person person){ personDao.AddPerson(person); } /* * 查询id为XX的所有人员和证件信息 */ public Person findById(int id) { return personDao.findById(id); } /* * 修改个人信息 */ public void UpdatePerson(String name, int p_id){ personDao.UpdatePerson(name, p_id); } /* * 根据id级联删除个人信息和证件信息 */ public void DeletePerson(int id){ personDao.DeletePerson(id); } }
#测试类
Test.java(级联添加)
package com.sunline.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.sunline.biz.PersonBiz; import com.sunline.entity.Card; import com.sunline.entity.Person; @Transactional(propagation=Propagation.REQUIRED) public class Test { /** * @param args */ @SuppressWarnings("unchecked") @Transactional(propagation=Propagation.REQUIRED) //用事务管理做双重保障 public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); PersonBiz personBiz = (PersonBiz) ctx.getBean("personBiz"); Person person = new Person(); person.setName("海哥"); Card card = new Card(); card.setPlace("山东省烟台市"); card.setPersons(person); //双向一对一 person.setCards(card); //双向一对一 personBiz.AddPerson(person); } }
DeleteTest.java
package com.sunline.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.sunline.biz.PersonBiz; @Transactional(propagation=Propagation.REQUIRED) public class DelteteTest { /** * @param args */ @SuppressWarnings("unchecked") @Transactional(propagation=Propagation.REQUIRED) //用事务管理做双重保障 public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); PersonBiz personBiz = (PersonBiz) ctx.getBean("personBiz"); personBiz.DeletePerson(1); //级联删除该用户ID相关的所有个人信息和证件信息 } }
UpdateTest.java(改)
package com.sunline.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.sunline.biz.PersonBiz; @Transactional(propagation=Propagation.REQUIRED) public class UpdateTest { /** * @param args */ @SuppressWarnings("unchecked") @Transactional(propagation=Propagation.REQUIRED) //用事务管理做双重保障 public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); PersonBiz personBiz = (PersonBiz) ctx.getBean("personBiz"); personBiz.UpdatePerson("杨旭", 1); } }
QueryTest.java(级联查找)
package com.sunline.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.sunline.biz.PersonBiz; import com.sunline.entity.Card; import com.sunline.entity.Person; @Transactional(propagation=Propagation.REQUIRED) public class QueryTest { /** * @param args */ @SuppressWarnings("unchecked") @Transactional(propagation=Propagation.REQUIRED) //用事务管理做双重保障 public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); PersonBiz personBiz = (PersonBiz) ctx.getBean("personBiz"); Person person = personBiz.findById(1); Card card = person.getCards(); System.out.println("你的个人信息为: "+person.getPId()+" "+person.getName()); System.out.println("你的证件信息为: "+card.getCId()+" "+card.getPlace()); } }
相关文章推荐
- Hibernate 多对多关联映射对象 级联增删改查示例
- Hibernate 一对多关联映射对象 级联增删改查示例
- 【HIbernate框架学习】:Hibernate对象关系映射之一对一关联映射(二)
- Hibernate之对象关系映射03一对一单向外键关联
- Hibernate之对象关系映射05一对一双向主键关联
- 【HIbernate框架学习】:Hibernate对象关系映射之一对一关联映射(一)
- Hibernate之对象关系映射04一对一双向关联
- Hibernate之对象关系映射01一对一单向关联
- Hibernate之对象关系映射02一对一单向主键关联
- Hibernate之对象关系映射06一对一双向外键关联
- 【hibernate】之关于一对一单向,双向关联映射
- hibernate映射一对一关联(主键关联)
- 【SSH进阶之路】Hibernate映射——一对一双向关联映射(六)
- 【SSH进阶之路】Hibernate映射——一对一双向关联映射(六)
- Hibernate3.5.3 之 一对一映射关联
- 【Hibernate步步为营】--双向关联一对一映射详解(二)
- Hibernate数据关联映射之一对一和多对多
- 【SSH进阶之路】Hibernate映射——一对一双向关联映射(六)
- Hibernate关系映射(2)_一对一双向外键关联
- 【SSH进阶之路】Hibernate映射——一对一单向关联映射(五)