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



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();

.



Relevant Pages

  • Re: Bill Of Material UML and Hibernate file
    ... The referenced link has an E-R diagram but no class diagrams. ... Given that the article in question contains no UML, Java or Hibernate ... private String description; ...
    (comp.lang.java.databases)
  • Re: Java class with set and get method
    ... hilz wrote: ... > Hibernate framework. ... > I am not sure if this is true, but it seems like hibernate has this kind ... > private double height; ...
    (comp.lang.java.programmer)
  • Re: hibernate: automatically generate timestamps
    ... field (using hibernate annotations as demonstrated in Hibernate In ... hibernate as a timestamp for when it was created. ... private Date modified; ...
    (comp.lang.java.programmer)
  • Re: hibernate: automatically generate timestamps
    ... field (using hibernate annotations as demonstrated in Hibernate In ... hibernate as a timestamp for when it was created. ... private Date modified; ... create/modify timestamp? ...
    (comp.lang.java.programmer)
  • Re: hibernate: automatically generate timestamps
    ... field (using hibernate annotations as demonstrated in Hibernate In ... timestamp field to be updated whenever the record ... private Date modified; ... Complete prototype of the time structure would be helpful. ...
    (comp.lang.java.programmer)