I am trying to create a one-to-many association -- essentially I want to have a parent object (UserErrorSettings) which contains a collection of children objects (a set of UserErrorFilterStrings). I can generate the codes with hbm2java, compile the classes, and export the schema, but when I try to create some objects and save them to my database I get a MappingException telling me that java.lang.String is an unknown entity class. This doesn't happen if I remove the cascade="all-delete-orphan" or cascade="all" from the set declaration in the mapping document for the parent class (UserErrorSettings) which contains the set/collection of children, but without this cascading the collection of children is not saved in the database whenever I save the parent object.
I've searched the FAQs, used Google, and looked through Hibernate in Action and Hibernate Developer's Notebook and I can't seem to find anything which explains this. If anyone can tell me what I've done wrong then I'll really appreciate the help.
--James
Hibernate version: 2.1.7
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.sesame.grover.hibernate.UserErrorSettings"
table="USER_ERROR_SETTINGS">
<id name="userErrorSettingsId"
column="USER_ERROR_SETTINGS_ID"
type="int">
<generator class="hilo"/>
</id>
<property name="userId"
column="USER_ID"
type="string"/>
<property name="blockEmail"
column="BLOCK_EMAIL"
type="boolean"/>
<property name="timeFilterInterval"
column="TIME_FILTER_INTERVAL"
type="long"/>
<set name="userErrorFilterStrings"
inverse="true"
cascade="all-delete-orphan">
<key column="USER_ERROR_SETTINGS_ID"/>
<one-to-many class="com.sesame.grover.hibernate.UserErrorFilterStrings"/>
</set>
</class>
</hibernate-mapping>
------------------------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.sesame.grover.hibernate.UserErrorFilterStrings"
table="USER_ERROR_FILTER_STRINGS">
<id name="userErrorFilterStringId"
column="USER_ERROR_FILTER_STRING_ID"
type="int">
<generator class="hilo"/>
</id>
<property name="filterString"
column="FILTER_STRING"
type="string"/>
<many-to-one name="userErrorSettingsId"
column="USER_ERROR_SETTINGS_ID"
class="com.sesame.grover.hibernate.UserErrorSettings"
not-null="true"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Transaction transaction = null;
try
{
// Create some data and persist it
transaction = session.beginTransaction();
// create and save an error settings object
HashSet filterStringsSet = new HashSet();
filterStringsSet.add("Bad Voodoo");
filterStringsSet.add("Catastrophe");
UserErrorSettings userErrorSettings = new UserErrorSettings("ernie",
new Boolean(false),
new Long(60000),
filterStringsSet);
session.save(userErrorSettings);
// We're done; make our changes permanent
transaction.commit();
Full stack trace of any exception that occurs:
[java] net.sf.hibernate.MappingException: Unknown entity class: java.lang.String
[java] at net.sf.hibernate.impl.SessionFactoryImpl.getPersister(SessionFactoryImpl.java:347)
[java] at net.sf.hibernate.impl.SessionImpl.getClassPersister(SessionImpl.java:2710)
[java] at net.sf.hibernate.impl.SessionImpl.getPersister(SessionImpl.java:2717)
[java] at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1391)
[java] at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
[java] at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
[java] at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
[java] at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
[java] at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
[java] at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:961)
[java] at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:866)
[java] at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:788)
[java] at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:747)
[java] at com.sesame.grover.test.CreateTest.main(CreateTest.java:48)
[java] Exception in thread "main"
[java] Java Result: 1
Name and version of the database you are using: Oracle 9i
The generated SQL (show_sql=true):
[schemaexport] create table USER_ERROR_FILTER_STRINGS (
[schemaexport] USER_ERROR_FILTER_STRING_ID number(10,0) not null,
[schemaexport] FILTER_STRING varchar2(255),
[schemaexport] USER_ERROR_SETTINGS_ID number(10,0) not null,
[schemaexport] primary key (USER_ERROR_FILTER_STRING_ID)
[schemaexport] )
[schemaexport] create table USER_ERROR_SETTINGS (
[schemaexport] USER_ERROR_SETTINGS_ID number(10,0) not null,
[schemaexport] USER_ID varchar2(255),
[schemaexport] BLOCK_EMAIL number(1,0),
[schemaexport] TIME_FILTER_INTERVAL number(19,0),
[schemaexport] primary key (USER_ERROR_SETTINGS_ID)
[schemaexport] )
[schemaexport] alter table USER_ERROR_FILTER_STRINGS add constraint FKA5B237C6B0525045 foreign key (USER_ERROR_SETTINGS_ID) references USER_ERROR_SETTINGS
[schemaexport] create table hibernate_unique_key (
[schemaexport] next_hi number(10,0)
[schemaexport] )
[schemaexport] insert into hibernate_unique_key values ( 0 )
Debug level Hibernate log excerpt:I'm not sure how to get this -- I've uncommented the file appender section in log4j.properties but I don't see that a hibernate.log file is ever being created.