Hibernate in the inverse and cascade
1.cascade ="..."?
cascade is not a property of many-to-many relationship must have it just so that we insert or delete at the time like some more convenient, as long as the source of the cascade insert or delete, all the relationship between the cascade will be its own dynamic insert or delete. Is able to correct for cascade, unsaved-value is a very important property.
Hibernate through the property to determine an object should save or update, if the object id of the unsaved-value yes, then explain this object is not persistence object to save (insert); If unsaved-value non-id, then explain this object are persistence object (the database already exists), as long as the update on the list. Ways saveOrUpdate this mechanism is used.
2.inverse = "ture"?
inverse property is false by default, that the relationship at both ends to maintain the relationship. This means that if there is a Student, Teacher and TeacherStudent Table, Student and Teacher are many-to-many to many relationship, this relationship from the table to TeacherStudent performance. So when TeacherStudent insert or delete records in the table to maintain a relationship? Using hibernate, we will not be displayed on the table to do TeacherStudent operation.
TeacherStudent operation of hibernate are helping us to do. hibernate HBM files look that is specified in the "Who" maintaining relations at insert or delete the "who" when the hair on the relationship between the Office of the operation table. The premise that "who" this object has to know this relationship, that is to say relations between the other end of the object has been set or add to the "who" come in this object. As mentioned above the default inverse are false, is at both ends of the relationship between the maintenance of relations of one of the Agency will operate any hair on the operation table Department table. When in a relationship, such as Student of the bag or set using the inverse = "true" when it is on behalf of client relations are maintained by another (Teacher). That is to say when the insert Student will not operate TeacherStudent table, even if the Student had been aware of the relationship. Teacher only when insert or delete when hair on the relationship between the Office of the operation table.
So, when relations between the two are inverse = "true" is wrong, it will not lead to any operation Department of the hair on the relationship between the operating table. When both ends are the inverse = "false" or the default value is in the code shown in the maintenance of relations is also wrong and will result in the relationship table to insert the relationship between the two. At one-to-many inverse relationship is even more meaningful. In the many-to-many, in which end inverse = "true" effect is almost (in efficiency). However, in one-to-many, if you want a party to the maintenance of the relationship, it will cause the insert or delete at "one" side when the go update "many" side of each with the "one" has the relationship between the target object.
And if the "many" aspects of the maintenance of the relationship between the update there will be no operation, because the relationship is at a multi-object, and directly insert or delete multiple objects on the list. Then of course, have to traverse, "many" side of each shows the operation of an object changes in repair relations embodied in the DB. However that still allow the "many" side to safeguard the relationship between a number of more intuitive.
And the inverse has 3.cascade What's the difference?
Can interpret it this way, cascade relations are defined at both ends of the object to the object relations cascade; and are the definition of inverse relations and object relations cascade. At one-to-many mapping, one party to be set up <set (inverse="false") casecade="save-update" />, many of the party to be set up <many-to-one inverse = true (casecade = "none ") />
Me to do this are the use cases. Test cascade and inverse. The results of the analysis as well as specific hibernate sql are relatively easy to understand look.
I hope it is not very clear to see, to deepen the impression.
Scholarship boy , if there is something wrong, please point it out, thank you! ~
Test environment: Eclispe, using breakpoint testing.
Database are SQLServer2000.
Area.hbm.xml
<? xml version = "1.0" encoding = "utf-8"?>
<! DOCTYPE hibernate-mapping PUBLIC "- / / Hibernate / Hibernate Mapping DTD 2.0 / / EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd";>
<hibernate-mapping>
<class name="net.villion.model.test.Area" table="r_area">
<id column="ID" type="long" name = "id" length="10" unsaved-value="null">
<generator class = "native"/>
</ id>
<property column="name" name="name" type="string"/>
<many-to-one name="state" cascade="all">
<column name="stateid" index="index_area_state"/>
</ many-to-one>
</ class>
</ hibernate-mapping>
State.hbm.xml
<? xml version = "1.0" encoding = "utf-8"?>
<! DOCTYPE hibernate-mapping PUBLIC "- / / Hibernate / Hibernate Mapping DTD 2.0 / / EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd";>
<hibernate-mapping>
<class name="net.villion.model.test.State" table="r_state">
<id column="ID" type="long" name = "id" length="10" unsaved-value="null">
<generator class = "native"/>
</ id>
<property column="name" name="name" type="string"/>
<set name="areas" lazy="true" cascade="all" inverse="false">
<key column="stateid"/>
<one-to-many/>
</ set>
</ class>
</ hibernate-mapping>
We look inverse, cascade cascade settings.
When State.hbm.xml inverse = "false" cascade = "all"
/ / Add
/ / code
StateDAO sdao = (StateDAO) ctx.getBean ( "stateDAO");
AreaDAO adao = (AreaDAO) ctx.getBean ( "areaDAO");
Area a3 = new Area ();
a3.setName ( "NewYork");
Area a4 = new Area ();
a4.setName ( "Frolida");
State s2 = new State ();
s2.setName ( "America");
Set set = new HashSet ();
set.add (a3);
set.add (a4);
s2.setAreas (set);
sdao.saveObject (s2);
/ / sql:
Hibernate: insert into r_state (name) values (?)
Hibernate: insert into r_area (name, stateid) values (?,?)
Hibernate: insert into r_area (name, stateid) values (?,?)
Hibernate: update r_area set stateid =? Where
Hibernate: update r_area set stateid =? Where
/ / When State.hbm.xml inverse = "false" cascade = "none" (note that we operate are State)
/ / sql: insert into r_state (name) values (?)
Hibernate: update r_area set stateid =? Where
org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance
At this time when the update area because of area, not so can not find error cascade insert. So this set will be updated or Area of stateid
Then above that we have to look at when the settings change the sql changes.
Illustrated above, cascade = "all" when cascade insertion, and none of the time just to maintain the relationship between (inverse = "false").
Delete the above two situations are not the same, but a truth
cascade = "all" when the two tables of data are concatenated deleted.
The cascade = "none" when
Hibernate: update r_area set stateid = null where stateid =?
Hibernate: delete from r_state where it is only to maintain the relationship.
When the above inverse = "true" when cascade = "none" when
sql: insert into r_state (name) values (?)
Insert only one State, not to insert the Area Table.
Delete when
Hibernate: delete from r_state where
org.springframework.dao.DataIntegrityViolationException: (Hibernate operation): data integrity violated by SQL''; nested exception is com.jnetdirect.jsql.u: DELETE statement with COLUMN REFERENCE constraint 'FKC7F1003A8EEBF16C' conflict. The conflict occurred in database 'hd', table 'r_area', column 'stateid'.
com.jnetdirect.jsql.u: DELETE statement with COLUMN REFERENCE constraint 'FKC7F1003A8EEBF16C' conflict
Maintenance of the database because there is no relationship between the error caused by foreign key conflict.
inverse = "true" when cascade = "all" when
sql:
Hibernate: insert into r_state (name) values (?)
Hibernate: insert into r_area (name, stateid) values (?,?)
Hibernate: insert into r_area (name, stateid) values (?,?)
Insert the full cascade, but r_area table stateid are null.
When delete.
Hibernate: delete from r_area where
Hibernate: delete from r_area where
Hibernate: delete from r_state where
Cascade delete.
So to sum up, cascade cascade are the extent of, and inverse = "false | ture" refers to whether to maintain the relationship between the two entities. (Two tables of the foreign key).
Related Posts of Hibernate in the inverse and cascade
-
javascript learning object-oriented programming (basic)
-------------------------------------------------- ------------------------------- Copyright Note: This articles is to use <<JavaScript Advanced programming >> (Nicholas C. Zakas, and Cao Li, Zhang Xin, et al) study summed up the process. ...
-
Rails January of dynamic
More than a month before, Rails 2.2.2 released at the same time, the official immediately issued a statement, saying that Rails 2.3 is under development. See the news, while lamenting on the Rails Core Team, the progress is so compact, at the same ti ...
-
hibernate in the get method and the difference between load method
hibernate in the get method and the load method of the fundamental difference is: If you use the load method, hibernate consider the corresponding object id (database records) are in the database must exist, so it can be assured that the use, it can ...
-
Great Design
Understand the principle with the practice, details the relationship between thought, we'll go have selectively study the details, we must work on the details because the details are important to you to use the technical details of implementation ...
-
I heard good Ruby and Rails site
1. Ruby Inside is said to be the best news station, the subscriber number 16xxx [img] http://www.rubyinside.com/simg/logo.gif "alt =" [/ img] http://www.rubyinside.com/ 2. Rails Inside is also a news station, the subscriber number of 3xxx [ ...
-
Hibernate II Study Notes
11. Many-to-many Of many that can be converted to two one-to-many many-to-many data only from one end of the maintenance, if at both ends of maintenance, will be reported to the conflict between form the primary key of the error message. Many-to-many quer
-
To a generic hibernate example DAO
Reprint: http://blog.csdn.net/dingx
-
How the primary key agent-hibernate
Existing Table A, B Statement: A, B field id-based keys. A: [id, name] B: [id, Aid, title] B and A's set up a many-to-one relationship because of A, B two tables are the primary key id field so add B when Hibernate will automatically retrieve id ...
-
Hibernate annotation using notebook
These are the basic common comment, the following are examples to illustrate TU is a two-way as long as if the increase in Largess













Leave a Reply