Hibernate表关联配置

一对一

一对一主键关联

1.XML配置

  • 必须双向关联
  • 被关联方主键生成策略为外键生成策略
  • 被关联方one-to-one需设置constrained=“true”约束




    
        
           
        
        
        
    





    
        
            
                person
            
        
        
        
    

2.Java注解配置

  • 主控方使用@PrimaryKeyJoinColumn
  • 被控方在@OneToOne中要加上(mappedBy = "card")
//基于注解的配置
@Entity
@Table(name = "person")
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(name = "person_name")
    private String personName;

    //共享主键关联使用PrimaryKeyJoinColumn
    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    private Card card;
  }

@Entity
@Table(name = "card")
public class Card {
    @Id
    @GenericGenerator(name = "generator",strategy = "foreign",
            parameters = @org.hibernate.annotations.Parameter(name = "property",value = "person"))
    @GeneratedValue(generator = "generator")
    private Integer id;

    @Column(name = "card_num")
    private String cardNum;


    //必须设置OneToOne,mappedBy为自己在对方中的属性名
    @OneToOne(mappedBy = "card")
    private Person person;
  }

一对一外键关联

数据库结构


Hibernate表关联配置_第1张图片
数据库结构

1.XML配置

  • 各自主键生成策略无外键约束
  • 主控方(含外键的一方)使用many-to-one,并且要加上unique="true"约束
  • 被控方使用one-to-one,并且要加上property-ref="passageContent"




    
        
            
        
        
        
    





    
        
            
        
        
        
    

2.Java注解配置

  • 区别于XML配置,这里全部采用@OneToOne
  • 主控方使用@JoinColumn(name = "content_id")
  • 被控方使用@OneToOne(mappedBy = "passageContent"),mappedBy相当于XML中的property-ref,是一个反向声明
@Entity
@Table(name = "passage")
public class Passage {

    @Id
    @GeneratedValue
    private Integer id;
    private String title;

    @OneToOne
    @JoinColumn(name = "content_id")
    private PassageContent passageContent;
  }

@Entity
@Table(name = "passage_content")
public class PassageContent {

    @Id
    @GeneratedValue
    private Integer id;
    private String content;


    @OneToOne(mappedBy = "passageContent")
    private Passage passage;
  }

一对多关联

  • 保存时先保存一端,再保存多端
    数据库结构


    Hibernate表关联配置_第2张图片
    一对多关联

    1.XML配置

  • 一端在set中要指明表及关联的键(即table和key属性),并且要放弃关系维护




    
        
            
        


        
        
        
        
    





    
        
        
            
        


        
        
            
            
        
    

2.Java注解配置

  • ManyToOne端要加上JoinColumn,OneToMany要加上mappedBy
  • 如需级联操作,一端还需加上Cascade
@Entity
@Table
public class Address {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(name = "city_name")
    private String cityName;
    private String address;

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;
  }

@Entity
@Table
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String username;

    @OneToMany(mappedBy = "user")
    @Cascade(CascadeType.DELETE)
    private Set
addressSet; }

多对多关联

数据库结构


Hibernate表关联配置_第3张图片
多对对关联

1.XML配置





    
        
            
        
        

        
            
            
        
    





    
        
            
        
        

        
            
            
        
    

2.Java注解配置

@Entity
@Table
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;



    @Column(name = "student_name")
    private String studentName;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @ManyToMany
    @JoinTable(name = "student_teacher_real",joinColumns = {@JoinColumn(name = "student_id")},inverseJoinColumns = {@JoinColumn(name = "teacher_id")})
    private Set teacherSet;
  }

@Entity
@Table
public class Teacher {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(name = "teacher_name")
    private String teacherName;

    @ManyToMany(mappedBy = "teacherSet")
    private Set studentSet;
  }

Java注解总结

  • 如果是主键关联,那么就使用@PrimaryKeyJoinColumn,只要声明@JoinColumn,@PrimaryKeyJoinColumn,@JoinTable的都属于主控方,外键通常也就存储在主控方,对应的含有mappedBy的都为被控方。
  • 一对一关联不管是主键关联还是外键关联,基于注解都是OneToOne

XML配置总结

  • 一对一主键关联时,由于被控方的主键是主控方的外键,所以被控方的OneToOne标签中需要加constrained="true"约束
  • 被控方通常需要设置inverse="true"放弃关系维护
  • 这样的标签放置在集合内部

你可能感兴趣的:(Hibernate表关联配置)