-->
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: how to define a mapping file if there is no primary key???
PostPosted: Thu Feb 16, 2006 7:06 pm 
Beginner
Beginner

Joined: Thu Jan 12, 2006 7:38 pm
Posts: 25
Hi All,

I have a database table which dont have a primary key .It has only foreign key.

so how to define a xml mapping file for that table.

I tried giving foreign key in <id> tag. but when the query executes it returns only one row, eventhough if there are more row exists.


how to solve it???


thanks in advance
Gopal


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 17, 2006 1:56 am 
Beginner
Beginner

Joined: Mon Oct 24, 2005 2:45 am
Posts: 23
can you update ur mapping xml

this would be base without id


<class name="Test" table="test">

<property name="tid" column="tid" type="integer" />
<property name="name" column="name" type="string" />
</class>


Top
 Profile  
 
 Post subject: Re: how to define a mapping file if there is no primary key?
PostPosted: Fri Feb 17, 2006 2:09 am 
Regular
Regular

Joined: Sun May 08, 2005 2:48 am
Posts: 118
Location: United Kingdom
gopal wrote:
I have a database table which dont have a primary key .It has only foreign key.

so how to define a xml mapping file for that table.

I tried giving foreign key in <id> tag. but when the query executes it returns only one row, eventhough if there are more row exists.


Who is managing the constraint ? Hibernate or the application ? I have a relationship like that where my application manages the constraint so I use an "assigned" id.

Code:
<hibernate-mapping>
    <class name="com.foobar.data.MajorObject" table="major_object">
       <id name="id" type="long" column="id">
           <generator class="native" />
       </id>

       <property    ... />
    </class>

    <class name="com.foobar.data.MinorObject" table="minor_object">
       <id name="objectId" type="long" column="object_id">
           <generator class="assigned" />
       </id>

       <property    ... />
    </class>
</hibernate-mapping>


So the creation code looks like:

Code:
Session hsession = HibernateSesson.currentSession();
MasterObject maxo = new MasterObject();
maxo.setFoobar(0);
hsession.save(maxo);

// Now I have the foriegn key to work with to persist MinorObject instance
MinorObject mino = new MinorObject();
mino.setObjectId(maxo.getId());
mino.setFoobar(0);
hsession.save(mino);



Now I guess it is possible to have mappings of <many-to-one ..> or <one-to-one ...> in MajorObject to allow a field to be linked and lazy loaded, But in my application I don't need that, in my application the MinorObject optionaly exists for each MajorObject and the graph traversal from MajorObject to MinorObeject does not happen in app code. The MinorObject are independantly managed with their own lifecycle by a separate task.

Don't forget to rate... Don't forget to help others... Even if you dont know the answer, you might learn thing new, I know I do :)


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.