简体   繁体   中英

Hibernate doubles foreign key in one-to-many

I have parent entity for example named as Parent , it extends Base entity, also i have children entities named Children , and there is relation one-to-many in Parent, meaning Set<Children> , but in Children , parent is described just as Base , and therefore hibernate creates two foreign keys in children's table, but what most annoying - one of them is DELETE-CASCADE, and another is not, which cause errors when i'am trying to delete parent, how can i either have one foreign key(without strictly relation to parent as Parent in Children ), or at least fix violating key without DELETE-CASCADE?

Code (not all, just relative) :

Base.hbm.xml:

<class name="Base" table="controls">
    <id name="id">
    <subclass name="Parent" discriminator-value="Parent">
         <set name="items" cascade="save-update, delete-orphan" order-by="orderNumber asc" lazy="false" inverse="true" sort="natural">
             <key column="controlId" on-delete="cascade"/>
             <one-to-many class="children"/>
         </set>
     </subclass>
</class>

Children.hbm.xml:

<class name="controls.FAQItem" table="faq_item">
     <id name="id">
         <generator class="native"/>
      </id>
      <many-to-one name="base" column="controlId"/>
</class>

Parent.java:

public class Parent extends Base{
    private Set<Children> items;
    //getters  and setters
}

Children.class:

public class Children {
    private Base base;
    //getters and setters
}

Errors:

    Cannot delete or update a parent row: a foreign key constraint fails 
(`db_cofp`.`children`, CONSTRAINT `FK3534269CB50AD2C` FOREIGN KEY (`controlId`) 
REFERENCES `controls` (`id`))

Show create table: *just relative end

KEY `FK3534269CC41202F7` (`controlId`),
KEY `FK3534269CB50AD2C` (`controlId`),
CONSTRAINT `FK3534269CB50AD2C` FOREIGN KEY (`controlId`) REFERENCES `controls` (`id`),
CONSTRAINT `FK3534269CC41202F7` FOREIGN KEY (`controlId`) REFERENCES `controls` (`id`) ON DELETE CASCADE

Hope i showed all that matters, note thet Base and Children extends BasicEntity class with id, didn't wrote it as it just have int id and getters and setters

Solved by rewriting mappings to:

Base.hbm.xml:

<class name="Base" table="controls">
    <id name="id">
    <subclass name="Parent" discriminator-value="Parent">
         <set name="items" cascade="save-update, delete-orphan" order-by="orderNumber asc" lazy="false" sort="natural">
             <key column="controlId"/>
             <one-to-many class="children"/>
         </set>
     </subclass>
</class>

Children.hbm.xml:

<class name="controls.FAQItem" table="faq_item">
     <id name="id">
         <generator class="native"/>
      </id>
      <many-to-one name="base" column="controlId" class="Parent"/>
</class>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM