-->
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.  [ 2 posts ] 
Author Message
 Post subject: Just cannot figure this foreign key problem out.
PostPosted: Wed Jan 25, 2006 1:20 pm 
Newbie

Joined: Wed Jan 25, 2006 1:00 pm
Posts: 2
I've been trying to figure this out for hours now. Everything is there and it seems like it should work, but for some reason it is just not. Can someone please help me? I'll keep this as brief as possible. Here is my mapping:

Code:
<hibernate-mapping auto-import="true" default-lazy="true">

   <class name="package.domain.pojo.request.server.ServerInRequest" table="server_in_request">
      <id name="id" column="id">
         <generator class="identity"/>
      </id>

      <property name="hostname" column="hostname"/>
      <property name="ip" column="ip"/>
      <property name="usernamePassword" column="username_password"/>
      <property name="location" column="location"/>

      <joined-subclass name="package.domain.pojo.request.server.ServerForAppInstall" table="app_request_server">
         <key column="server_in_request_id"/>

         <set name="applicationInfos" cascade="all">
            <key column="app_request_server_id" not-null="true"/>
            <one-to-many class="package.domain.pojo.request.server.ApplicationInformation"/>
         </set>

      </joined-subclass>
     
   <class name="package.domain.pojo.request.server.ApplicationInformation" table="app_request_server_app">
      <id name="id" column="id">
         <generator class="identity"/>
      </id>
     
      <property name="applicationName" column="name"/>
      <property name="applicationVersion" column="version"/>
      <property name="applicationServicePackLevel" column="service_pack_level"/>
   </class>
   
</hibernate-mapping>



When I do this straightforward code (shortened for brevity), it gets an error:
Code:
ServerForAppInstall s1 = new ServerForAppInstall();
...
ApplicationInformation info = new ApplicationInformation();
...
s1.getApplicationInfos().add(info);
dao.save(s1);

This is what Hibernate tells me from the debug:
Code:
Hibernate: insert into server_in_request (hostname, ip, username_password, location) values (?, ?, ?, ?)
Hibernate: insert into app_request_server (app_request_id, server_in_request_id) values (?, ?)
Hibernate: insert into app_request_server_app (name, version, service_pack_level, app_request_server_id) values (?, ?, ?, ?)
- SQL Error: 1216, SQLState: 23000
- Cannot add or update a child row: a foreign key constraint fails
- Rolled back transaction after test execution


Although the second INSERT was successful, the third one fails because app_request_server_id is a bad value. Although it shouldn't because it should be the generated primary key of the second INSERT. I've been trying to figure out what value Hibernate is trying to insert, but I haven't been able to figure that out either.

Also, as far the database is concerned, the app_request_server_app.app_request_server_id REFERENCES the app_request_server.id. Yet, it still INSERTs the wrong value. Does anyone see anything wrong? Thank you. I really appreciate this.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 2:09 pm 
Newbie

Joined: Wed Jan 25, 2006 1:00 pm
Posts: 2
Crap. I just realized something. Since my Server is a subclass of another server the primary key is being stored in the parent class's table but with my design I am assuming I have a generated primary key in the subclass's "subtable." Damn that is not right. So I'm referencing the wrong column. If I want the primary key to be inserted correctly, I need to get the key from the parent table. Ok, let me see if this works.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.