Mapping documents:
CategoryImpl
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="gr.eulife.eucatalogue.base.CategoryImpl"
table="categories"
proxy="gr.eulife.eucatalogue.base.Category"
>
<id
name="id"
column="id"
type="java.lang.String"
>
<generator class="uuid">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-CategoryImpl.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<bag
name="categories"
lazy="true"
inverse="true"
cascade="save-update"
>
<key
column="parent"
>
</key>
<one-to-many
class="gr.eulife.eucatalogue.base.CategoryImpl"
/>
</bag>
<bag
name="fields"
table="categories_fields"
lazy="true"
cascade="none"
>
<key
column="category"
>
</key>
<many-to-many
class="gr.eulife.eucatalogue.base.FieldImpl"
column="field"
outer-join="auto"
/>
</bag>
<many-to-one
name="parent"
class="gr.eulife.eucatalogue.base.CategoryImpl"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="parent"
not-null="false"
/>
<bag
name="localizedViews"
lazy="true"
inverse="true"
cascade="all"
>
<key
column="category"
>
</key>
<one-to-many
class="gr.eulife.eucatalogue.i18n.CategoryLocalization"
/>
</bag>
<property
name="code"
type="java.lang.String"
update="true"
insert="true"
column="code"
/>
<property
name="name"
type="java.lang.String"
update="true"
insert="true"
column="name"
/>
<bag
name="items"
table="items_categories"
lazy="true"
cascade="none"
>
<key
column="category"
>
</key>
<many-to-many
class="gr.eulife.eucatalogue.base.ItemImpl"
column="item"
outer-join="auto"
/>
</bag>
<joined-subclass
name="gr.eulife.eucatalogue.base.CatalogueCategoryImpl"
table="catalogue_categories"
proxy="gr.eulife.eucatalogue.base.CatalogueCategory"
>
<key column="category" />
<property
name="catalogueName"
type="java.lang.String"
update="true"
insert="true"
column="catalogue_name"
/>
</joined-subclass>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-CategoryImpl.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
FieldImplCode:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="gr.eulife.eucatalogue.base.FieldImpl"
table="fields"
proxy="gr.eulife.eucatalogue.base.Field"
>
<id
name="id"
column="id"
type="java.lang.String"
>
<generator class="uuid">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-CategoryField.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<bag
name="categories"
table="categories_fields"
lazy="false"
inverse="true"
cascade="none"
>
<key
column="field"
>
</key>
<many-to-many
class="gr.eulife.eucatalogue.base.CategoryImpl"
column="category"
outer-join="auto"
/>
</bag>
<bag
name="fieldValues"
lazy="true"
inverse="true"
cascade="all"
>
<key
column="field"
>
</key>
<one-to-many
class="gr.eulife.eucatalogue.base.FieldValueImpl"
/>
</bag>
<bag
name="localizedViews"
lazy="false"
inverse="true"
cascade="all"
>
<key
column="field"
>
</key>
<one-to-many
class="gr.eulife.eucatalogue.i18n.FieldLocalization"
/>
</bag>
<property
name="code"
type="java.lang.String"
update="true"
insert="true"
column="code"
/>
<property
name="name"
type="java.lang.String"
update="true"
insert="true"
column="name"
/>
<joined-subclass
name="gr.eulife.eucatalogue.base.CatalogueFieldImpl"
table="catalogue_fields"
proxy="gr.eulife.eucatalogue.base.CatalogueField"
>
<key column="field" />
<property
name="catalogueName"
type="java.lang.String"
update="true"
insert="true"
column="catalogue_name"
/>
</joined-subclass>
<joined-subclass
name="gr.eulife.eucatalogue.base.RequiredFieldImpl"
table="categories_fields"
proxy="gr.eulife.eucatalogue.base.RequiredField"
>
<key column="field" />
<many-to-one
name="category"
class="gr.eulife.eucatalogue.base.CategoryImpl"
cascade="none"
outer-join="auto"
update="false"
insert="false"
column="category"
not-null="true"
/>
<property
name="required"
type="boolean"
update="true"
insert="true"
column="required"
/>
</joined-subclass>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-CategoryField.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
database tablesCode:
Table "eucatalogue.categories"
Column | Type | Modifiers
--------+-----------------------+-----------
id | character varying(32) | not null
code | character varying(25) |
name | character varying(50) |
parent | character varying(32) |
Code:
Table "eucatalogue.categories_fields"
Column | Type | Modifiers
----------+-----------------------+-----------
category | character varying(32) | not null
field | character varying(32) | not null
required | boolean |
Code:
Table "eucatalogue.fields"
Column | Type | Modifiers
--------+-----------------------+-----------
id | character varying(32) | not null
code | character varying(25) |
name | character varying(50) |
Now if there is already an entry in the categories_fields table and I do a query to update the "required" column, it will work
e.g.
Code:
Category c = Catalogue.category("402881c11067f033011067f037b50001");
Field f = Catalogue.field("402881c11067f033011067f037ee0002");
CriterionQuery query = new CriterionQuery(FieldImpl.class);
query.addParameter(new Parameter("category", new Parameter("id", c.getId())));
query.addEquality("id", f.getId());
RequiredField rf = (RequiredField)new HiberClient().uniqueResult(query);
rf.setRequired(true);
However if I have just created the association I'll get a ClassCastException
Code:
Category c = new CategoryImpl();
Catalogue.save(c);
Field f = new FieldImpl();
Catalogue.save(f);
c.addField(f);
CriterionQuery query = new CriterionQuery(FieldImpl.class);
query.addParameter(new Parameter("category", new Parameter("id", c.getId())));
query.addEquality("id", f.getId());
RequiredField rf = (RequiredField)new HiberClient().uniqueResult(query);
rf.setRequired(true);
How can I tackle this? Do I have to flush the session before doing the query?