[castor-dev] One to many relation

Lukas Lang <[email protected]> Thu, 06 Aug 2009 17:32:22 +0200
Newsgroups gmane.comp.java.castor.devel
Message-ID <[email protected]>
Hey everybody,

I'm facing a problem when trying to map a one to many relation.

Imagine the following: A Parent entity references a List of Child
entities. The mapping is defined as follows:

<mapping>
	<class name="org.castor.cpa.test.lazycollection.Child" identity="id">
		<map-to table="lazy_collection_child" />
		<field name="id" type="integer">
			<sql name="id" type="integer" />
		</field>
	</class>
	<class name="org.castor.cpa.test.lazycollection.Parent" identity="id">
		<map-to table="lazy_collection_parent" />
		<field name="id" type="integer">
			<sql name="id" type="integer" />
		</field>
		<field name="children" type="org.castor.cpa.test.lazycollection.Child"
			lazy="false" collection="arraylist">
			<sql many-key="parent_id" />
		</field>
	</class>
</mapping>

when executing the following statements:

	Parent parent = new Parent();
        parent.setId(1);
        Child child1 = new Child();
        child1.setId(1);
        Child child2 = new Child();
        child2.setId(2);
        parent.setChildren(Arrays.asList(child1, child2));

        // Persist entities.
        _db.setAutoStore(true);
        _db.begin();
        _db.create(parent);
        _db.commit();
        _db.setAutoStore(false);

The create operation fails because the insert statement is as follows:
INSERT INTO lazy_collection_child (id) VALUES (1);

Hence, Castor results in:

java.sql.SQLIntegrityConstraintViolationException: Column 'PARENT_ID'
cannot accept a NULL value.: Column 'PARENT_ID'  cannot accept a NULL
value. at
org.castor.cpa.persistence.sql.keygen.NoKeyGenerator.executeStatement(NoKeyGenerator.java:212)

My general questions are:

Why would it not be possible to map a one to many relation only by
specifying a key column?

The Castor JDO docu does not take care of this issue. It only shows
bidirectional one to many. Has noone ever requested that feature?

Ralf, Ahmad: Any ideas, since you are rewriting SQL stmt generation?

Regards,
Lukas

PS: I'm facing this behavior with 1.2, 1.3 and 1.3.0.1-SNAPSHOT.




---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email