I was able to do a simple join with the implicit notation, as in the following code. Note that I'm using Hibernate within Spring:
Code:
Collection<Media> titles = getHibernateTemplate().find("FROM Media m, Title t WHERE m.titleId = t.id AND t.title LIKE '%" + inTitle + "%'");
But when I tried to re-write it explicitly, the returned collection only contains generic Java Objects (the appropriate number), not Media objects:
Code:
Collection<Media> titles = getHibernateTemplate().find("FROM Media m WHERE m.title.title LIKE '%" + inTitle + "%'");
Chances are, I rewrote the query wrong, but I'm not sure how. Thanks for any suggestions!
I'm using the Hibernate Annotations below (additional Hibernate info at the end; I wasn't sure how to get some of the oft-requested info using Annotations):
MediaCode:
package com.latencyzero.dvd.dao;
//
// Java Imports
//
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
//
// Third-party Imports
//
import org.apache.log4j.Logger;
import org.hibernate.annotations.Proxy;
@Entity
@Table(name = "media")
@Proxy(lazy = false)
public
class
Media
{
@Id
@Column(name = "id")
public Integer getId() { return mID; }
public void setId(Integer inVal) { mID = inVal; }
@Column(name = "titleID")
public Integer getTitleId() { return mTitleID; }
public void setTitleId(Integer inVal) { mTitleID = inVal; }
@Column(name = "formatID")
public Integer getFormatId() { return mFormatID; }
public void setFormatId(Integer inVal) { mFormatID = inVal; }
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name="formatId")
public Format getFormat() { return mFormat; }
public void setFormat(Format inVal) { mFormat = inVal; }
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name="titleId")
public Title getTitle() { return mTitle; }
public void setTitle(Title inVal) { mTitle = inVal; }
private Integer mID;
private Integer mTitleID;
private Integer mFormatID;
private Format mFormat;
private Title mTitle;
private static Logger sLogger = Logger.getLogger(Media.class);
}
FormatCode:
package com.latencyzero.dvd.dao;
//
// Java Imports
//
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
//
// Third-party Imports
//
import org.apache.log4j.Logger;
import org.hibernate.annotations.Proxy;
@Entity
@Table(name = "format")
@Proxy(lazy = false)
public
class
Format
{
@Id
@Column(name = "id")
public Integer getId() { return mID; }
public void setId(Integer inVal) { mID = inVal; }
@Column(name = "parentID")
public Integer getParentId() { return mParentID; }
public void setParentId(Integer inVal) { mParentID = inVal; }
@Column(name = "name")
public String getName() { return mName; }
public void setName(String inVal) { mName = inVal; }
private Integer mID;
private Integer mParentID;
private String mName;
private static Logger sLogger = Logger.getLogger(Format.class);
}
TitleCode:
package com.latencyzero.dvd.dao;
//
// Java Imports
//
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
//
// Third-party Imports
//
import org.apache.log4j.Logger;
import org.hibernate.annotations.Proxy;
@Entity
@Table(name = "title")
@Proxy(lazy = false)
public
class
Title
{
@Id
@Column(name = "id")
public Integer getId() { return mID; }
public void setId(Integer inVal) { mID = inVal; }
@Column(name = "title")
public String getTitle() { return mTitle; }
public void setTitle(String inVal) { mTitle = inVal; }
private Integer mID;
private String mTitle;
private String mImdbID;
private static Logger sLogger = Logger.getLogger(Title.class);
}
Hibernate version: 3.2.1ga
Name and version of the database you are using: MySQL 5.1.14beta