Hi,
i'm using simple parent/child relationships:
Code:
<class name="Parent">
<id name="id" type="long">
<generator class="native"/>
</id>
<set cascade="all-delete-orphan" inverse="true" name="children">
<key/>
<one-to-many class="Child"/>
</set>
</class>
<class name="Child">
<id name="id" type="long">
<generator class="native"/>
</id>
<many-to-one class="Parent" name="parent" not-null="true"/>
</class>
For database i'm using HSQLDB 1.8.1. The log file there tells me, which tables and which constraints are created:
Code:
create table Child (id bigint generated by default as identity (start with 1), parent bigint not null, primary key (id))
create table Ontology (id bigint generated by default as identity (start with 1), primary key (id))
alter table Child add constraint FK3E2BA382BCAB2B8 foreign key (id) references Parent
alter table Child add constraint FK3E2BA3893F033A5 foreign key (parent) references Parent
As you can see, a foreign key constraint for the primary key "id" is created.
Therfor i'm getting the following error when inserting a new child (that has an "id" that no parent has):
Code:
java.sql.SQLException: Integrity constraint violation - no parent FK3E2BA382BCAB2B8 table: PARENT in statement [insert into Child (id, parent) values (null, ?)]
When i remove the constraint by hand, everything works as expected. I'm using Hibernate version 3.5.2 final which is initialized with the following properties:
Code:
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
Why is this constraint created? Is this supposed to be like this? Or is this a bug? Or a misconfiguration?
Btw i tried to name the "id" column of Child like this
<id name="id" type="long" column="CHILD_ID"> and referring to this column in the set-element of Parent like this
<key column="CHILD_ID"/> with no effect on the outcome.