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




<canoeguy63@xxxxxxxxx> wrote:
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();


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.)



.



Relevant Pages

  • How Alerts can work for Overdue Issues
    ... Adding a choice list for every Issue list in the Sharepoint sites. ... DTS package to the SQL report server (The only reason why this was done ... Making a new SQL Report for SQL Reporting Services via Visual ... The Table is Userdata (All of the Issue Lists are in this ...
    (microsoft.public.sharepoint.portalserver.development)
  • Re: what are cursors in sql???
    ... This shows that you don't know SQL ... traditional comma. ... It makes life easier if the lists in the input strings start and end ... INSERT INTO ParmList ...
    (comp.databases.ms-sqlserver)
  • RE: VB.Net Treeviews
    ... I suggest that you use a Listto contain objects and build a TreeView ... TreeNode corresponding to a given key randomly. ... Private m_Name As String ... Private lists As New List ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Linked List Boxes on Form
    ... Dim sql as string ... Sylvain Lafontaine, ing. ... shows the corresponding Employees of the selected Department. ... two lists are NOT linked because the SQL statement ...
    (microsoft.public.access.adp.sqlserver)
  • Re: OT: It just figures!--UPDATE
    ... > not spoken about on any of the stitching lists that I belong to because ... along with a handmade gift that was something I ... because you have shared things about your private life with me ...
    (rec.crafts.textiles.needlework)