Because of the time, I am here to test the environment is a one-to-many relationship in the use of the annotated way of cascading, there are a lot of posts on the Internet, I have seen, but, I would like to summarize it, it felt that the cascade is unidirectional, not bidirectional, meaning that, we set two classes of the relationship between objects, Always set in one side is very specific, on the other side to set a mappedby, but if you want both sides can be deleted, or when generated, must be set on both sides cascade= Cascadetype.all has the effect, the following is the test code, the test sample is to refer to the horse soldier's video to do,
PackageCom.jll.model;ImportJava.util.HashSet;ImportJava.util.Set;ImportJavax.persistence.CascadeType;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;Importjavax.persistence.Id;ImportJavax.persistence.JoinColumn;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; } PublicString GetName () {returnname; } @OneToMany (Mappedby= "Group", cascade=Cascadetype.all) PublicSet<user>getusers () {returnusers; } Public voidSetId (intID) { This. ID =ID; } Public voidsetName (String name) { This. Name =name; } Public voidSetusers (set<user>users) { This. Users =users; }}
Now there are cascade=cascadetype.all here. There are also on the other side of the associated class,
PackageCom.jll.model;ImportJavax.persistence.CascadeType;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;Importjavax.persistence.Id;ImportJavax.persistence.JoinColumn;ImportJavax.persistence.ManyToOne;Importjavax.persistence.Table; @Entity @table (name= "T_user") Public classUser {Private intID; PrivateString name; PrivateGroup Group; @ManyToOne (Cascade=cascadetype.all) @JoinColumn (name= "GroupId") PublicGroup Getgroup () {returnGroup; } @Id @GeneratedValue Public intgetId () {returnID; } PublicString GetName () {returnname; } Public voidSetgroup (Group group) { This. Group =Group; } Public voidSetId (intID) { This. ID =ID; } Public voidsetName (String name) { This. Name =name; } PublicString toString () {return This. GetName () + "---" + This. GetId () + "---" + This. Getgroup (). GetId (); }}
Test code:
PackageCom.jll.model;ImportJava.util.HashSet;ImportJava.util.Iterator;ImportJava.util.Set;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;ImportOrg.hibernate.boot.registry.StandardServiceRegistryBuilder;Importorg.hibernate.cfg.Configuration;ImportOrg.hibernate.tool.hbm2ddl.SchemaExport;ImportOrg.junit.AfterClass;ImportOrg.junit.BeforeClass;ImportOrg.junit.Ignore;Importorg.junit.Test; Public classTestcoreapi {Private StaticSessionfactory sf=NULL; Private StaticConfiguration Configuration =NewConfiguration (). Configure (); @BeforeClass Public Static voidBeforeclass () {Standardserviceregistrybuilder builder=NewStandardserviceregistrybuilder (). Applysettings (Configuration.getproperties ()); SF=configuration.buildsessionfactory (Builder.build ()); } @AfterClass Public Static voidAfterclass () {sf.close (); } @Test Public voidtestrelationship () {schemaexport se=Newschemaexport (configuration); Se.create (true,true); Session Session=sf.getcurrentsession (); Group g=NewGroup (); G.setname ("Group1"); User U1=NewUser (); User U2=NewUser (); /*U1.setgroup (g); U2.setgroup (g);*/U1.setname ("U1"); U2.setname ("U2"); Set<User> users =NewHashset<user>(); Users.add (U1); Users.add (U2); G.setusers (users); Session.begintransaction (); Session.save (g); Session.gettransaction (). commit (); }
The resulting SQL statement is as follows:
Alter TableT_userDrop Foreign KeyFk_7ktm6l2qkykpqrf6oq01ys8wyDrop Table if existsT_groupDrop Table if existsT_userCreate TableT_group (IDinteger not NULLauto_increment, namevarchar(255), Primary Key(ID))Create TableT_user (IDinteger not NULLauto_increment, namevarchar(255), GroupIdinteger, Primary Key(ID))Alter TableT_userAdd constraintFk_7ktm6l2qkykpqrf6oq01ys8wyForeign Key(groupId)Referencest_group (ID) Hibernate:Insert intot_group (name)Values (?) Hibernate:Insert intoT_user (groupId, name)Values (?, ?) Hibernate:Insert intoT_user (groupId, name)Values (?, ?)
If the group class is not cascaded, the resulting statement is as follows:
Alter TableT_userDrop Foreign KeyFk_7ktm6l2qkykpqrf6oq01ys8wyDrop Table if existsT_groupDrop Table if existsT_userCreate TableT_group (IDinteger not NULLauto_increment, namevarchar(255), Primary Key(ID))Create TableT_user (IDinteger not NULLauto_increment, namevarchar(255), GroupIdinteger, Primary Key(ID))Alter TableT_userAdd constraintFk_7ktm6l2qkykpqrf6oq01ys8wyForeign Key(groupId)Referencest_group (ID) Hibernate:Insert intot_group (name)Values (?)
This is only inserted once, and the top one is inserted three times, so I guess the cascade is one-way, not bidirectional, if you want both sides can be crud, then the associated class to add Cascade=cascadetype.all, I also have to delete the test, delete must first find out , and then to cascade Delete, the conclusion is the same as the above experiment, here do not post code.
Hibernate Cascade=cascadetype.all