Hibernate annotations appear to develop

Source: Internet
Author: User

an interface with @ and some description meta annotations

Annotations used in this development

@Id:

You must define a property that maps to the primary key of a database table, and an entity can have only one property mapped to the primary key

@Table

optional , usually used in conjunction with @entity, can only be labeled at the class definition of the entity , indicating the database table information for the entity

@Entity

must , name is optional, corresponds to one of the tables in the database

@GeneratedValue

Optional ,

Strategy: Represents a primary key generation strategy with AUTO,indentity,SEQUENCE , and Table 4 , respectively, for the ORM Framework to automatically select, based on the Identity field of the database generated, based on the database table The Sequence field is generated, and a primary key is generated based on an additional table. Default is AUTO

Generator: Represents the name of the primary key generator, which is usually associated with an ORM framework. For example,Hibernate can specify how to generate primary keys such as UUID

default is not written by the local key generation strategy native

@Column

Optional , which describes the detailed definition of the field in a database table

@Transient

Optional , indicating that the property is not a mapping to a field in a database table,The ORM framework ignores the property

@OneToMany

Optional ,

Onetomany Specifies a one-to-many relationship, andmappedby= " A" specifies which party to maintain the association with,Mappedby refers to the number of parties to 1 This side of the dependency property,( Note: If you do not specify who will maintain the association relationship, then the system will create an intermediate table for us )

@JoinColumn (name = "Deptno")

through The Name property of the Joincolumn specifies the names of the foreign keys deptno ( Note: If we do not pass Joincolum To specify the name of the foreign key, the system will declare a name for us )

@ManyToOne (cascade = Cascadetype. All)

Manytoone Specifies a many-to-one relationship

Cascadetype.persist: Cascade new

Cascadetype.merge: level Union and

Cascadetype.refresh: cascading refreshes

Cascadetype.remove: cascading Delete

Cascadetype.all: The above four kinds are

in the The Passanger table relationship is defined as follows:

@ManyToOne (cascade = {Cascadetype.persist,cascadetype.merge})

One-to-one instances:

One person corresponds to an ID card.

Package Cn.baby.entity;import javax.persistence.*;import java.io.serializable;/** * person * person with province certificate one on one * Created by Administrat or on 2018/1/3. */@Entity @table (name = ' person ') public class person implements serializable{    @Id    @GeneratedValue    Private int id;    @Column    private String name;    Do not initialize, initialization will cause the object to be in a temporary state, the save time throws exception    @OneToOne    //Declare a foreign key    @JoinColumn (name = "pk_id")    private card;    public int getId () {        return ID;    }    public void setId (int id) {        this.id = ID;    }    Public String GetName () {        return name;    }    public void SetName (String name) {        this.name = name;    }    Public card Getcard () {        return card;    }    public void Setcard (card) {        this.card = card;    }}

  

Package Cn.baby.entity;import javax.persistence.*;import java.io.serializable;/** * Province certificate * One to one * Created by Administrator o N 2018/1/3. */@Table (name = "card") @Entitypublic class card implements serializable{    @Id    @GeneratedValue    Private int ID;    Who maintains the table of the relationship card, save, only need to save card on line    @OneToOne (mappedby = "card", cascade = cascadetype.all) private person person    ;    public int getId () {        return ID;    }    public void setId (int id) {        this.id = ID;    }    Public Person Getperson () {        return person;    }    public void Setperson (person person) {        This.person = person;    }}

Test class

One-to-one    @Test public    void Onetoone () {person        person=new person ();        Card card=new card ();        Session session = Hibernateutil.getsession ();        Transaction Transaction = Session.begintransaction ();        Person.setname ("Brother Wei");        Card.setperson (person);        Person.setcard (card);        Session.save (card);        Transaction.commit ();    }

  One-to-many

A department corresponds to an employee.

package Cn.baby.entity;import Javax.persistence.*;import Java.io.serializable;import java.util.hashset;import java.util.set;/** * Department table * Multiple employees under one department * Created by Administrator on 20 17/12/28. */@Entity @table (name = "DEPT5") public class Dept implements Serializable {@Id @GeneratedValue private Integer de    Ptno;    @Column private String Deptname; There are multiple employees in a department that are implanted in set sets, which must be set not hashset, but the underlying implementation is his @OneToMany (Mappedby = "Dept", cascade = Cascadetype.all) privat    E set<emp> emps=new hashset<emp> ();    Public Integer Getdeptno () {return deptNo;    } public void Setdeptno (Integer deptNo) {this.deptno = DeptNo;    } public String Getdeptname () {return deptname;    } public void Setdeptname (String deptname) {this.deptname = Deptname;    } public set<emp> Getemps () {return emps;    } public void Setemps (set<emp> emps) {this.emps = Emps; }}

Package Cn.baby.entity;import Javax.persistence.*;import Java.io.serializable;import java.util.hashset;import java.util.set;/** * Employee Table * An employee corresponds to a department * Created by Administrator on 2017/12/28. */@Table (name = "EMP5") @Entitypublic class Emp implements Serializable {@Id @GeneratedValue private Integer EMPN    O    @Column private String EmpName;    An employee corresponds to a department that implants a departmental attribute @ManyToOne (cascade = cascadetype.all) @JoinColumn (name = "Deptno") private Dept Dept;  Mappedby who maintains the relationship, Mappedby specifies the property name @ManyToMany (Mappedby = "Emps", cascade = cascadetype.all) Private set<project>    Projects=new hashset<project> ();    Public set<project> getprojects () {return projects;    } public void Setprojects (set<project> projects) {this.projects = projects;    } public Integer Getempno () {return empNo;    } public void Setempno (Integer empNo) {this.empno = EmpNo;    } public String Getempname () {return empname;  }  public void Setempname (String empname) {this.empname = EmpName;    } public Dept getdept () {return Dept;    } public void Setdept (Dept Dept) {this.dept = Dept; }}

  Test class:

One-to-many    @Test public    void Onetomany () {        Dept dept=new Dept ();        Dept.setdeptname ("technical department");        EMP emp=new EMP ();        Emp.setempname ("Xiao Huang");        EMP emp1=new EMP ();        Emp1.setempname ("small Black");        Emp.setdept (dept);        Emp1.setdept (dept);        Dept.getemps (). Add (EMP);        Dept.getemps (). Add (EMP1);        Session session = Hibernateutil.getsession ();        Transaction Transaction = Session.begintransaction ();        Session.save (dept);        Transaction.commit ();    }

  Many pairs of

An employee can write multiple projects

A project can be written by multiple employees

Package Cn.baby.entity;import Javax.persistence.*;import Java.io.serializable;import java.util.hashset;import java.util.set;/** * Employee Table * An employee corresponds to a department * Created by Administrator on 2017/12/28. */@Table (name = "EMP5") @Entitypublic class Emp implements Serializable {@Id @GeneratedValue private Integer EMPN    O    @Column private String EmpName;    An employee corresponds to a department that implants a departmental attribute @ManyToOne (cascade = cascadetype.all) @JoinColumn (name = "Deptno") private Dept Dept;  Mappedby who maintains the relationship, Mappedby specifies the property name @ManyToMany (Mappedby = "Emps", cascade = cascadetype.all) Private set<project>    Projects=new hashset<project> ();    Public set<project> getprojects () {return projects;    } public void Setprojects (set<project> projects) {this.projects = projects;    } public Integer Getempno () {return empNo;    } public void Setempno (Integer empNo) {this.empno = EmpNo;    } public String Getempname () {return empname;  }  public void Setempname (String empname) {this.empname = EmpName;    } public Dept getdept () {return Dept;    } public void Setdept (Dept Dept) {this.dept = Dept; }}

Package Cn.baby.entity;import Javax.persistence.*;import Java.io.serializable;import java.util.hashset;import java.util.set;/** * Project Table * Many-to-many * Created by Administrator on 2017/12/28. */@Entity @table (name = "PROJECT5") public class Project implements Serializable {@Id @GeneratedValue private Inte    GER Prono;    @Column private String Proname; @ManyToMany (cascade = cascadetype.all)//The name of the intermediate table in the middle table this entity corresponds to the column of the intermediate table, and the other entity corresponds to the column of the intermediate table @JoinTabl E (name = "Proemp5", Joincolumns = @JoinColumn (name = "Proid"), Inversejoincolumns = @JoinColumn (    Name = "Empid")//Employee collection Private set<emp> emps=new hashset<emp> ();    Public Integer Getprono () {return prono;    } public void Setprono (Integer prono) {this.prono = Prono;    } public String Getproname () {return proname;    } public void Setproname (String proname) {this.proname = Proname; } public set<emp> Getemps () {RETurn emps;    } public void Setemps (set<emp> emps) {this.emps = Emps; }}

 Test class

  Duo to multiple    @Test public    void Manytomany () {        emp emp=new emp ();        Emp.setempname ("Xiao Huang");        EMP emp1=new EMP ();        Emp1.setempname ("small Black");        Project Project=new project ();        Project.setproname ("Easy to buy net");        Project Project1=new project ();        Project1.setproname ("Easy buy net 1");        Emp.getprojects (). Add (project);        Emp.getprojects (). Add (Project1);        Emp1.getprojects (). Add (project);        Project.getemps (). Add (EMP);        Project1.getemps (). Add (EMP);        Project.getemps (). Add (EMP1);        Session session = Hibernateutil.getsession ();        Transaction Transaction = Session.begintransaction ();        Session.save (EMP);        Session.save (EMP1);        Transaction.commit ();    }

  

Hibernate annotations appear to develop

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.