-->
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.  [ 6 posts ] 
Author Message
 Post subject: Primary key constraints ?
PostPosted: Wed May 24, 2006 7:43 am 
Regular
Regular

Joined: Wed Mar 08, 2006 2:07 am
Posts: 50
Location: Bangalore
I want to have a composite-id where Its is collection of 5 keys :

A , B , C , D , E.

It is possible sometimes that E is null but then combination of A, B , C , D are unique.
And in case when E is not null then A , B , C , D , E are together a primary key.


Can I have a primary key combination like the one mentioned above ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 3:21 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
All parts of the primary key have to be not-null. Postgres makes them that way and MySQL too
See also http://bugs.mysql.com/bug.php?id=390

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 11:56 pm 
Regular
Regular

Joined: Wed Mar 08, 2006 2:07 am
Posts: 50
Location: Bangalore
I am using sql server ... though there is no specified Primary Key, in application it is assumed that a combination is a primary key.

kgignatyev, Would that work with Hibernate ??


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 25, 2006 1:41 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Composite ID should do the trick although there are bugs in Hibernate related to composite id handling.
like this http://opensource.atlassian.com/project ... e/HHH-1774

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 25, 2006 2:02 am 
Regular
Regular

Joined: Wed Mar 08, 2006 2:07 am
Posts: 50
Location: Bangalore
ok..
If we go according to the example on that link ...

then instead of the following query:
Code:
from Cobrador cobrador where cobrador = :cobrador


if we change it to:
Code:
from Cobrador cobrador where cobrador.empresa = :emp
and cobrador.matricula = :mat


then that should work.
Right now I am using latter form of query so might not cause problem.

Code:
<composite-id name="id" class="Key" mapped="false" unsaved-value="undefined">
  <key-property name="eId" column="A" type="java.lang.Long" />
  <key-property name="pId" column="B" type="java.lang.Integer" />
  <key-property name="tId" column="C" type="java.lang.Integer" />
  <key-property name="lut" column="D" type="java.util.Date" />
  <key-property name="parId" column="E" type="integer" />
  <key-property name="object" column="object_sys" type="integer" not-null="false" />
  </composite-id>


In case when object_sys has null value then column A,B,C,D,E can uniquely identify the row.
but if object_sys is not null then all column A,B,C,D,E,object_sys are needed to uniquely identify a row.

What would u say about this ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 25, 2006 2:12 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
As long as there are no duplicates are returned from DB for a given ID Hibernate will be happy.

For querying by ID I suggest the syntax:
from Cobrador c where c.id.eId = :eid
and c.id.pId = :pid
....

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


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