hibernate一对一配置两种方式

  外键方式:

 



		
			
				
			
		












		
			

			
		

		
			
		

		
		


	

测试代码:

public void test()
    {
        Bed bed = new Bed();
        bed.setId("001008");
        bed.setSizes(40);

        Person person = new Person();
        person.setId("002008");
        person.setName("ms chen");

       bed.setPerson(person);
        person.setBed(bed);

        Session session = this.getHibernateTemplate().getSessionFactory().openSession();
        session.beginTransaction();

        session.save(person);

        session.beginTransaction().commit();

        session.close();

    }

主键方式:



		
		
		    
                person   
               
		
		











		
			

			
		

		
			
		


  
  

	

测试代码:

public void test()
    {
        Bed bed = new Bed();
        bed.setSizes(40);

        Person person = new Person();
        person.setId("002009");
        person.setName("ms chen");

       bed.setPerson(person);
//        person.setBed(bed);

        Session session = this.getHibernateTemplate().getSessionFactory().openSession();
        session.beginTransaction();

        session.save(person); //分别保存
        session.save(bed);

        session.beginTransaction().commit();

        session.close();

    }




你可能感兴趣的:(======ssh)