-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: One to many, associated lists and indexing
PostPosted: Fri Apr 16, 2010 6:20 am 
Newbie

Joined: Fri Apr 16, 2010 5:35 am
Posts: 3
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.


Top
 Profile  
 
 Post subject: Re: One to many, associated lists and indexing
PostPosted: Fri Apr 16, 2010 8:22 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You can't use a date/timestamp as a list index. List indexes are always numbers like 0, 1, 2,.... etc.


Top
 Profile  
 
 Post subject: Re: One to many, associated lists and indexing
PostPosted: Fri Apr 16, 2010 8:48 am 
Newbie

Joined: Fri Apr 16, 2010 5:35 am
Posts: 3
nordborg wrote:
You can't use a date/timestamp as a list index. List indexes are always numbers like 0, 1, 2,.... etc.


Thanks for the reply.
Gosh...
If I use "TABLE_B.ID" or another declared column as index, Hibernate would say Repeated column in mapping for entity: B column: ID (should be mapped with insert="false" update="false"). Moreover, "insert" and "update" aren't id tag attributes.
Any solution?


Top
 Profile  
 
 Post subject: Re: One to many, associated lists and indexing
PostPosted: Fri Apr 16, 2010 8:55 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You'll need an additional column that holds the list index.


Top
 Profile  
 
 Post subject: Re: One to many, associated lists and indexing
PostPosted: Fri Apr 16, 2010 9:13 am 
Newbie

Joined: Fri Apr 16, 2010 5:35 am
Posts: 3
nordborg wrote:
You'll need an additional column that holds the list index.

I've tried but the index doesn't seem to increment. Is it up to me?
Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.