"Learning Notes" Hibernate annotations (y2-1-9)

Source: Internet
Author: User

Hibernate annotations

1. What is an annotation
To hit other types of type metadata on a class or on a method

@ Flag

Used Annotations:

@override Method Overrides

@Test Junit Unit Tests

@Before Unit Tests

@After Unit Tests

Frequently used annotations in hibernate

[EMAIL protected] Declare a class as a persisted class
[email protected] declares the identity property of a persisted class
[Email protected] Defines a build policy that identifies the value of a property, which defaults to native
[Email protected] mapping properties to classes
[Email protected] Persistent class map specified table
[email protected] ignore these properties

Hibernate can use a small configuration file to correlate the mappings, or you can use annotations.

1. One-to-one correlation

A man human has a social security number. Identity a social security number belongs to only one person

Entity classes are as follows

@Entity @table Public classHuman {PrivateInteger hid; @ColumnPrivateString Hname; Privateidentity Identity; @OneToOne @JoinColumn (Name= "IID")     PublicIdentity getidentity () {returnidentity; }     Public voidsetidentity (Identity identity) { This. Identity =identity; } @Id @GeneratedValue PublicInteger Gethid () {returnhid; }     Public voidSethid (Integer hid) { This. HID =hid; }     PublicString Gethname () {returnHname; }     Public voidsethname (String hname) { This. Hname =Hname; }}
@Entity @table Public classIdentity {PrivateInteger IID; PrivateString Inum; PrivateHuman Human; @OneToOne (Mappedby= "Identity", Cascade =Cascadetype.all) PublicHuman Gethuman () {returnHuman; }     Public voidSethuman (Human Human) { This. Human =Human; } @Id @GeneratedValue PublicInteger getiid () {returnIID; }     Public voidsetiid (Integer iid) { This. IID =IID; }     PublicString Getinum () {returnInum; }     Public voidSetinum (String inum) { This. Inum =Inum; }}

Test as follows

@Test      Public void Insert () {        new  Identity ();        Id.setinum ("1231231212123");         New Human ();        Human.sethname ("Ghost");        Human.setidentity (ID);        Id.sethuman (human);        Session.save (human);        Session.save (ID);    }

2. Bidirectional one-to-many association

A Department dept has multiple employees EMP an employee can only belong to one department

Entity classes are as follows

@Entity @table (name= "DEPT3") Public classDept {@Id @GeneratedValuePrivateInteger DeptID; PrivateString dname; @OneToMany (Mappedby= "Dept", Cascade ={Cascadetype.all})Privateset<emp> Emps =NewHashset<emp>();  PublicInteger Getdeptid () {returnDeptID; }     Public voidSetdeptid (Integer deptid) { This. DeptID =DeptID; }     PublicString Getdname () {returndname; }     Public voidsetdname (String dname) { This. dname =dname; }     PublicSet<emp>Getemps () {returnEmps; }     Public voidSetemps (set<emp>emps) {         This. Emps =Emps; }}
@Entity @table (name= "Emp3") Public classEmp {@Id @GeneratedValuePrivateInteger Eid; PrivateString ename; @ManyToOne @JoinColumn (Name= "DeptID")    PrivateDept Dept;  PublicInteger Geteid () {returnEid; }     Public voidSeteid (Integer Eid) { This. Eid =Eid; }     PublicString Getename () {returnename; }     Public voidsetename (String ename) { This. ename =ename; }     PublicDept getdept () {returnDept; }     Public voidsetdept (Dept Dept) { This. Dept =Dept; }}

Test as follows

@Test      Public void Insert () {        new  Dept ();        Dept.setdname ("Ghost Animal Ministry");         New Emp ();        Emp1.setename ("Destiny");        Emp1.setdept (dept);         New Emp ();        Emp2.setename ("Swatch");        Emp2.setdept (dept);        SetNew hashset<emp>();        Set.add (EMP1);        Set.add (EMP2);        Dept.setemps (set);        Session.save (dept);    }

3. Many-to-many associations

One player player can play multiple game games a game can be played by multiple players

Entity classes are as follows

@Entity @table Public classGame {@Id @GeneratedValuePrivateInteger GID; PrivateString Gname; @ManyToMany (Cascade=cascadetype.all) @JoinTable (name= "", Joincolumns= {@JoinColumn (name = "GID")}, Inversejoincolumns= {@JoinColumn (name = "pid"))})    PrivateSet<player> players =NewHashset<player>();  PublicInteger Getgid () {returnGID; }     Public voidSetGid (Integer gid) { This. GID =GID; }     PublicString Getgname () {returnGname; }     Public voidsetgname (String gname) { This. Gname =Gname; }     PublicSet<player>getplayers () {returnplayers; }     Public voidSetplayers (set<player>players) {         This. Players =players; }}
@Entity @table Public classPlayer {@Id @GeneratedValuePrivateInteger pid; PrivateString pname; @ManyToMany (Cascade= Cascadetype.all, Mappedby = "Players")    Privateset<game> games =NewHashset<game>();  PublicInteger getpid () {returnpid; }     Public voidsetpid (Integer pid) { This. PID =pid; }     PublicString Getpname () {returnpname; }     Public voidsetpname (String pname) { This. pname =pname; }     PublicSet<game>Getgames () {returnGames ; }     Public voidSetgames (set<game>Games ) {         This. games =Games ; }}

Test as follows

@Test Public voidInsert () {Game G1=NewGame (); G1.setgname ("My class."); Game G2=NewGame (); G2.setgname ("The Heart of Thanksgiving"); Player P1=NewPlayer (); P1.setpname ("Chara"); Player P2=NewPlayer (); P2.setpname ("Frisk");        G1.getplayers (). Add (p1);        G1.getplayers (). Add (p2);        G2.getplayers (). Add (p1);        G2.getplayers (). Add (p2);        Session.save (G1);    Session.save (G2); }

"Learning Notes" Hibernate annotations (y2-1-9)

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.