Results 1 to 6 of 6
  1. #1
    berlindutza is offline Member
    Join Date
    Aug 2009
    Posts
    25
    Rep Power
    0

    Default How to delete entities from many-to-many and and many-to-one relationships

    Hi!
    I've got stuck in a problem and I feel it's real important to understand it thoroughly!

    I have a many-to-many relationship, which is mapped unidirectional (with a set). If I delete an entity from the table, which contains the set definition, then the corresponding rows from the link table are also deleted...but when I try to delete an entity from the many side, which contains no set definition, I get an error. Is the only solution for this problem, to make the relationship bidirectional?

    The same problem appears for the many-to-one relationship, which I have only mapped on the many side. If I try now to delete an entity from the one side, and that entity has a corresponding entity child on the many side, then I'm not allowed to delete it. Again, do I need to make the relationship bidirectional or is there any other solution?

    I've read that it's not recommended to make relationships bidirectional.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Making relationships unidirectional or bidirectional is based on the nature of the (real world) relationships between the entities.
    What error do you get? Obviously if the record is linked to other records then you can't delete it because of normal database referential integrity rules.

  3. #3
    berlindutza is offline Member
    Join Date
    Aug 2009
    Posts
    25
    Rep Power
    0

    Default

    The code for the many side, which contains the set definition
    Java Code:
    <hibernate-mapping>
      <class name="packPojo.Category" table="category">
            <id name="id" type="long" column="id">
                 <generator class="native"/>
            </id>
    
           <property name="title" column="title" type="string"  unique="true"/>
    
           <set name="adverts" table="link_category_advert"  cascade="save-update" >
                     <key column="category" foreign-key="fk_advert_category"/>
                     <many-to-many class="packPojo.Advert" column="advert" foreign-key="fk_category_advert" />
           </set>
      </class>
    </hibernate-mapping>
    The code for the many side, which contains no set definition
    Java Code:
    <hibernate-mapping>
      <class name="packPojo.Advert" table="advert">
            
             <id name="id" type="long" column="id">
                 <generator class="native"/>
             </id>
     
              <property name="message" column="message" type="string"/>
              <property name="title" column="title" type="string"/>
              
      </class>
    </hibernate-mapping>
    A part of the error code I get:
    Caused by: java.sql.BatchUpdateException: Duplicate key or integrity constraint violation message from server: "Cannot delete or update a parent row: a foreign key constraint fails (`category_advert`.`link_category_advert`, CONSTRAINT `fk_category_advert` FOREIGN KEY (`advert`) REFERENCES `advert` (`id`))"
    Last edited by berlindutza; 02-25-2010 at 01:27 PM. Reason: code not displayed correctly

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Like I said before, it's normal database integrity rules.
    If you try to delete the record when other records are linked to it then the delete will fail.

  5. #5
    berlindutza is offline Member
    Join Date
    Aug 2009
    Posts
    25
    Rep Power
    0

    Default

    Yeah, but why am I allowed to delete entities from the one side (where the mapping file contains the set definition) and not from the other (where no set definition exists)?

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    That side may have explicit/implicit cascades on deletes or it could be that the records you will be deleting are not linked to other records. As you can see there are several reasons.
    You will need to go through a JPA tutorial/book to be able to use JPA correctly.

Similar Threads

  1. entities are passed by value or passed by reference
    By syntrax in forum New To Java
    Replies: 1
    Last Post: 12-17-2009, 08:13 AM
  2. EntityManager.refresh() - Works on Detatched Or Managed Entities ?
    By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 12-07-2009, 04:34 AM
  3. EntityManager.refresh() works only on managed entities.
    By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 12-05-2009, 01:11 PM
  4. Replies: 3
    Last Post: 10-08-2009, 11:27 PM
  5. Replies: 2
    Last Post: 04-20-2009, 09:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •