Collection mappings (less important)
1 Set
2 List (almost multiple @orderby with set)
A) @OrderBy sort
Private New Arraylist<user>(); @OneToMany (Mappedby= "group", cascade={cascadetype.all} ) @OrderBy ("Name ASC" ) public list<user> getusers () { return users; } Public void setusers (list<user> users) { this. Users = users; }
3 Map
A) @Mapkey
Private New Hashmap<integer, user>(); @OneToMany (Mappedby= "group", cascade=cascadetype.all) @MapKey (name= "id") public Map<integer, user> getusers () { return users; } Public void setusers (Map<integer, user> users) { this. Users= users; }
@Test Public void Testloadgroup () { = sessionfactory.getcurrentsession (); S.begintransaction (); Group G = (group) s.load (group). Class, 1); for (Map.entry<integer, user> entry:g.getusers (). EntrySet ()) { System.out.println (Entry.getvalue ()). GetName ()); } s.gettransaction (). commit (); }
Inheritance mappings (less important)
13 Different ways
A) a summary table single_table (a table is generated in the database)
@Entity @inheritance (Strategy=inheritancetype.single_table) @DiscriminatorColumn (name= "Discriminator", discriminatortype=discriminatortype.string) @DiscriminatorValue ("Person") Public classPerson {Private intID; PrivateString name; @Id @GeneratedValue Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; }}
@Entity @discriminatorvalue ( "student" ) public class Student extends person { private int score; public int Getscore () { score; public void SetScore (int score) { this . Score = score; } }
@Entity @discriminatorvalue ("Teacher") Public class extends Person { private String title; Public String GetTitle () { return title; } Public void Settitle (String title) { this. title = title; } }
@Test Public voidTestsave () {Student s=NewStudent (); S.setname ("S1"); S.setscore (80); Teacher T=NewTeacher (); T.setname ("T1"); T.settitle (Intermediate); Session Session=sessionfactory.opensession (); Session.begintransaction (); Session.save (s); Session.save (t); Session.gettransaction (). commit (); Session.close (); } @Test Public voidtestLoad () {testsave (); Session Session=sessionfactory.opensession (); Session.begintransaction (); Student s= (Student) session.load (Student.class, 1); System.out.println (S.getscore ()); Person P= (person) session.load (person).class, 2); System.out.println (P.getname ()); Session.gettransaction (). commit (); Session.close (); }
b) One table for each class Table_per_class
@Entity @inheritance (Strategy=inheritancetype.table_per_class) @TableGenerator (name= "T_gen", table= "T_gen_table", Pkcolumnname= "T_PK", Valuecolumnname= "T_value", Pkcolumnvalue= "PERSON_PK", InitialValue=1, Allocationsize=1 ) Public classPerson {Private intID; PrivateString name; @Id @GeneratedValue (Generator= "T_gen", strategy=generationtype.table) Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; }}
@Entity Public class extends Person { privateint score; Public int Getscore () { return score; } Public void setscore (int score) { this. score = score; } }
@Entity Public class extends Person { private String title; Public String GetTitle () { return title; } Public void Settitle (String title) { this. title = title; } }
c) Each sub-Class A table joined
The parent class is annotated @inheritance (Strategy=inheritancetype. JOINED)
@Entity @inheritance (Strategy=inheritancetype.joined) Public classPerson {Private intID; PrivateString name; @Id @GeneratedValue Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; }}
Hibernate Learning Notes _ relational Mapping _ Other