-->
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.  [ 4 posts ] 
Author Message
 Post subject: Setting foreign key in collection when saving.
PostPosted: Fri Oct 26, 2007 8:55 pm 
Newbie

Joined: Fri Oct 26, 2007 8:40 pm
Posts: 3
The problem is generic (not specific to these classes). I've probably misunderstood the way collections work but I haven't been able to sort it out via google/hibernate.org/java persistance with hibernate.

I have 2 classes:

Code:
public class MemoryBO extends AbstractBO {
   protected int userId;
   protected String title;
   protected String contents;
   protected Collection<TagBO> tags;

   public MemoryBO() {}

        ... getters/setters removed for brevity...
}

public class TagBO extends AbstractBO {
   protected String tag;
   
   public TagBO() {}

        ... getters/setters removed for brevity...
}


This is mapped as such:
Code:
   <class name="bo.MemoryBO" table="Memories">
      <id name="id">
         <generator class="native"/>
      </id>

      <property name="userId"/>
      <property name="title"/>
      <property name="contents"/>

      <set name="tags" cascade="save-update">
         <key column="parent_id"/>
         <one-to-many class="bo.TagBO" />
      </set>
   </class>

   <class name="bo.TagBO" table="Tags">
      <id name="id">
         <generator class="native"/>
      </id>
      <property name="tag"/>
      <property name="created" type="timestamp"/>
   </class>


The table definitions are thus:

Code:
CREATE TABLE Memories (
   id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
   userId INT NOT NULL,
   CONSTRAINT FOREIGN KEY (userId) REFERENCES Users(id),
   title VARCHAR(80) NOT NULL,
   contents TEXT NOT NULL
) TYPE=InnoDB;

CREATE TABLE Tags (
   id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
   tag VARCHAR(20) NOT NULL,
   memoryId INT NOT NULL,
   CONSTRAINT FOREIGN KEY (memoryId) REFERENCES Memories(id),
   Created TIMESTAMP(14)
) TYPE=InnoDB;



I can load the data fine. When I attempt to save an instance of MemoryBO with some tags in it, the insert fails due to a null constraint - because hibernate isn't including the id of the memoryBO object in the insert to Tags(memoryId).

What I haven't been able to figure out is how I tell hibernate to set that field to the ID of the container class. I've tried this with a "int memoryId" field in TagBO but still can't figure out how to make hibernate set the value correctly.

Any help is greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 27, 2007 8:36 am 
Beginner
Beginner

Joined: Mon Feb 19, 2007 4:22 am
Posts: 22
Location: Poland
This
Code:
<class name="bo.MemoryBO" table="Memories">
   ...
   <key column="parent_id"/>
</class>

implies this:

Code:
CREATE TABLE Tags (
   ...
   parent_id INT NOT NULL,
   ...
) TYPE=InnoDB;


Helped?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 8:21 am 
Newbie

Joined: Fri Oct 26, 2007 8:40 pm
Posts: 3
Unfortunately, no. I think I had tried that before, but I just tried it again to make sure...

When I attempt to persist an instance of the MemoryBO class with a couple TagBOs inside it, I get the following:

Code:

Hibernate: insert into Memories (userId, title, contents, viewCount, explicit, featured, draft, created) values (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into Tags (tag, created) values (?, ?)

org.hibernate.exception.GenericJDBCException: could not insert: [bo.TagBO]

...

Caused by: java.sql.SQLException: Field 'memoryId' doesn't have a default value



This is directly caused by the Not-Null constraint in the SQL. However, I obviously want the key for the containing memory stored in the tag so I can reload it.

If I need to scrap the current definitions in the mapping file, I'm fine with that. In the end, what I care about is having a collection of tags associated with a memory that get loaded and saved correctly.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 8:50 am 
Newbie

Joined: Fri Oct 26, 2007 8:40 pm
Posts: 3
Let me ask the question a different way...

Given the definition of the classes above but with a int TagBO.memoryId field with getter/setters, how do I change the mapping to tell hibernate to update the memoryId field with the ID of the container just before saving? That is really all I need.

I can't do it manually since I use database generated IDs and thus don't have one until after I call persist.


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