These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: foreign key constraints for primary keys generated
PostPosted: Wed May 19, 2010 7:15 pm 
Beginner
Beginner

Joined: Wed Jul 01, 2009 8:11 am
Posts: 34
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.


Top
 Profile  
 
 Post subject: Re: foreign key constraints for primary keys generated
PostPosted: Thu May 20, 2010 2:49 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Code:
<key column="CHILD_ID"/>


This is incorrect. You should specify the column in the child table that holds the parent id values:

Code:
<key column="parent"/>


Top
 Profile  
 
 Post subject: Re: foreign key constraints for primary keys generated
PostPosted: Thu May 20, 2010 9:32 am 
Beginner
Beginner

Joined: Wed Jul 01, 2009 8:11 am
Posts: 34
ok that did the trick. i left out the specification of an id column-name because i'm using inheritance by the means of table per concrete class strategy.

my most basic class looks like this:
Code:
class Entity {
   private Long id;
   public setId/getId...;
}

which is mapped with the help of DTD entities as recommended:
Quote:
Also notice that properties of Payment are mapped in each of the subclasses. If you want to avoid duplication, consider using XML entities (for example, [ <!ENTITY allproperties SYSTEM "allproperties.xml"> ] in the DOCTYPE declaration and &allproperties; in the mapping).

the use of it looks like this:
EntityProperties.xml:
Code:
<id name="id" type="long" column="ENTITY_ID">
    <generator class="native"/>
</id>

Entity.hbm.xml:
Code:
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" [
        <!ENTITY entityProperties SYSTEM "EntityProperties.xml">
        ]>

<hibernate-mapping>
    <class name="Entity">
    &entityProperties;
    </class>
</hibernate-mapping>

Every other class that is to be mapped with Hibernate should inherit from Entity. Thus every mapping file will include EntityProperties.xml with the help of a DTD entity as shown in Entity.hbm.xml. The problem now is that every mapping file will have the same column name for its id! Thats why i didn't specify one in the first place to let Hibernate generate an appropriate one. Apparently Hibernate isn't capable of doing so?! Is there solution to this? Or will i have to avoid the use of DTD entities and insert the id-element in every mapping file by hand?

regards


Top
 Profile  
 
 Post subject: Re: foreign key constraints for primary keys generated
PostPosted: Thu May 20, 2010 9:41 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I am not really sure why you are concerned about the column name for the id property? This can be the same in all tables if you like.

Your issue is that the column in the <key> tag (in the Parent class) must match the column name in the <many-to-one> tag on the other end (in the Child class).


Top
 Profile  
 
 Post subject: Re: foreign key constraints for primary keys generated
PostPosted: Fri May 21, 2010 10:09 am 
Beginner
Beginner

Joined: Wed Jul 01, 2009 8:11 am
Posts: 34
ok, i finally understand, thx!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.