-->
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.  [ 2 posts ] 
Author Message
 Post subject: Mapping POJOs without a referential integrity constraint
PostPosted: Fri Feb 27, 2009 10:44 am 
Newbie

Joined: Mon Nov 10, 2008 8:43 am
Posts: 8
Hi all,

Is it possible to map two POJOs without a database relation between them? Let me explain my case in little more detail.

I have got two entities : Contact and Guest. I have used Contact as one of the instance variable in the guest class. I want that whenever Hibernate loads the guest class, it automatically loads the Contact object in it. The kinking can be on the basis of the contact's ID which I am keeping in the guest table.

Please let me know if it is possible or not. If it can be done with annotations, it will be great. Alternatively if there is some kind of entity creation interceptor/listener in Hibernate, where I can handle this will work as well.

Regards,
Nitin


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2009 1:32 pm 
Newbie

Joined: Mon Dec 29, 2008 7:40 pm
Posts: 6
Location: France
Yes this is possible and it works whether or not you've specified a foreign key constraint.

All you have to do is specify the relationships either in your mapping files or by way of annotations.

Here's an SQL example to create the tables (with no foreign key constraint!)...

Code:
create table guest (
    id bigint not null primary key,
    name varchar(50) not null
)

create table contact (
    id bigint not null primary key,
    name varchar(50) not null,
    guestId bigint
)

...here's the mapping examples...

Code:
<hibernate-mapping package="test.model">
   <class name="Guest" table="guest">
      <id
         column="id"
         name="Id"
         type="java.lang.Long"
      >
         <generator class="vm" />
      </id>
      <property
         column="name"
         length="20"
         name="Name"
         not-null="true"
         type="string"
       />
      <set inverse="true" name="ContactSet">
         <key column="guestId" />
         <one-to-many class="Guest" />
      </set>
   </class>
</hibernate-mapping>


Code:
<hibernate-mapping package="test.model">
   <class name="Contact" table="contact">
      <id
         column="id"
         name="Id"
         type="java.lang.Long"
      >
         <generator class="vm" />
      </id>
      <property
         column="name"
         length="45"
         name="Name"
         not-null="true"
         type="string"
       />
      <many-to-one
         class="Guest"
         name="Guest"
         not-null="true"
      >
         <column name="guestId" />
      </many-to-one>
   </class>
</hibernate-mapping>

...with the relevant Guest and Contact classes...

Code:
package test.model;

import java.io.Serializable;
/**
* @hibernate.class
*  table="guest"
*/
public class Guest implements Serializable {
   private Long id;
   private String name;
   private java.util.Set contactSet;

   public Guest() { .. }

   public Long getId () { .. }
   public void setId (Long id) { .. }

   public String getName () { .. }
   public void setName (String name) { ... }

   public java.util.Set getContactSet () { .. }
   public void setContactSet (java.util.Set contactSet) { ... }

   public void addToContactSet (Object obj) { ... }
   public boolean equals (Object obj) { ... }
   public int hashCode () { ... }
   public String toString () { ... }
}

Code:
package test.model;

import java.io.Serializable;

/**
* @hibernate.class
*  table="contact"
*/
public class Contact  implements Serializable {
   private java.lang.Long id;
   private String name;
   private Guest guest;

   public Contact() { ... }

   public Long getId () { ... }
   public void setId(Long id) { ... }

   public String getName() { ... }
   public void setName(String name) { ... }

   public Guest getGuest() { ... }
   public void setGuest(Guest guest) { ... }

   public boolean equals (Object obj) { ... }
   public int hashCode () { ... }
   public String toString () { ... }
}

...and your query could look like this...

Code:
List<Guest> guestList = session.createQuery(
          "select g from Guest g" +
          "left join g.ContactSet c" +
          "where g.Id = :guestId"
).setParameter("guestId", guestId).list();

If you're interested in JPA annotations, have a look here for an example with a Professor who has many Departments.

Hope this helps


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