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

C

canoeguy63

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

Adam Maass

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

stephen_wilde

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top