Hibernate annotation development of association mapping annotation

review

Hibernate annotation development and other level annotations

Hibernate annotation development of attribute-level annotations

1. One-to-one single foreign key association

Students04

@Entity
public class Students04 implements Serializable {

    @Id //设置为主键
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int sid;
    @OneToOne(cascade = CascadeType.ALL)//指定被控类, 默认是全级联
    @JoinColumn(name = "pid", unique = true)//name指定被控类(IdCard类)的主键, unique说明该键是唯一的
    private IdCard card;
    private String gender; //性别
    private Date birthday; //出生日期
    private String major; //专业  ... 构造方法和settergetter方法自行补充 ...}

IdCard

@Entity
public class IdCard {

    @Id
    @GeneratedValue(generator = "pid")
    @GenericGenerator(name = "pid", strategy = "assigned")
    @Column(length = 18)
    private String pid;  //身份证号码
    private String sname;  //学生姓名  ... 构造方法和settergetter方法自行补充 ...}

Configuration file

<mapping class="com.idea.hibernate.oto_fk.Students04"/>
<mapping class="com.idea.hibernate.oto_fk.IdCard"/>

Test (Before saving the main table object, you should save the foreign key object)

@Test
public void addStudents04(){
    Configuration configuration = new Configuration().configure();
    SessionFactory sessionFactory = createSessionFactory(configuration);
    Session session = sessionFactory.getCurrentSession();
    Transaction tx = session.beginTransaction();
    IdCard card = new IdCard("1234567890123456", "王五");
    Students04 students04 = new Students04(card, "男", new Date(), "Java开发");
    session.save(card);
    session.save(students04);
    tx.commit();
}

/**
 * 建表策略
 */
private SessionFactory createSessionFactory(Configuration configuration){
    //创建服务注册对象
    StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
    //生成SssionFactory
    SessionFactory  sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    //返回配置对象
    return sessionFactory;
}

ps: The annotations in the same entity class should either be written on the attributes, or written on the getXxx method, and cannot be mixed. 

2. One-to-one bidirectional foreign key association

Features: I am in you, you in me

The writing of the master party is the same as the one-to-one one-way association, the difference lies in the accused party (IdCard)

For two-way association, the mappedBy attribute must be set and handed over to the master to control.

IdCard02

@Entity
public class IdCard02 {

    @Id
    @GeneratedValue(generator = "pid")
    @GenericGenerator(name = "pid", strategy = "assigned")
    @Column(length = 18)
    private String pid;  //身份证号码
    private String sname;  //学生姓名
    @OneToOne(mappedBy = "card")  //指定主控方(Students05)中的属性
    private Students05 students05;  ... 构造方法和settergetter方法自行补充 ...}

The other parts are the same. 

3. One-to-one bidirectional foreign key union primary key

① Create the primary key class StudentsPK

② Annotation @Embeddable on the primary key class

@Embeddable
public class StudentsPK implements Serializable {

    private static final long serialVersionUID = 6632444697825110476L;
    @Column(length = 18)
    private String id; //ID number
    @Column(length = 8)
    private String sid; //Student ID...}

③ Introduce the attribute of the primary key class in the entity class, and use the annotation @EmbeddedId on the attribute

Move to the attribute-level annotations of Hibernate annotation development in detail

4. One-to-many single foreign key association

One party holds a collection of many parties, such as a class with multiple students (one-to-many)

Students07

@Entity
public class Students07 implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int sid;
    private String sname;
    private String gender;
    private String major;
    private Date birthday; ...}

 ClassRoom01

@Entity
public class ClassRoom01 {

    @Id
    @GeneratedValue(generator = "cid")
    @GenericGenerator(name = "cid", strategy = "assigned")
    @Column(length = 4)
    private String cid;
    private String cname;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)  //多的一方使用急加载, 一的一方使用懒加载
    @JoinColumn(name = "CID")
    private Set<Students07> stus;  //一的一方持有多的一方的集合  ...}

5. One-to-many two-way foreign key association

The configuration of the class class and the one-way is the same, except that the class class reference is added to the student class

Students08

@Entity
public class Students08 implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int sid;
    private String sname;
    private String gender;
    private String major;
    private Date birthday;
    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)  //多的一方需要设为急加载
    @JoinColumn(name = "CID")
    private ClassRoom02 classRoom02;  //多的一方持有一的一方的引用  ...}

6. Many-to-many single foreign key association

① The corresponding relationship between students and teachers is a many-to-many relationship 

② Student table holds the collection of teacher table

③ Create an intermediate table

Students09

@Entity
public class Students09 implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int sid;
    private String sname;
    private String gender;
    private String major;
    private Date birthday;
    @ManyToMany
    @JoinTable(
            name = "teachers_students",  //中间表的名称
            joinColumns = {@JoinColumn(name = "SID")},  //中间表外键关联字段的名称
            inverseJoinColumns = {@JoinColumn(name = "TID")}  //中间表外键关联字段的名称
    )
    private Set<Teachers> teachers;  //学生持有教师的集合  ...}

Teachers

@Entity
public class Teachers {

    @Id
    @GeneratedValue(generator = "tid")
    @GenericGenerator(name = "tid", strategy = "assigned")
    @Column(length = 4)
    private String tid;  //教师的编号
    private String tname;  //教师的姓名 ...}

7. Many-to-many two-way foreign key association

Both parties hold each other's collection, involving the master and the accused, and need to transfer control to the master

@Entity
public class Teachers01 {

    @Id
    @GeneratedValue(generator = "tid")
    @GenericGenerator(name = "tid", strategy = "assigned")
    @Column(length = 4)
    private String tid;  //教师的编号
    private String tname;  //教师的姓名
    @ManyToMany(mappedBy = "teachers")  //将主控权给学生方, teachers是学生类中的属性
    private Set<Students10> stus;   ...}

The student class is the same as the test

Guess you like

Origin blog.csdn.net/ip_JL/article/details/85775517