-->
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: Association of Two Individual Tables
PostPosted: Mon Sep 22, 2008 12:07 pm 
Newbie

Joined: Fri Sep 12, 2008 2:10 pm
Posts: 2
Can we associate two tables having similar column but no relation between them?
i.e No foreign key reference between them.

If yes can any one give me an example for that?

Thanks in Advance...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 22, 2008 3:43 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Yes, it is possible, using theta-joins.
Suppose you have this mapping file

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="test11" >
   <class name="Singer" table="SINGER">
    <id name="singerId" column="SINGER_ID" type="int">
      <generator class="increment"/>
    </id>
    <property name="name" column="NAME" type="string"/>
  </class>

  <class name="Album" table="ALBUM">
    <id name="albumId" column="ALBUM_ID" type="int">
      <generator class="increment"/>
    </id>
    <property name="title" column="TITLE" type="string"/>
    <property name="singerId" column="SINGER_ID" type="int"/>
  </class>
</hibernate-mapping>


And the follwing classes:

Code:
package test11;

public class Album {
   private int albumId;
   private String title;
   private int singerId;
   
   public Album(){}
   
   public Album(String title){
       this.title=title;
   }

   /**
    * @return the albumId
    */
   public int getAlbumId() {
      return albumId;
   }

   /**
    * @param albumId the albumId to set
    */
   public void setAlbumId(int id) {
      this.albumId = id;
   }

    public int getSingerId() {
        return singerId;
    }

    public void setSingerId(int singerId) {
        this.singerId = singerId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

}



Code:
package test11;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Singer {
   private int singerId;
   private Set<Album> albums=new HashSet<Album>();
   private String name;

   public Singer(){}
   
   public Singer(String name){
       this.name=name;
   }
   
   /**
    * @return the accountId
    */
   public int getSingerId() {
      return singerId;
   }

   /**
    * @param accountId the accountId to set
    */
   public void setSingerId(int id) {
      this.singerId = id;
   }

    public Set<Album> getAlbums() {
        return this.albums;
    }

    public void setAlbums(Set<Album> albums) {
        this.albums = albums;
    }
   
    public void addAlbum(Album album){
        this.albums.add(album);
        album.setSingerId(this.singerId);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
   
}



A working HQL query would be:

Code:
      StringBuffer sql=new StringBuffer();
      sql.append("select sin.name, alb.title    \n");
      sql.append("from Singer as sin, Album as alb  \n");
      sql.append("where sin.id=alb.singerId  \n");
      
       session.flush();
      
      Query query=session.createQuery(sql.toString());

      Query query=session.createQuery(sql.toString());
      List<Object[]> results=query.list();
      
      for (Object[] res: results){
          logger.info("Singer=" + res[0] +  " album=" + res[1]);
      }



If you want a definition of what a theta-join is, go here

_________________
Gonzalo Díaz


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.