本文共 5055 字,大约阅读时间需要 16 分钟。
1 2 3 4 5 6 | CREATE TABLE `hibernate`.`person` ( `id` INT NOT NULL AUTO_INCREMENT, ` name ` VARCHAR (45) NULL , `age` INT NULL , `idcard_id` INT NULL , PRIMARY KEY (`id`)); |
1 2 3 4 5 | CREATE TABLE `hibernate`.`idcard` ( `id` INT NOT NULL AUTO_INCREMENT, `number` INT NULL , `content` VARCHAR (45) NULL , PRIMARY KEY (`id`)); |
1 2 3 4 5 6 7 8 | public class Person { private Long id; private String name; private Long age; private IDCard idCard; //省略get、set方法 } |
1 2 3 4 5 6 7 8 9 10 | < hibernate-mapping > < class name = "com.ligang.domain.Person" table = "person" > < id name = "id" column = "id" type = "long" > < generator class = "identity" /> </ id > < property name = "name" column = "name" type = "string" /> < property name = "age" column = "age" type = "long" /> < many-to-one name = "idCard" class = "com.ligang.domain.IDCard" column = "idcard_id" cascade = "save-update" ></ many-to-one > </ class > </ hibernate-mapping > |
1 2 3 4 5 6 7 | public class IDCard { private Long id; private Long number; private String content; //省略get、set方法 } |
1 2 3 4 5 6 7 8 9 | < hibernate-mapping > < class name = "com.ligang.domain.IDCard" table = "idcard" > < id name = "id" column = "id" type = "long" > < generator class = "identity" /> </ id > < property name = "number" column = "number" type = "long" /> < property name = "content" column = "content" type = "string" /> </ class > </ hibernate-mapping > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | @Test public void addPerson(){ Session session=hibernateDao.getSession(); Transaction tx=session.beginTransaction(); Person p= new Person(); p.setName( "张三" ); p.setAge(122L); IDCard idCard= new IDCard(); idCard.setNumber(123445L); idCard.setContent( "你是一个人" ); p.setIdCard(idCard); session.save(p); tx.commit(); session.close(); } |
1 2 | Hibernate: insert into hibernate.idcard (number, content) values (?, ?) Hibernate: insert into hibernate.person ( name , age, idcard_id) values (?, ?, ?) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | @Test public void updatePerson(){ Session session=hibernateDao.getSession(); Transaction tx=session.beginTransaction(); Person p=(Person) session.get(Person. class ,6L); p.setName( "张三" ); p.setAge(122L); IDCard idCard= new IDCard(); idCard.setNumber(123445L); idCard.setContent( "你是一个人" ); p.setIdCard(idCard); session.save(p); tx.commit(); session.close(); } |
1 2 3 | Hibernate: select person0_.id as id1_3_0_, person0_. name as name2_3_0_, person0_.age as age3_3_0_, person0_.idcard_id as idcard_i4_3_0_ from hibernate.person person0_ where person0_.id=? Hibernate: insert into hibernate.idcard (number, content) values (?, ?) Hibernate: update hibernate.person set name =?, age=?, idcard_id=? where id=? |
1 2 3 4 5 6 7 8 | public class IDCard { private Long id; private Long number; private String content; private Person person; //略get、set方法 } |
1 2 3 4 5 6 7 8 9 10 | < hibernate-mapping > < class name = "com.ligang.domain.IDCard" table = "idcard" > < id name = "id" column = "id" type = "long" > < generator class = "identity" /> </ id > < property name = "number" column = "number" type = "long" /> < property name = "content" column = "content" type = "string" /> < one-to-one name = "person" property-ref = "idCard" ></ one-to-one > </ class > </ hibernate-mapping > |
1 2 3 4 5 6 7 8 9 10 11 | @Test public void getPerson(){ Session session=hibernateDao.getSession(); Transaction tx=session.beginTransaction(); IDCard idCard=(IDCard) session.get(IDCard. class ,2L); System.out.println(idCard.getPerson().getName()); tx.commit(); session.close(); } |
1 2 | Hibernate: select idcard0_.id as id1_1_0_, idcard0_.number as number2_1_0_, idcard0_.content as content3_1_0_, person1_.id as id1_3_1_, person1_. name as name2_3_1_, person1_.age as age3_3_1_, person1_.idcard_id as idcard_i4_3_1_ from hibernate.idcard idcard0_ left outer join hibernate.person person1_ on idcard0_.id=person1_.idcard_id where idcard0_.id=? 张三 |
转载地址:http://ywyhl.baihongyu.com/