tags:

views:

75

answers:

1

Background

The object model is such that:

  • A 'Component' is a primitive, it cannot be made up of smaller parts.
  • A 'Part' is a composite. It can be made up of 'Components', other 'Parts', or any combination of the two.

I have modeled this by having a Part inherit from Component and then having the Part have a collection of Component objects that it is made up of.

Assumptions

  • Java 1.6
  • Hibernate 3.5.1-Final
  • HBM File Mapping (vs Annotations)

Problem

When I try and create Part object, I get an integrity constraint violation:

14:29:24,121  WARN JDBCExceptionReporter:100 - SQL Error: -177, SQLState: 23503
14:29:24,126 ERROR JDBCExceptionReporter:101 - integrity constraint violation: foreign key no parent; FK24013CDD89C7219B table: COMPONENT

Looking at the schema that hbm2ddl generates below, it seems like it is creating an impossible situation.

The Inheritance FK

The Part table refers to the Component table via FK for PartID->ComponentID due to Table per subclass which is to be expected.

alter table Part add constraint FK25D813296E407B foreign key (PartID) references Component;

The Collection FK

It also generates a relationship from the Component table to the Part table via the FK for the ComponentID to represent the collection. However, how it did this was slightly unexcepted.

alter table Component add constraint FK24013CDD89C7219B foreign key (ComponentID) references Part;

I would have expected it to generate the following to represent the collection instead:

alter table Component add constraint FK24013CDD89C7219B foreign key (PartID) references Part;

Or more generally

alter table <Name of class in the collection> add constraint FK123 foreign key (id of the class object which owns the collection) references (Name of the class that holds the collection)

I can see how it might choose ComponentID since really, PartID is FK'd to ComponentID due to the inheritance. However, as result, I believe this has created an impossible situation where the Component table now has an FK requirement to itself, which can never be satisfied.

Question

How do you map a collection of base type objects in a derived type of the same inheritance tree?

Code

Java POJOs

package manufacturing;

public class Component {
    private long _id;
    private String _name;
    private float _price;

    // Hibernate requires all persistent classes to have a default constructor, even if it is private.
    @SuppressWarnings("unused")
    protected Component() {
        this(-1L, "", 0.0f);
    }

    public Component(String name) {
        this(-1L, name, 0.0f);
    }

    public Component(long id, String name, float price) {
        setID(id);
        setName(name);
        setPrice(price);
    }

    public long getID() {
        return _id;
    }

    // Required method for Hibernate
    protected void setID(long id) {
        _id = id;
    }


    public String getName() {
        return _name;
    }

    public void setName(String name) {
       _name = name;
    }

    public float getPrice() {
        return _price;
    }

    public void setPrice(float price) {
        _price = price;
    }
}


package manufacturing;

import java.util.Collection;
import java.util.HashSet;

public class Part extends Component {
    private Collection<Component> _subcomponents;

    // Hibernate requires all persistent classes to have a default constructor, even if it is private.
    @SuppressWarnings("unused")
    private Part() {
        this(-1L, "", 0.0f, new HashSet<Component>());
    }

    public Part(String name) {
        this(-1L, name, 0.0f, new HashSet<Component>());
    }

    public Part(long id, String name, float price, Collection<Component> subcomponents) {
        super(id, name, price);
        setSubcomponents(subcomponents);
    }

    public Collection<Component> getSubcomponents() {
        return _subcomponents;
    }

    public void setSubcomponents(Collection<Component> subcomponents) {
        _subcomponents = subcomponents;
    }

}

Hibernate Mapping

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
                        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;

<hibernate-mapping package="manufacturing">
 <class name="Component" table="Component">
  <id name="ID" column="ComponentID">
   <generator class="native" />
  </id>
  <property name="name" column="Name" />
  <property name="price" column="Price" />        

  <joined-subclass name="Part" table="Part">
   <key column="PartID" />
   <!-- A part has has [0..n] subcomponents -->
   <set name="Subcomponents" lazy="true" cascade="all" inverse="true">
    <key column="ComponentID" />
    <one-to-many class="Component" />
   </set>

  </joined-subclass>
 </class>
</hibernate-mapping>

HBM2DDL Schema

create table Component (ComponentID bigint generated by default as identity (start with 1), Name varchar(255), Price float, primary key (ComponentID));
create table Part (PartID bigint not null, primary key (PartID));
alter table Component add constraint FK24013CDD89C7219B foreign key (ComponentID) references Part;
alter table Part add constraint FK25D813296E407B foreign key (PartID) references Component;

Relavent Stack Trace

14:28:15,001 DEBUG SessionImpl:257 - opened session at timestamp: 12798232950
14:28:47,299 DEBUG JDBCTransaction:82 - begin
14:28:47,313 DEBUG ConnectionManager:444 - opening JDBC connection
14:28:47,333 DEBUG JDBCTransaction:87 - current autocommit status: false
14:29:23,929 DEBUG AbstractSaveEventListener:320 - executing identity-insert immediately
14:29:23,941 DEBUG AbstractBatcher:410 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
14:29:23,948 DEBUG SQL:111 - insert into Component (ComponentID, Name, Price) values (null, ?, ?)
Hibernate: insert into Component (ComponentID, Name, Price) values (null, ?, ?)
14:29:23,966 DEBUG AbstractBatcher:418 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
14:29:23,991 DEBUG JDBCExceptionReporter:92 - could not insert: [manufacturing.Part] [insert into Component (ComponentID, Name, Price) values (null, ?, ?)]
java.sql.SQLException: integrity constraint violation: foreign key no parent; FK24013CDD89C7219B table: COMPONENT
 at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
 at org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)
 at org.hsqldb.jdbc.JDBCPreparedStatement.executeUpdate(Unknown Source)
 at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
 at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
 at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2329)
 at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2836)
 at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
 at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268)
 at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
 at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
 at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
 at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
 at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
 at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
 at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
 at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
 at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:705)
 at org.hibernate.impl.SessionImpl.save(SessionImpl.java:693)
 at org.hibernate.impl.SessionImpl.save(SessionImpl.java:689)
 at DAO.GenericHibernateDAO.create(GenericHibernateDAO.java:91)
 at DAO.PartDAOHibernateTest.testCreatePartWithNoSubcomponents(PartDAOHibernateTest.java:69)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 at java.lang.reflect.Method.invoke(Unknown Source)
 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
 at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
 at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
 at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
 at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
 at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
14:29:24,121  WARN JDBCExceptionReporter:100 - SQL Error: -177, SQLState: 23503
14:29:24,126 ERROR JDBCExceptionReporter:101 - integrity constraint violation: foreign key no parent; FK24013CDD89C7219B table: COMPONENT
+1  A: 

There are two issues here, both are with the mapping file.

The Component is a primitive, it cannot be further divided and thus has no subcomponents. The Part is a composite and thus can have multiple subcomponents that are either Components, Parts, or some combination there of. Therefore, a Component is the base class and Part is a derived class which adds a collection of Components. In this relationship, the Component objects are what is being collected and the Part object is what is holding that collection.

Issue #1 - The set key

The set called "Subcomponents" is properly placed in the Part class, however the key column incorrectly points to the ComponentID when it should point to the PartID. Per the Hibernate Reference Documentation Section 6.2.1. Collection foreign keys:

Collection instances are distinguished in the database by the foreign key of the entity that owns the collection.

So here, since the Part is what owns the collection, the PartID should be used to key it.

Issue #2 - Inverse

From Hibernate - Inside explanation of inverse=true

Inverse defines which side is responsible for the association maintenance. The side having inverse="false" (default value) has this responsibility and will create the appropriate SQL query. Changes made to the association on the side of the inverse="true" are not persisted in the DB.

So here, since this isn't a bi-directional association, the Part is responsible for the collection and should be marked inverse="false" (or leave out this attribute entirely since the default is false). If you don't mark it as false and instead mark it as true, you'll end up with an empty PartID value in the Component table for Subcomponents and possibly duplicate Components. In otherwords, it will persist all of the objects in the collection, but it won't persist the relationship information that tells Hibernate which components belong in which Part's collection.

Code

The correct Hibernate Mapping

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
                        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;

<hibernate-mapping package="manufacturing">
    <class name="Component" table="Component">
        <id name="ID" column="ComponentID">
            <generator class="native" />
        </id>
        <property name="name" column="Name" />
        <property name="price" column="Price" />        

        <joined-subclass name="Part" table="Part">
            <key column="PartID" />
            <!-- A part has has [0..n] sub-components -->
            <set name="Subcomponents" lazy="true" cascade="all" inverse="false">
                <key column="PartID" />
                <one-to-many class="Component" />
            </set>

        </joined-subclass>
    </class>
</hibernate-mapping>

The resulting schema:

create table Component (ComponentID bigint generated by default as identity (start with 1), Name varchar(255), Price float, PartID bigint, primary key (ComponentID));
create table Part (PartID bigint not null, primary key (PartID));
alter table Component add constraint FK24013CDD89368311 foreign key (PartID) references Part;
alter table Part add constraint FK25D813296E407B foreign key (PartID) references Component;
Burly