-->
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: composite-id help
PostPosted: Tue Feb 03, 2004 1:54 pm 
Newbie

Joined: Tue Feb 03, 2004 1:49 pm
Posts: 16
i would like to have a composite-id for a table.
primary key a & b.

the first primary key -- a is a integer and "auto-increment".
the second primary key --b is assigned string.

how to do that in hibernate mapping file?

_________________
mzoom@homealone


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2004 2:00 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
If the integer is auto-incremented for each new record, it is a perfect primary key without the string?

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2004 9:12 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Try this...

Mapping:
Code:
<hibernate-mapping default-cascade="all">
   
    <class name="foo" table="foos">
       
        <composite-id name="id" class="CompositeID" unsaved-value="any">
            <key-property name="sequenceId" type="java.lang.Long" column="sequence_id"/>
            <key-property name="stringId" type="string" column="string_id"/>
        </composite-id>

    </class>

    ....

</hibernate-mapping>


A key generator:
Code:
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.engine.SessionFactoryImplementor;
import net.sf.hibernate.engine.SessionImplementor;
import net.sf.hibernate.id.SequenceGenerator;
import net.sf.hibernate.type.LongType;
import java.util.Properties;

public class CompositeIDGenerator {

    private static SequenceGenerator SEQUENCE_GENERATOR =
        new SequenceGenerator();

    static {

        try {
            SEQUENCE_GENERATOR.configure(
                                         new LongType(),
                                         new Properties(),
                                         ((SessionFactoryImplementor)<!-- INSERT METHOD TO GET YOUR SESSION FACTORY HERE -->).getDialect()
                                        );
        } catch(HibernateException he) {
            //You'll want to do a bit more error checking than this, of course... :)
            he.printStackTrace();
        }
    }
   
    public static final CompositeSiteID generate(
                                                 String stringId,
                                                 Session session

                                                )
                                          throws HibernateException {

        CompositeID id = new CompositeID();
        id.setStringId(stringId);
        id.setSequenceId((Long)SEQUENCE_GENERATOR.generate(
                                                           (SessionImplementor)session,
                                                           id
                                                          )
                        );

        return id;
    }
}


Then, whenever you create an object of type foo, you've got to do this:
Code:
new Foo(CompositeIDGenerator.generate("A_STRING", session), ...)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2004 6:00 am 
Newbie

Joined: Tue Feb 03, 2004 1:49 pm
Posts: 16
thanks -- both of you.

_________________
mzoom@homealone


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2004 5:06 pm 
Newbie

Joined: Wed Feb 04, 2004 4:58 pm
Posts: 1
Hi,

I tried to use the following hbm file
--------------------------------------------------------------------------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>
<class name="com.org.csaims.hibernate.IssueResol"
table="ISSUE_RESOL">

<composite-id name="irPK" type="com.org.csaims.pk.IssueResolPK">
<key-property name="custReqResolDate" column="CUST_REQ_RESOL_DATE" type="timestamp" length="7"/>
<key-property name="issueNum" column="ISSUE_NUM" type="string" length="8"/>
</composite-id>
<property name="projectedResolDate" column="PROJECTED_RESOL_DATE" type="timestamp" length="7"/>


</class>
</hibernate-mapping>
--------------------------------------------------------------------------------------
if i dont specify the TYPE atribute to <composite-id it gives me the xml validator exceptions.......

if i use the above file as it is and try to get data using hibernation..i get the following error when it is trying to read and configuring the file hibernate.cfg.xml the file and mapp the objects
-------------------------------------------------------------------------------------

net.sf.hibernate.MappingException: duplicate import: IssueResol
at net.sf.hibernate.cfg.Mappings.addImport(Mappings.java:82)
at net.sf.hibernate.cfg.Binder.bindClass(Binder.java:126)
at net.sf.hibernate.cfg.Binder.bindRootClass(Binder.java:217)
at net.sf.hibernate.cfg.Binder.bindRoot(Binder.java:1204)

-----------------------------------------------------------------------------------

did any one come accross the same problem,

Thanks for ur time and help in response...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2004 5:32 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Well, first of all, "type" is not an attribute of "composite-id"

Code:
<!ELEMENT composite-id ( meta*, (key-property|key-many-to-one)+ )>
  <!ATTLIST composite-id class CDATA #IMPLIED>
  <!ATTLIST composite-id name CDATA #IMPLIED>
  <!ATTLIST composite-id access CDATA #IMPLIED>
  <!ATTLIST composite-id unsaved-value (any|none) "none">

Try "class" instead.


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.