1.save one-to-many bidirectional
Packagecom.bjsxt.hibernate;ImportJava.util.HashSet;ImportJava.util.Set;ImportJavax.persistence.CascadeType;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;Importjavax.persistence.Id;ImportJavax.persistence.OneToMany;Importjavax.persistence.Table; @Entity @table (name= "T_group") Public classGroup {Private intID; PrivateString name; Privateset<user> users =NewHashset<user>(); @Id @GeneratedValue Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } @OneToMany (Mappedby= "Group"}//As long as the additions and deletions are automatically saved) PublicSet<user>getusers () {returnusers; } Public voidSetusers (set<user>users) { This. Users =users; }}
Group.java
Packagecom.bjsxt.hibernate;ImportJavax.persistence.CascadeType;Importjavax.persistence.Entity;ImportJavax.persistence.FetchType;ImportJavax.persistence.GeneratedValue;Importjavax.persistence.Id;ImportJavax.persistence.ManyToOne;Importjavax.persistence.Table; @Entity @table (name= "T_user") Public classUser {Private intID; PrivateString name; PrivateGroup Group; @ManyToOne (Cascade={Cascadetype.all}) PublicGroup Getgroup () {returnGroup; } Public voidSetgroup (Group group) { This. Group =Group; } @Id @GeneratedValue Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; }}
Test
@Test Public void Testsaveuser () { new User (); U.setname ("U1"); New Group (); G.setname ("G1"); U.setgroup (g); = sessionfactory.getcurrentsession (); S.begintransaction (); // S.save (g); If the Csadtype is set, the G s.save (U) can not be saved first; S.gettransaction (). commit (); }
Cascade Tube Adding and removing to fetch tube read
From one side of the other, the information of the less party will be automatically taken out.
The value to be set for the Fatch property to be Fatchtype.eager can not be taken from the lesser side.
The one who saves less
such as the deposit group
Group.java others do not change
Packagecom.bjsxt.hibernate;ImportJava.util.HashSet;ImportJava.util.Set;ImportJavax.persistence.CascadeType;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;Importjavax.persistence.Id;ImportJavax.persistence.OneToMany;Importjavax.persistence.Table; @Entity @table (name= "T_group") Public classGroup {Private intID; PrivateString name; Privateset<user> users =NewHashset<user>(); @Id @GeneratedValue Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } @OneToMany (Mappedby= "Group", Cascade={Cascadetype.all}) PublicSet<user>getusers () {returnusers; } Public voidSetusers (set<user>users) { This. Users =users; }}
Test
@Test Public voidTestsavegroup () {User U1=NewUser (); U1.setname ("U1"); User U2=NewUser (); U2.setname ("U2"); Group g=NewGroup (); G.setname ("G1"); G.getusers (). Add (U1); G.getusers (). Add (U2); U1.setgroup (g); You must manually set the user ID number otherwise the database is null u2.setgroup (g); Session s=sessionfactory.getcurrentsession (); S.begintransaction (); //S.save (g);S.save (g); S.gettransaction (). commit (); }
When you get to user, the group is removed by default (1 that side)
@Test publicvoid Testgetuser () { testsavegroup (); = sessionfactory.getcurrentsession (); S.begintransaction (); = (user) s.get (user). Class, 1); S.gettransaction (). commit (); System.out.println (U.getgroup (). GetName ()); }
Even if the removal point Cascade will be taken away
Does the test take a lot of places out of the place? No
Hibernate does not take out fewer of its own when setting up more than one side Fatch as Lazay.
@Test Public void Testgetuser () { testsavegroup (); = sessionfactory.getcurrentsession (); S.begintransaction (); = (user) s.get (user). Class, 1); It's okay to put it here. S.gettransaction () of the one who will take it out . commit (); System.out.println (U.getgroup (). GetName ()); Cannot be taken out }
If it's in one place, because Fatch for eager has been automatically detected, no need to deal with the database.
Test the Load method
@Test Public void Testloaduser () { testsavegroup (); = sessionfactory.getcurrentsession (); S.begintransaction (); = (user) s.load (user). Class, 1);//Returns the proxy object System.out.println (U.getgroup (). GetName ()); This sentence emits two select s.gettransaction (). commit (); }
If Fatch is set to lazy when looking for this object, it will not automatically go to the side of the association, but when looking for the other side, if the Fatch is eager, the SELECT statement will be issued.
Update
@Test Public voidTestupdateuser () {testsavegroup (); Session s=sessionfactory.getcurrentsession (); S.begintransaction (); User u= (user) s.get (user).class, 1); If user is set to fetch as lazy then only take out user if eager then group also takes out S.gettransaction (). commit (); Into detach state u.setname ("User"); Change the name cache to have the object U.getgroup (). SetName ("Group"); Get group visible Here's the fetch is eager Session S2=sessionfactory.getcurrentsession (); S2.begintransaction (); S2.update (U); You changed the U s2.gettransaction (). commit (); Submit}
Delete
Delete Many of the party
@Test public void Testdeleteuser () {testsavegroup (); Session s = sessionfactory.getcurrentsession (); S.begintransaction (); // user u = (User) s.load (User.class, 1); The contents of the comment first, this pair of corresponding group is set to null and then removed to prevent the deletion of user caused by deleting the group to remove the additional user // u.setgroup (null); You can also use the HQL statement as shown below // s.delete (U); You can also set cascade to non-delete s.createquery ("Delete from User u where u.id = 1"
Delete the less party
Generally speaking
Delete the less party
@Test Public voidTestdeletegroup () {testsavegroup (); Session s=sessionfactory.getcurrentsession (); S.begintransaction (); //User U = (user) s.load (user.class, 1); //u.setgroup (NULL); //s.delete (U); This will make the user without groupid.Group G = (group) s.load (group).class, 1); S.delete (g); This causes the group to be deleted and the user whose ID corresponds to the//s.createquery ("Delete from User u where u.id = 1"). Executeupdate (); Using the HQL statements.gettransaction (). commit (); }
Hibernate Re-contact CRUD