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