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