1、关联表的操作,一对一一对多 与 多对一多对多关联的高级设置,一对一,以主键关联以外键关联,一对一 以主键关联,需求有两张表,如右图。要求:对数据库进行操作,一个劳改犯一定有一张身份证,而一张身份证一定属于一个劳改犯。要求:增删改查。,一对一 以主键关联,POJO:public class Prison private String prisonId;private String prisonName;private IdCard idCard;public class IdCard private String cardId;private String cardNumber;private
2、Prison prison;,一对一 以主键关联,一对一 以主键关联,prison,插入数据,Prison p = new Prison();p.setPrisonName( Jason );IdCard ic = new IdCard();ic.setCardNumber( 9527 );/ 建造关联关系p.setIdCard( ic );ic.setPrison( p );session.save( ic );看看实际插入了几个对象?操作是否安全?,删除数据,String hql = SELECT p FROM Prison p WHERE p.prisonName = Jason;Que
3、ry query = session.createQuery( hql );List list = query.list();for ( Prison p: list ) session.delete( p );看看实际删除了几个数据?操作是否安全?,级联查询数据,String hql = SELECT p FROM Prison p WHERE p.prisonName = Jason;Query query = session.createQuery( hql );List list = query.list();for ( Prison p: list ) / 取得级联对象IdCard
4、card = p.getIdCard();System.out.println( card.getCardNumber() );通过主数据,不用再次编写嵌套子查询或外连接查询,就可以取道关联数据,操作是否方便?,跨对象查询数据,String hql = SELECT i FROM IdCard i WHERE i.prison.prisonName = Jason LIMIT 0,5;Query query = session.createQuery( hql );List list = query.list();for ( IdCard card: list ) System.out.pri
5、ntln( card.getCardNumber() );一步就位的取得关联表中数据,操作是否方便?,一对一 以外键关联,需求有两张表,如右图。要求:对数据库进行操作,一个学生一定有一张证书,而一张证书一定属于一个学生。,一对一 以外键关联,POJO:public class Teacher private String teacherId;private String teacherName;private int teacherAge;private Card teacherCard;,一对一 以外键关联,POJO:public class Card private String card
6、Id;private String cardNumber;private Teacher cardOwner;,一对一 以外键关联,插入数据,Teacher teacher = new Teacher();teacher.setTeacherName( Eric );teacher.setTeacherAge( 18 );Card card = new Card();card.setCardNumber( 007 );/ 建造关联关系teacher.setTeacherCard( card );card.setCardOwner( teacher );session.save( card );
7、,级联查询数据,String hql = SELECT c FROM Card c WHERE c.cardNumber = 007;Query query = session.createQuery( hql );List list = query.list();for ( Card card: list ) Teacher teacher = card.getCardOwner();System.out.println( teacher.getTeacherName() );,跨对象查询数据,String hql = SELECT c FROM Card c WHERE c.cardOwn
8、er.teacherAge 16;Query query = session.createQuery( hql );List list = query.list();for ( Card card: list ) System.out.println( card.getCardNumber() );,删除数据,Card card = ( Card )session.load( Card.class, 402882481eecaaa8011eecaaa9ab0001 );session.delete( card );,一对多 与 多对一,一对多与多对一有区别吗?看看这个例子。有区别吗?,一对多
9、与 多对一,POJO:public class Member private String memberId;private String memberName;private int memberAge;private Team memberTeam;,一对多 与 多对一,POJO:public class Team private String teamId;private String teamName;private int teamAge;private Set memberList = new HashSet ();,一对多 与 多对一,一对多 与 多对一,插入数据,Team te
10、am = new Team();team.setTeamName( Togogo Footballl Team );team.setTeamAge( 16 );session.save( team );,分配关联关系,Team team = ( Team )session.load( Team.class, 402882481eecc36a011eecc36c3b0001 );Member member = ( Member )session.load( Member.class, 402882481eecc36a011eecc36c4b0002 );/ 分配关联关系,事务提交,外键的值自动u
11、pdateteam.getMemberList().add( member );member.setMemberTeam( team );,取消关联关系,Team team = ( Team )session.load( Team.class, 402882481eecc36a011eecc36c3b0001 );Set memberList = team.getMemberList();for ( Member member: memberList ) if ( true = Jason.equals( member.getMemberName() ) ) / 取消关联关系,事务提交,外键的
12、值自动update为nullmemberList.remove( member );break;,删除数据(同时自动删除关联对象),/ 使用持久化对象Team team = ( Team )session.load( Team.class, 402882481eecd665011eecd6674d0001 );session.delete( team );,删除数据(只删除主对象),/ 直接使用游离态对象,指定IDTeam team = new Team();team.setTeamId( 402882481eecc36a011eecc36c3b0001 );session.delete( t
13、eam );,多对多,看看这个例子:,多对多,POJOpublic class Client private String clientId;private String clientName;private Set productList = new HashSet ();,多对多,POJOpublic class Product private String productId;private String productName;private Set clientList = new HashSet ();,多对多,多对多,插入数据,Client eric = new Client()
14、;eric.setClientName( Eric );Product mobile = new Product();mobile.setProductName( Nokia N82 );session.save( eric );session.save( mobile );,分配关联关系,Client client = ( Client )session.get( Client.class, 402882481eecf2da011eecf2dc360001 );Product product = ( Product )session.get( Product.class, 402882481
15、eecf2da011eecf2dc560002 );client.getProductList().add( product );/ 关联关系不要互给,单向就行,取消关联关系,Client client = ( Client )session.get( Client.class, 402882481eecf2da011eecf2dc360001 );Product product = ( Product )session.get( Product.class, 402882481eecf2da011eecf2dc560002 );Set productList = client.getProd
16、uctList();for ( Product p: productList ) if ( product.getProductName().equals( p.getProductName() ) ) / 取消单向的关联关系即可,不用双向productList.remove( p );break;,删除数据(级联删除),Client client = ( Client )session.get( Client.class, a );session.delete( client );注意:n层以后的关联对象都全被删除了,删除数据(两层删除),Client client = ( Client )
17、session.get( Client.class, a );Set productList = client.getProductList();for ( Product p: productList ) p.getClientList().clear();session.delete( p );session.delete( client );注意:Client和直接与Client关联的对象被删除了,删除数据(单一删除),Client client = ( Client )session.get( Client.class, a );client.getProductList().clea
18、r();session.delete( client );注意:只有Client被删除了,关联的高级属性inverse,当inverse=false时,hibernate将对set的改动反映到数据库中,关联的高级属性cascade,cascade属性的可选值:all : 所有情况下均进行关联操作。none:所有情况下均不进行关联操作。这是默认值。save-update:在执行save/update/saveOrUpdate时进行关联操作。delete:在执行delete时进行关联操作。,关联的高级属性unique,many-to-one元素增加unique=“true”属性,用于表示N的一端也必须是唯一的,在N的一端增加了唯一的约束,即成为一对一。,延迟加载,不会将查询到的数据所有数据(包含n层关联表的数据)一起加载到对象中。配置样例:,延迟加载,控制通过Product的set获取其关联对象的情况,