Cascade Cascade, only affects crud cud and does not affect reads. Do not set cascading, from a party can read one side, set up a cascade, from one side, the default can not read a lot of the party.
If there is an association between two objects, whether it's a one-to-many, one-way, or two-way, if from a can be directed to B:
A--->b
By default, the save of a does not affect B unless you set cascade. If a--->b--->c, from a can navigate to B,b can navigate to C, and also cascade on B, the operation of a can affect C, but the cascade is not necessary, it just makes programming a little easier. You can manually first save C, then save B, and then save a.
Example: Group class:
Packagecom.oracle.hibernate;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;Importjavax.persistence.Id;Importjavax.persistence.Table; @Entity @table (name= "T_group")//Group is a key word for MySQL, change the name Public classGroup {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; } }
User class:
Packagecom.oracle.hibernate;ImportJavax.persistence.CascadeType;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;Importjavax.persistence.Id;ImportJavax.persistence.JoinColumn;ImportJavax.persistence.ManyToOne; @Entity Public classUser {Private intID; PrivateString name; PrivateGroup Group; //Many-to-one, cascading.
@ManyToOne (Cascade={cascadetype.all}) @JoinColumn (name= "GroupId")//Specify a foreign key name 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:
Packagecom.oracle.hibernate;Importorg.hibernate.HibernateException;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.cfg.AnnotationConfiguration;ImportOrg.hibernate.tool.hbm2ddl.SchemaExport;ImportOrg.junit.AfterClass;ImportOrg.junit.BeforeClass; Public classTest {Private StaticSessionfactory SF =NULL; @BeforeClass Public Static voidBeforeclass () {Try { NewSchemaexport (NewAnnotationconfiguration (). Configure ()). Create (false,true); SF=Newannotationconfiguration (). Configure (). Buildsessionfactory (); } Catch(hibernateexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }} @org. junit.test Public voidTestschemaexport () {NewSchemaexport (NewAnnotationconfiguration (). Configure ()). Create (false,true); } @AfterClass Public Static voidAfterclass () {sf.close (); } @org. junit.test Public voidTestsave () {Session s=sf.getcurrentsession (); S.begintransaction (); User User=NewUser (); User.setname ("U1"); Group g=NewGroup (); G.setname ("G1"); User.setgroup (g); //S.save (g); The association variable is not automatically saved by default.//once the cascade is set, the user's group can be saved directly by savings.save (user); S.gettransaction (). commit (); }}
Database:
The CRUD cascade of Hibernate Association relationships