首页 > 代码库 > EnityBean一对一关联关系
EnityBean一对一关联关系
一、工程架构:
工程资源下载地址:http://download.csdn.net/detail/u012750578/7660633
二、beans.xml配置
<?xml version="1.0" encoding="UTF-8"?> <!-- 基于注解方式 http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd"> <context:annotation-config /> <context:component-scan base-package="com.entity"></context:component-scan> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value=http://www.mamicode.com/"oracle.jdbc.driver.OracleDriver" />>三、基于共享主键一对一多关系
1、EntityBean
RefereePrimary:
@Entity @Table(name="t_primary_referee") public class RefereePrimary { private int id; private String name; private CustomerPrimary customer; @OneToOne @PrimaryKeyJoinColumn public CustomerPrimary getCustomer() { return customer; } public void setCustomer(CustomerPrimary customer) { this.customer = customer; } //采用基于主键的一对一映射时,要把主键生成策略改为foreign //属性对应customer @Id @GeneratedValue(generator="pkGenerator") @GenericGenerator(name="pkGenerator",strategy="foreign", parameters=@Parameter(name="property",value=http://www.mamicode.com/"customer"))>
CustomerPrimary:@Entity @Table(name = "t_primary_customer") public class CustomerPrimary { private int id; private String name; private RefereePrimary referee; @Id // 标注表示这个id属性是外键,并且依赖于customer属性相对应的实体bean的id属性值(主键值) @GenericGenerator(name = "GenericGenerator", strategy = "sequence", parameters = { @Parameter(value = http://www.mamicode.com/"seq_primary_customer", name = "sequence") })>
2、Service@Service("customerPrimaryService") @Transactional public class CustomerPrimaryService { @Resource private SessionFactory sessionFactory; //测试保存 public void savePrimary() { CustomerPrimary customer = new CustomerPrimary(); customer.setName("微软"); RefereePrimary referee = new RefereePrimary(); referee.setName("赵军"); // 关联起来 // 使用基于主键的一对一时:也是只有有外键方可以保存关联关系 customer.setReferee(referee); referee.setCustomer(customer); // 先保存无外键方,先保存主键方 sessionFactory.getCurrentSession().persist(customer); // 再保存有外键方,因为有外键方要引用无外键方的主键值 sessionFactory.getCurrentSession().persist(referee); } //测试根据id获取 public CustomerPrimary getCustomerPrimary(int id) { return (CustomerPrimary) sessionFactory.getCurrentSession().get( CustomerPrimary.class, 1); } //测试根据id获取 public RefereePrimary getRefereePrimary(int id) { return (RefereePrimary) sessionFactory.getCurrentSession().get( RefereePrimary.class, 1); } // 移除关联关系 // 使用基于主键的一对一时,双方都不可以移除关联关系 public void RemoveRelationPrimary() { CustomerPrimary customer = (CustomerPrimary) sessionFactory .getCurrentSession().get(CustomerPrimary.class, 1); customer.setReferee(null); sessionFactory.getCurrentSession().persist(customer); } // 移除关联关系 // 使用基于主键的一对一时,双方都不可以移除关联关系 public void RemoveRelationNoPrimary() { RefereePrimary referee = (RefereePrimary) sessionFactory .getCurrentSession().get(RefereePrimary.class, 1); referee.setCustomer(null); sessionFactory.getCurrentSession().persist(referee); } // 因为RefereePrimary是无外键言,不可以维护关联关系,所以删除RefereePrimary时,如果有关联的Customer,就会抛异常 // 因为CustomerPrimary的id有引用RefereePrimary的id public void DeleteNoForeign() { sessionFactory.getCurrentSession() .delete(sessionFactory.getCurrentSession().get( RefereePrimary.class, 1)); } //删除就同时删除CustomerPrimary与RefereePrimary记录 public void DeleteForeign() { sessionFactory.getCurrentSession().delete( sessionFactory.getCurrentSession() .get(CustomerPrimary.class, 1)); } //测试删除 public void DeleteAll() { // 先删除有外键方 sessionFactory.getCurrentSession().delete( sessionFactory.getCurrentSession() .get(CustomerPrimary.class, 2)); } //测试获取列表信息 @SuppressWarnings("unchecked") public List<CustomerPrimary> getCustomerPrimarys(){ return sessionFactory.getCurrentSession().createQuery("from CustomerPrimary").list(); } //测试获取列表信息 @SuppressWarnings("unchecked") public List<RefereePrimary> getRefereePrimarys(){ return sessionFactory.getCurrentSession().createQuery("from RefereePrimary").list(); } }
3、Junit测试public class CustomerPrimaryServiceTest { private static CustomerPrimaryService customerPrimaryService; @BeforeClass public static void setUpBeforeClass() throws Exception { try { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "beans.xml"); customerPrimaryService = (CustomerPrimaryService) applicationContext .getBean("customerPrimaryService"); } catch (RuntimeException e) { e.printStackTrace(); } } //测试保存 @Test public void testSavePrimary() { customerPrimaryService.savePrimary(); } //测试获取关联获取值 @Test public void testGetPrimaryCustomer(){ CustomerPrimary customer=customerPrimaryService.getCustomerPrimary(1); System.out.println(customer.getReferee().getName()); } //测试获取关联获取值 @Test public void testGetPrimaryReferee(){ RefereePrimary referee=customerPrimaryService.getRefereePrimary(1); System.out.println(referee.getCustomer().getName()); } //测试移除关系关系 @Test public void testRemoveRelationPrimary(){ customerPrimaryService.RemoveRelationPrimary(); } //测试移除关系关系 @Test public void testRemoveRelationNoPrimary(){ customerPrimaryService.RemoveRelationNoPrimary(); } //无外键方,不可以删除 @Test public void testDeleteNoForeign(){ customerPrimaryService.DeleteNoForeign(); } // 有外键方 可以删除,删除是删除两个表的信息 @Test public void testDeleteForeign(){ customerPrimaryService.DeleteForeign(); } //测试获取列表信息 @Test public void testGetCustomerPrimarys(){ for(CustomerPrimary c:customerPrimaryService.getCustomerPrimarys()){ System.out.println(c.getName()); } } //测试获取列表信息 @Test public void testGetPrimaryReferees(){ for(RefereePrimary r:customerPrimaryService.getRefereePrimarys()){ System.out.println(r.getName()); } } }四、采用基于外键一对一关联关系(重点,一般采用此种方式)
1、EntityBean@Entity @Table(name="t_customer") public class Customer { private int id; private String name; private Referee referee; @Id @GenericGenerator(name = "GenericGenerator", strategy = "sequence", parameters = { @Parameter(value = http://www.mamicode.com/"seq_t_customer", name = "sequence") })>
Referee:@Entity @Table(name="t_referee") public class Referee { private int id; private String name; private Customer customer; @Id @GenericGenerator(name = "GenericGenerator", strategy = "sequence", parameters = { @Parameter(value = http://www.mamicode.com/"seq_t_referee", name = "sequence") })>
2、Service@Service("customerService") @Transactional public class CustomerService { @Resource private SessionFactory sessionFactory; public void saveForgin() { Customer customer = new Customer(); customer.setName("微软"); Referee referee = new Referee(); referee.setName("赵军"); customer.setReferee(referee); referee.setCustomer(customer); // 先保存无外键方 sessionFactory.getCurrentSession().persist(referee); sessionFactory.getCurrentSession().persist(customer); } public Customer getCustomer(int id) { return (Customer) sessionFactory.getCurrentSession().get( Customer.class, id); } public Referee getReferee(int id) { return (Referee) sessionFactory.getCurrentSession().get(Referee.class, id); } /** * 外键方可以维护关系 移除关联关系 */ public void RemoveRelationForeign() { Customer customer = (Customer) sessionFactory.getCurrentSession().get( Customer.class, 1); customer.setReferee(null); sessionFactory.getCurrentSession().persist(customer); } /** * 无外键方,无法维护关系 ,不可以维护关联关系,所以删除Referee时,如果有关联的Customer,就会抛异常 导致双方都删除不成功 */ public void RemoveRelationNoForeign() { sessionFactory.getCurrentSession().delete( sessionFactory.getCurrentSession().get(Referee.class, 1)); } /** * 外键方可以维护关系, * 会先移除关联关系 * 删除有外键方, */ public void RemoveRelationForeignOne() { sessionFactory.getCurrentSession().delete( sessionFactory.getCurrentSession().get(Customer.class, 1)); } public void DeleteNoForeignOne(){ ////先删除有外键方 sessionFactory.getCurrentSession().delete( sessionFactory.getCurrentSession().get(Referee.class, 1)); } public void DeleteForeignOne(){ ////先删除有外键方 sessionFactory.getCurrentSession().delete( sessionFactory.getCurrentSession().get(Customer.class, 1)); } public void DeleteForeignTow() { ////先删除有外键方 sessionFactory.getCurrentSession().delete( sessionFactory.getCurrentSession().get(Customer.class, 1)); //再删除无外键方 sessionFactory.getCurrentSession().delete( sessionFactory.getCurrentSession().get(Referee.class, 1)); } @SuppressWarnings("unchecked") public List<Customer> getCustomers(){ return sessionFactory.getCurrentSession().createQuery("from Customer").list(); } @SuppressWarnings("unchecked") public List<Referee> getReferees(){ return sessionFactory.getCurrentSession().createQuery("from Referee").list(); } }
3、Junit测试public class CustomerServiceTest { private static CustomerService customerService; @BeforeClass public static void setUpBeforeClass() throws Exception { try { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "beans.xml"); customerService = (CustomerService) applicationContext .getBean("customerService"); } catch (RuntimeException e) { e.printStackTrace(); } } // 测试保存 @Test public void testSaveForgin() { customerService.saveForgin(); } // 测试获取关联对象的属性值 @Test public void testGetCustomer() { Customer customer = customerService.getCustomer(1); System.out.println(customer.getReferee().getName()); } // 测试获取关联对象的属性值 @Test public void testGetReferee() { Referee referee = customerService.getReferee(1); System.out.println(referee.getCustomer().getName()); } // 测试移除关联有关系 // 外键方可以维护关系 移除关联关系 @Test public void testRemoveRelationForeign() { customerService.RemoveRelationForeign(); } // 测试移除关联有关系 // 无外键方,无法维护关系 ,不可以维护关联关系,所以删除Referee时,如果有关联的Customer,就会抛异常 导致双方都删除不成功 @Test public void testRemoveRelationNoForeign() { customerService.RemoveRelationNoForeign(); } //删除无外键方,不能删除 @Test public void testDeleteNoForeignOne(){ customerService.DeleteNoForeignOne(); } //删除有外键方,可以删除 @Test public void testDeleeteForeignOne(){ customerService.DeleteForeignOne(); } //两个再时删除,先删除有外键方,再删除无外键方 @Test public void testDeleteForeignTow() { customerService.DeleteForeignTow(); } @Test public void testGetReferees(){ for(Referee r:customerService.getReferees()){ System.out.println(r.getName()); } } @Test public void testGetCustomers(){ for(Customer c:customerService.getCustomers()){ System.out.println(c.getName()); } } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。