Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Hibernate: having trouble saving a LIST of objects to database.

Thread view: 
canoeguy63@gmail.com - 08 Mar 2007 22:42 GMT
I'm trying to use hibernate to have one object save a list of other
objects without success.

When I try to save the containing object, the generated SQL for
inserting the contained objects is missing the parent id and index
columns, so the inserts fail. I have a Group class that contains a
list of Story classes. I want to create a Group object, add some Story
objects to its list, then save the Group object (and the Story objects
by transitivity.)

Classes, mappings and table defs are below. Any indications of what I
am doing wrong are greatly appreciated!

The generated SQL and error message is:
Hibernate: insert into GROUPTABLE (groupname, id) values (?, ?)
Hibernate: insert into STORY (info, id) values (?, ?)
Hibernate: insert into STORY (info, id) values (?, ?)

SEVERE: General error,  message from server: "Field 'parent_id'
doesn't have a default value"

CLASSES
----------------
public class Group {
 private int id;
 private String name;
 private List stories;
 // accessors
}
public class Story {
 private int id;
 private String info;
 // accessors
}

MAPPINGS:
----------------
<class name="Group" table="GROUPTABLE">
        <id name="id" unsaved-value="0">
           <generator class="increment"/>
       </id>

       <list name="stories" cascade="all">
           <key column="parent_id"/>
           <index column="idx"/>
           <one-to-many class="Story"/>
       </list>
       <property name="name" column="groupname" type="string"/>
</class>

<class name="Story" table="STORY">
        <id name="id" unsaved-value="0">
           <generator class="increment"/>
       </id>
         <property name="info"/>
</class>

TABLE DEFS
---------------------
create table groupTable
(id        bigint     not null primary key,
          groupname char(255)  not null
);
create table Story
(id          bigint    not null primary key,
parent_id   bigint    not null,
idx         integer   not null,
info        char(255) not null
);

APPLICATION
----------------------
  Session session = sessionFactory.openSession();
  Transaction tx = session.beginTransaction();

  Group group = new Group("My Group");

  ArrayList stories = new ArrayList();
  stories.add(new Story("Story 1"));
  stories.add(new Story("Story 2"));
  group.setStories(stories);

  session.save(group);
  tx.commit();
  session.close();
Adam Maass - 09 Mar 2007 05:47 GMT
> I'm trying to use hibernate to have one object save a list of other
> objects without success.
[quoted text clipped - 81 lines]
>   tx.commit();
>   session.close();

This doesn't answer your question, but your Story table does not have an
index column idx. (Lists are ordered; relationships in SQL are by default
unordered, best represented by a Set, not a List.)
stephen_wilde@gmx.de - 30 Mar 2007 13:08 GMT
Hi,

I´m having the same problem as u are describing. Unfortunaly I can´t
solve it.
But I would like to know if u have solved the problem already an if
yes, how did u solve it??
Thanks in advance.

Greetings


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.