hibernate cascade delete encountered problems and solutions

2010-08-28  来源:本站原创  分类:Java  人气:215 

We know that in dealing with hibernate-many of the problems, we generally in the configuration file Zhong stated below their relations and operations Rules .

For example, when doing Web page, the article will list owners and Bo is the one to many relationship. I use to achieve their cascading hiberbnate relationship profile as follows

Blogger Userinfo.hbm.xml code:

<hibernate-mapping>
<!--  Specifies the class to the table mapping   -->
    <class name="test.pojo.Userinfo" table="userinfo">
     <!-- Primary key id of the generating mechanism  :native Automatically generated according to the database, mysql is the growth mode    -->
     <id name="id" column="id">
        <generator></generator>
     </id>
     <!-- Set each property Userinfo class to  userinfo The corresponding column in the table mappings    -->
     <property name="name" column="name" />
     <property name="psw" column="psw"/>
     <property name="signature" column="signature"/>
     <property name="sex" column="sex"/>
     <property name="age" column="age"/>
      <property name="birthday" column="birthday"/>
     <property name="qq" column="qq"/>
     <property name="e_mail" column="e_mail"/>
     <property name="phone" column="phone"/>
       <property name="occupation" column="occupation"/>
     <property name="score" column="score"/>
     <property name="graduateSchool" column="graduateSchool"/>
     <property name="country" column="country"/>
     <property name="province" column="province"/>
     <property name="head_path" column="head_path"/>
       <!--save-update  -->
     <!--  Mapping Userinfo object and  Article Object relationship to pair more  article By way of mapping  id The sort order, and delay-load  -->
     <bag name="articles" table="article" order-by="id asc" lazy="false" cascade="all-delete-orphan" >
        <key column="userid"></key>
        <one-to-many></one-to-many>
     </bag>
       </class>
</hibernate-mapping>

In fact, a key is

<bag name="articles" table="article" order-by="id asc" lazy="false" cascade="all-delete-orphan" >
        <key column="userid"></key>
        <one-to-many></one-to-many>
     </bag>    

This is a cascade realization of the object and the object relations

Articles Article.hbm.xml code:

<hibernate-mapping>
    <!-- Specifies the class to the table mapping    -->
      <class name="test.pojo.Article" table="article">
    <!-- Primary key id of the generating mechanism  :native Automatically generated according to the data, mysql is the growth mode    -->
      <id name="id" column="id">
       <generator></generator>
      </id>
    <!-- Set each property Article class to  article The corresponding column in the table mappings    -->
      <property name="title" column="title"></property>
      <property name="content" column="content"></property>
      <property name="publishTime" column="publishTime"></property>
      <property name="published" column="published"></property>
      <property name="clickCount" column="clickCount"></property>
      <property name="type" column="type"></property>

      <!-- Set the Article object and  Userinfo The object of a many-to-one relationship   -->

 <many-to-one name="user" fetch="select" cascade="delete" outer-join="true">
      <column name="userid"/>
      </many-to-one>

     </class>
  </hibernate-mapping>

The key section is

<many-to-one name="user" fetch="select" cascade="delete" outer-join="true">
      <column name="userid"/>
      </many-to-one>

This was the test "by", "change", "Charles" is achievable, but delete can not.

Now, I want to delete a blogger code:

Userinfo temuser=userDao.getUserinfoById(4);
  userDao.deleteUser(temuser);

Will report the following error:

Hibernate:
    select
        userinfo0_.id as id0_0_,
        userinfo0_.name as name0_0_,
        userinfo0_.psw as psw0_0_,
        userinfo0_.signature as signature0_0_,
        userinfo0_.sex as sex0_0_,
        userinfo0_.age as age0_0_,
        userinfo0_.birthday as birthday0_0_,
        userinfo0_.qq as qq0_0_,
        userinfo0_.e_mail as e9_0_0_,
        userinfo0_.phone as phone0_0_,
        userinfo0_.occupation as occupation0_0_,
        userinfo0_.score as score0_0_,
        userinfo0_.graduateSchool as graduat13_0_0_,
        userinfo0_.country as country0_0_,
        userinfo0_.province as province0_0_,
        userinfo0_.head_path as head16_0_0_
    from
        userinfo userinfo0_
    where
        userinfo0_.id=?
Hibernate:
    select
        articles0_.userid as userid0_1_,
        articles0_.id as id1_,
        articles0_.id as id1_0_,
        articles0_.title as title1_0_,
        articles0_.content as content1_0_,
        articles0_.publishTime as publishT4_1_0_,
        articles0_.published as published1_0_,
        articles0_.clickCount as clickCount1_0_,
        articles0_.type as type1_0_,
        articles0_.userid as userid1_0_
    from
        article articles0_
    where
        articles0_.userid=?
    order by
        articles0_.id asc
 Query user ' xiang ' success  !
Hibernate:
    update
        article
    set
        userid=null
    where
        userid=?
1953 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 22001
1953 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Data truncation: Column was set to data type implicit default; NULL supplied for NOT NULL column 'userid' at row 1
1953 [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.DataException: Could not execute JDBC batch update
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:102)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:262)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:180)
        at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
        at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
        at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
        at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
        at test.DAO.UserinfoDAO.deleteUser(UserinfoDAO.java:70)
        at test.test.TestUserinfo.main(TestUserinfo.java:25)
Caused by: java.sql.BatchUpdateException: Data truncation: Column was set to data type implicit default; NULL supplied for NOT NULL column 'userid' at row 1
        at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1213)
        at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:912)
        at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
        ... 9 more

Why is this?

We first look at its implementation of the sql statement, why it should update the article list?

In fact the inverse hibernate with a great relationship, and now look at the inverse:

The following is the interpretation of Baidu Encyclopedia of the inverse

Wrote,

Hibernate in the inverse relationship in the table mapping is often applied
inverse value of two, "true" and "false". inverse = "false" is the default value, if set to true change of state, said object is not synchronized to the database; set to false is the opposite;
inverse role: in hibernate is determined by inverse set is going to protect tables and relationships between tables.
We say that the establishment of improper inverse will lead to poor performance, he actually was inverse set up properly, will have to repeat the SQL statements and even unwanted result JDBC exception of the throw. This is the kind of relationship in the entity must establish areas of concern. In general, inverse = true is recommended, two-way associated with the two sides are set to inverse = false, then both sides will lead to a relationship with repeat update. But if both sides set inverse = true, then the relationship between the two sides did not maintain updated, this is not work, fortunately in the end-to-many: many-to-one by default is the inverse = false, to avoid the generation of this error . However-many would not have the default settings, and so many people at both ends of often-many using inverse = true, Results lead to the fundamental connection table of the data is not recorded, because they have no responsibility to maintain double-points relationship. Therefore, the best two-way set association is one end inverse = true, one end inverse = false. General inverse = false will be placed over one end, then the question was raised as a, many-to-many on both sides are many, inverse in the end go? In fact hibernate many to many relationship is also established separate them into two-to-many relationship in the middle connected to a connection table. Therefore, there is one to many common relationships, can be said: one to many is an essential component of many to many.

Where there is still a difference between when inverse cascade and

Wrote,

cascade is defined in relation to the target object at both ends of the cascade relations; and the definition of the inverse cascade relations and object relations.
inverse only set + one-to-many (or many-to-many) valid for many-to-one, one-to-one invalid. cascade on the relationship between markers are effective.
inverse of the collection object as a whole work, cascade to the collection object in an element of a work, if the set is empty, then the cascade will not lead to association operations.

In binernate also written in the document

Wrote,

Very Important Note: If the <key> column of a <one-to-many> association is declared NOT NULL, Hibernate may cause constraint violations when it creates or updates the association. To prevent this problem, you must use a bidirectional association with the many valued end (the set or bag) marked as inverse = "true".

Therefore to solve the cascade of "additions and deletions to change search" issue, should be "one to many" relationship in the "party" setting inverse = true;

Such as:

<bag name="articles" table="article" order-by="id asc" lazy="false" cascade="all-delete-orphan" inverse="true">
        <key column="userid"></key>
        <one-to-many></one-to-many>
     </bag>

That's all.

相关文章
  • hibernate cascade delete encountered problems and solutions 2010-08-28

    We know that in dealing with hibernate-many of the problems, we generally in the configuration file Zhong stated below their relations and operations Rules . For example, when doing Web page, the article will list owners and Bo is the one to many rel

  • After reinstalling the system and the android eclipse project encountered problems and solutions 2010-07-28

    So deeply, reloading a bit computer, the results came back to Gaga classes will not work. Encounters are varied, one by one solution, have set out for reference. 1 eclipse can not start. --- Do not forget to install JDK 2 Bahrain the JDK or can not s

  • hibernate annotation-to-many delete cascade 2011-08-15

    hibernate annotation-to-many cascade delete, the problem should be troubled by a lot of people, and I also was troubled for a long time, why not help us hibernate cascade delete it. Today finally solve this problem, the following example to everyone,

  • hibernate cascade problem 2010-04-21

    hibernate many to one operation, remove the child table, always delete the main table or update the main table. Very annoying, it seems to hibernate may be good time to play okay. http://wuxj888.javaeye.com/blog/140532 Thank yongganzhe response, this

  • Jforum secondary development problems and solutions encountered 2010-05-28

    Jforum secondary development problems and solutions encountered author: Hua-Feng Deng date: 2010-4-26 Company: JAGO Software Copyright: Copyright (c) 2010 Error message 1: 11:53:24,781 ERROR [ExceptionWriter] net.jforum.exceptions.ForumException: jav

  • hibernate one-to-many (one to many) cascade delete 2009-10-17

    I said here is one-to-many one-way cascade delete, and other consequential amendments to the need to cascade to cascade Item to Order and as an example Class 1 bean public class Order implements java.io.Serializable ( private Set items = new HashSet

  • Hibernate many-to-many delete problem 2009-04-09

    Hibernate many-to-many, many examples, but one look carefully, most of them are saved, delete the small talk, but there are still many problems, so has the need for a simple test, here we come to a simple set up many-to-many relationship Teacher and

  • Oracle database data files to grow automatically Problems and Solutions 2011-07-18

    Oracle database data files to grow automatically Problems and Solutions Recently encountered a rather distressing problems, Oracle Database continues to expand, accounting for server D, E two about 90% of the space, leading to a serious shortage of h

  • Oracle update cascade cascade delete yu 2011-03-19

    SQL and ORACLE foreign key constraints - cascade delete Recent software system to delete a record, it is necessary to also delete a lot of related tables, constraints still exist between them the relationship, so take into account when creating table

  • Hibernate cascade 2009-05-09

    Hibernate cascade (transfer) 2008-12-11 14:53 Extracted from Hibernate P159 proficient Sunwei Qin When the correlation between father and son both sides exist, you can set Office set cascade to all-delete-orphan The so-called father and son relations

  • About Hibernate cascade in inverse = true and 2010-03-31

    hibernate mapping inverse and cascade Xiangjie set 1, where are using cascade ="..."? cascade is not a many to many relationship between attribute must use, and with it so we just insert or delete in a more convenient time, as long as the source

  • struts2 hibernate mysql Jump Chinese problems and dynamic page issues 2010-09-30

    Learn to solve small problems recently, such as mysql Chinese problem, and dynamic page jump problem, to be a record. Internet to find a lot of information, read a lot of books. The first records about mysql Chinese problem. But this guy out of my he

  • Review the Hibernate cascade (transfer) 2010-10-08

    I am the ordinary one, put the crowd can not find the kind of cattle were better than those who can never forget, I believe that sentence - the chance to read a hundred times, its meaning self-evident, so a book I tend to have read it several times,

  • Cascade delete on the issue of data sheet 2010-10-28

    Encountered today in the written test, a question about the cascade delete, the problem is probably the contents of two tables: Employees table (UserId, Username, deptno) and units table (deptno, deptName), which unit employees table table deptno The

  • Windows, Cygwin and Eclipse-based development environment Hadoop configuration problems and solutions 2010-12-14

    Because writing is not immediate, so not every problem encountered in my place later. However, a deeper impression on a few problems and solutions can share it. Have the following main issues: First, when I run format namenode. Kept out of a line sta

  • hibernate --- cascade - inverse 2011-01-12

    First reference to another blog saying: 1.cascade ="..."? cascade attribute does not have to use many relationships, and with it just let us, like when you insert or delete more convenient, as long as the source of the cascade to insert or delet

  • Cascade delete on table problem 2010-10-28

    Encountered today in a written test on cascade delete problem, the problem is probably the contents of two tables: Employees table (UserId, Username, deptno) and units table (deptno, deptName), in which unit employees table table deptno The deptno co

  • Cascade delete and cascade changes 2011-06-22

    Because the issue was a waste of cascade delete more than N, Gu wrote this little experience here, for your reference. Were established in the database table t_food (menu) and Table t_book (orders), as follows: t_food: ---------------------------- fo

  • Phone recharge common problems and solutions 2010-02-25

    (Only need the password without number) Please call 13800138000 filled according to their own voice prompts This card applies to China Mobile's GSM, Shenzhouxing, mass cards, M-Zone and all other mobile brand users to recharge (Note: in some areas du

  • mysql foreign key to achieve the use of cascade delete. Update - Database cascade delete 2010-04-21

    MySQL foreign key to achieve the use of cascading delete, update MySQL storage engines support foreign keys only InnoDB, create a foreign key in the time required the parent table must have a corresponding index, the child table when you create a for