Hello everyone!
I'm trying to create a one-to-many association between these tables:
- TABLE_A : ID (pk, integer), SOMEDATA(varchar);
- TABLE_B : ID (pk, integer), TABLE_A_ID (integer), DATE(idx, datetime), SOMEDATA(varchar).
I'm using MySQL. Notice that "TABLE_A_ID" is not a db-side foreign key. I would like to manage that relationship through Hibernate (below).
Code:
<class name="A" table="TABLE_A">
<id name="id" column="ID" type="integer">
<generator class="increment" />
</id>
<property name="someData" column="SOMEDATA" />
<list name="bees" table="TABLE_B" lazy="false" cascade="all">
<key column="TABLE_A_ID" unique="false" />
<index column="DATE" type="timestamp"/>
<one-to-many class="B" />
</list>
</class>
<class name="B" table="TABLE_B">
<id name="id" column="ID" type="integer">
<generator class="increment" />
</id>
<property name="date" column="DATE" type="timestamp"/>
<property name="someData" column="SOMEDATA" />
</class>
With this configuration I got problems at insert time with "TABLE_A_ID" because it seems to be written before the "TABLE_A" row creation (
Field 'TABLE_A_ID' doesn't have a default value). Besides, I don't want to retrieve "TABLE_A_ID" in B instances and I would Hibernate to set it automatically.
So, I read some posts and changed
Code:
<key column="TABLE_A_ID" unique="false" />
to
Code:
<key column="TABLE_A_ID" unique="false" not-null="true"/>
but doing so I obtained
Repeated column in mapping for entity: B column: DATE (should be mapped with insert="false" update="false"). So
Code:
<property name="date" column="DATE" type="timestamp"/>
became
Code:
<property name="date" column="DATE" type="timestamp" insert="false" update="false"/>
At this point, I managed to store my objects but the column "TABLE_B.DATE" is filled with zeros ("0000-00-00 00:00:00")!
Any suggestion?
Thanks in advance.