-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate mapping and composite keys
PostPosted: Tue Sep 20, 2005 7:25 am 
Newbie

Joined: Tue Sep 20, 2005 6:47 am
Posts: 2
Hibernate version: 3

I have a question regarding hibernate mappings and composite keys. I have 3 java classes whose data members I have shown below. A service is a static table with a number of provisioned services. Each subscriber has a servicelist which is a one to many relationship. The primary key (PK) for each table is indicated for each class. I am wondering what is the best way to define the hibernate mapping for the CopyService class ? At the moment I am using a composite key but this is discouraged as described in the book Hibernate In Action. Not sure what’s the best alternative ?

Service:
private Long serviceid; PK
private String name;
private String description;
private Boolean systemStatus;

Subscriber:
private Long subscriberid; PK
private String msisdn;
private String password;
private String emailaddress;
private Set servicelist;

CopyService:
private Long copyserviceid; PK
private Long subscriberid; PK
private Set emaillist;
private Set msisdnlist;

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
                            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="dbase">
   <class name=" Subscriber"
      table="Subscriber">
      <id name="subscriberid" column="subscriberid" type="long" unsaved-value="null">
         <generator class="native" />
      </id>
      <property name="msisdn" column="msisdn" type="java.lang.String"
         not-null="true" unique="true" />
      <property name="password" column="password"
         type="java.lang.String" not-null="true" />
      <property name="email" column="email" type="java.lang.String" />
      <set name="servicelist" table="Subscriber_Servicelist">
         <key column="subscriberid" />
         <many-to-many class="Service" column="serviceid" unique="true"/>
      </set>
   </class>
</hibernate-mapping>


<hibernate-mapping package="dbase">
    <class name="CopyService" table="copy-service">
        <composite-id name="id" class="CopyServiceKey">
            <key-property name="id" column="id" type="java.lang.Long"/>
            <key-property name="subscriberid" column="subscriberid" type="java.lang.Long"/>
        </composite-id>
        <property name="email" column="email" type="java.lang.String" />
        <property name="msisdn" column="msisdn" type="java.lang.String" />
    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 20, 2005 9:31 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
add primary key id in copy service or use composite key


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 20, 2005 10:46 am 
Newbie

Joined: Tue Sep 20, 2005 6:47 am
Posts: 2
Is this what you mean ? My original mapping used composite primary key but this is discouraged....want an alternative where i have a table with 2 unique properties.


Code:
<hibernate-mapping package="dbase">
    <class name="CopyService" table="copy-service">
        <id name="copyserviceid" column="copyserviceid" type="long">
            <generator class="native" />
       </id>
        <property name="subscriberid" column="subscriberid" type="java.lang.Long"/>
        <property name="email" column="email" type="java.lang.String" />
        <property name="msisdn" column="msisdn" type="java.lang.String" />
    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 20, 2005 1:46 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
You can add new column (primary id) and set this two fields to unique key, but it isn't necessary - composite key work fine a long time (hibernate support for composite key is good)


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