-->
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.  [ 1 post ] 
Author Message
 Post subject: Composite PK that double as foreign
PostPosted: Fri Feb 29, 2008 1:56 pm 
Beginner
Beginner

Joined: Thu Aug 17, 2006 12:44 pm
Posts: 22
Location: Ohio
The location table has a PK made up of 3 FK fields. The only way that I can successfully add a row to the location table is if I save the customer, email, and phone records first, build the LocationId myself, associate it to the location class and then call session.save(location).

Is this normal for composite PK tables?

Code:
         Phone phone = new Phone();
         phone.setPhoneNumber("330-555-1212");
         session.save(phone);
         
         Email email = new Email();
         email.setEmailAddr("johndoe@usa.net");         
         session.save(email);
         
         Location loc = new Location();         
         LocationId id = new LocationId(cust, email, phone);   
         loc.setId(id);
         loc.setAddress("999 Third Street");
         loc.setCity("Younstown");
         loc.setName("Younstown Office");
         loc.setState("OH");
         loc.setZip("44514");
         
         session.save(loc);      
...

                        // To access I have to go through the id.

         Customer c = (Customer)session.load(Customer.class, 1);
         for (Iterator i = c.getLocations().iterator(); i.hasNext();) {
            Location loc = (Location)i.next();
            System.out.println(loc.getAddress());
            
            System.out.println(loc.getId().getEmail().getEmailAddr());
            System.out.println(loc.getId().getPhone().getPhoneNumber());
         }
         





Hibernate version: 3.1

Mapping documents:
Code:
<hibernate-mapping>
    <class name="maa.hibernate.Location" table="location" catalog="accounting">
        <composite-id name="id" class="maa.hibernate.LocationId">
            <key-many-to-one name="customer" class="maa.hibernate.Customer">
                <column name="customer_id" />
            </key-many-to-one>
            <key-many-to-one name="email" class="maa.hibernate.Email">
                <column name="email_id" />
            </key-many-to-one>
            <key-many-to-one name="phone" class="maa.hibernate.Phone">
                <column name="phone_id" />
            </key-many-to-one>
        </composite-id>
        <property name="name" type="java.lang.String">
            <column name="name" length="45" not-null="true" />
        </property>
        <property name="address" type="java.lang.String">
            <column name="address" length="45" not-null="true" />
        </property>
        <property name="city" type="java.lang.String">
            <column name="city" length="30" not-null="true" />
        </property>
        <property name="state" type="java.lang.String">
            <column name="state" length="2" not-null="true" />
        </property>
        <property name="zip" type="java.lang.String">
            <column name="zip" length="10" not-null="true" />
        </property>     
       
    </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:
MySQL 5.0 InnoDB


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.