-->
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.  [ 3 posts ] 
Author Message
 Post subject: Primary/Foreign key references own table
PostPosted: Wed May 17, 2006 4:10 pm 
Newbie

Joined: Wed May 17, 2006 3:45 pm
Posts: 4
Hibernate version: 3.1
Name and version of the database you are using: Oracle

Database information:
Table Person:
integer PERSON_ID primary key;
varchar FIRST_NAME;
integer CREATE_USER_ID foreign key references PERSON_ID;

Person table has a PERSON_ID which is a sequence and the primary key.
Person has a CREATE_USER_ID which is a foreign key reference to a PERSON_ID.

Test Person Data:
PERSON_ID = 84
FIRST_NAME = TestPerson
CREATE_USER_ID = 3

PERSON_ID = 3
FIRST_NAME = CreatePerson
CREATE_USER_ID = 3

Java information:
public class Person {
private UserTimestamp createUserTimestamp;
private long objectId;
private String firstName;
}

public class UserTimestamp {
private Person person;
private Date timestamp;
}


Problem:
I am trying to setup a hibernate mapping file where I can get hibernate to populate the UserTimestamp object with a Person from the database using the foreign key reference to an existing Person.

I can map fields on the Person just fine using components, I'm just having problems telling Hibernate to populate the Person object that is stored on a UserTimestamp using the primary/foreign key reference setup in the database. Here is some mapping code that does not seem to work:

Code:
<class name="Person" table="PERSON">

  <id name="objectId" column="PERSON_ID">
    <generator class="sequence">
   <param name="sequence">PERSON_SEQ</param>
    </generator>
  </id>

  <property name="firstName" column="FIRST_NAME" />

  <component name="createUserTimestamp" class="UserTimestamp">
    <many-to-one
    cascade="none"
    name="person"
    class="Person"
    column="PERSON_ID"
    unique="true"
    insert="false"
    update="false"/>
    <property name="timestamp" column="CREATE_DT"/>
  </component>
</class>



Any help would be greatly appreciated, I realize that this isn't something that comes up in everyday use. One alternative I have is that in my DAOs, I can manually do the populating of the create person object myself instead of defining it in the mapping. However, I am curious if Hibernate has the power to do something like this - or if anyone else has a better solution.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 12:01 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You're using the wrong column in the association. In the many-to-one, change column to CREATE_USER_ID.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 19, 2006 10:56 am 
Newbie

Joined: Wed May 17, 2006 3:45 pm
Posts: 4
This worked perfectly, I just had to change a few of the insert/update tag attributes and I was good to go. Glad to see that there's people that do care about supporting new Hibernate developers.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.