Hi there,
What is the correct way to map an association entity? I have the following scenario:
---------------
| Databases|
---------------
| id (PK) |
---------------
^
|
|
-----------------------------
| TableInDatabase |
-----------------------------
| databaseID (PK1)(FK) |
| tableID (PK2)(FK) |
-----------------------------
|
|
\/
------------
| Tables |
------------
| id (PK) |
------------
I've tried the following, as per the Reference Guide, but I keep getting
null for the
tableID regardless of whether I get it from the composite ID or from the Table object contained in the TableInDatabase object:
I Created a composite id object for TableInDatabase containing databaseID and tableID, and a many-to-one relationship on TableInDatabase to Databases and another many-to-one relationship on TableInDatabase to Tables.
Classes:
Database:
Code:
public class Database extends AbstractModelObject implements Serializable {
protected Integer id;
protected String name;
protected String dbmsServerName;
protected String dbmsServerConnect;
protected Dbms dbms = new Dbms();
// list of entries in TableInDatabase
protected List tables = new ArrayList();
protected Integer version;
protected String jdbcClass;
protected String jdbcUrl;
protected String jdbcUserId;
protected String jdbcPassword;
.
.
.
Table:Code:
public class Table extends AbstractModelObject {
protected Integer id;
protected String name;
protected String schema;
protected List columns = new ArrayList();
protected List primaryKeyColumns = new ArrayList();
protected Boolean hasFloats;
// list of entries in TableInDatabase
protected List databases = new ArrayList();
protected Integer version;
.
.
.
TableInDatabase:Code:
public class TableInDatabase extends AbstractModelObject {
protected TableInDatabaseId compositeId;
protected Database database = new Database();
protected Table table = new Table();
protected String insertConflictFlag;
.
.
.
Hibernate Mappings:Database:Code:
<hibernate-mapping
>
<class
name="za.co.telkom.ubr.model.Database"
table="ReplDatabases"
>
<id
name="id"
column="DatabaseId"
type="int"
unsaved-value="-1"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Database.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<version
name="version"
column="version"
type="java.lang.Integer"
/>
<property
name="name"
type="java.lang.String"
update="true"
insert="true"
column="DatabaseName"
length="64"
not-null="true"
/>
<property
name="dbmsServerName"
type="java.lang.String"
update="true"
insert="true"
column="DBMSServerName"
length="64"
not-null="true"
/>
<property
name="dbmsServerConnect"
type="java.lang.String"
update="true"
insert="true"
column="DBMSServerConnect"
length="255"
not-null="true"
/>
<many-to-one
name="dbms"
class="za.co.telkom.ubr.model.Dbms"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="DBMSId"
not-null="true"
/>
<bag
name="tables"
lazy="true"
inverse="true"
cascade="none"
order-by="TableId"
>
<key
column="DatabaseId"
>
</key>
<one-to-many
class="za.co.telkom.ubr.model.TableInDatabase"
/>
</bag>
<property
name="jdbcClass"
type="java.lang.String"
update="true"
insert="true"
column="JdbcClass"
length="255"
not-null="false"
/>
<property
name="jdbcUrl"
type="java.lang.String"
update="true"
insert="true"
column="JdbcUrl"
length="255"
not-null="false"
/>
<property
name="jdbcUserId"
type="java.lang.String"
update="true"
insert="true"
column="JdbcUserId"
length="64"
not-null="false"
/>
<property
name="jdbcPassword"
type="java.lang.String"
update="true"
insert="true"
column="JdbcPassword"
length="64"
not-null="false"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Database.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Table:Code:
<hibernate-mapping
>
<class
name="za.co.telkom.ubr.model.Table"
table="ReplTables"
>
<id
name="id"
column="TableId"
type="int"
unsaved-value="-1"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Table.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="name"
type="java.lang.String"
update="true"
insert="true"
column="TableName"
length="64"
not-null="true"
/>
<property
name="schema"
type="java.lang.String"
update="true"
insert="true"
column="SchemaName"
length="64"
not-null="false"
/>
<bag
name="columns"
lazy="true"
inverse="true"
cascade="all"
order-by="Sequence"
>
<key
column="TableId"
>
</key>
<one-to-many
class="za.co.telkom.ubr.model.Column"
/>
</bag>
<property
name="hasFloats"
type="boolean"
update="true"
insert="true"
column="HasFloatColumns"
not-null="false"
/>
<bag
name="databases"
lazy="true"
inverse="true"
cascade="all"
order-by="DatabaseId"
>
<key
column="TableId"
>
</key>
<one-to-many
class="za.co.telkom.ubr.model.TableInDatabase"
/>
</bag>
<property
name="version"
type="java.lang.Integer"
update="true"
insert="true"
column="Version"
not-null="true"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Table.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
TableInDatabase:Code:
<hibernate-mapping
>
<class
name="za.co.telkom.ubr.model.TableInDatabase"
table="ReplTableInDatabase"
>
<composite-id
name="compositeId"
class="za.co.telkom.ubr.model.TableInDatabaseId"
>
<key-property
name="databaseId"
type="java.lang.Integer"
column="DatabaseId"
/>
<key-property
name="tableId"
type="java.lang.Integer"
column="TableId"
/>
</composite-id>
<many-to-one
name="database"
class="za.co.telkom.ubr.model.Database"
cascade="none"
outer-join="auto"
update="false"
insert="false"
column="DatabaseId"
not-null="true"
/>
<many-to-one
name="table"
class="za.co.telkom.ubr.model.Table"
cascade="none"
outer-join="auto"
update="false"
insert="false"
column="TableId"
not-null="true"
/>
<property
name="insertConflictFlag"
type="java.lang.String"
update="true"
insert="true"
column="InsertConflictFlag"
length="1"
not-null="true"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-TableInDatabase.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Any help is most appreciated. Thanks,
Stefan