The CRUD cascade of Hibernate Association relationships

Source: Internet
Author: User

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

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.