Composite-key self reference causes invalid SQL on select query
---------------------------------------------------------------
Key: HHH-2994
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2994
Project: Hibernate3
Issue Type: Bug
Components: core
Affects Versions: 3.2.5
Environment: Hibernate 3.2.5, SqlServer 2000
Reporter: Kocha Kaden
When creating a mapping on a composite-key table that maps back to itself, Hibernate gets
confused and generates invalid SQL if I do a select statement checking for null.
HQL "from TestBean where superceededBy is null" results in:
"select testbean0_.id as id0_, testbean0_.createdDate as createdD2_0_,
testbean0_.name as name0_, testbean0_.superceededById as supercee4_0_,
testbean0_.superceededByCreatedDate as supercee5_0_ from TestBean testbean0_ where
(testbean0_.superceededById, testbean0_.superceededByCreatedDate) is null"
SqlServer does not understand "where (testbean0_.superceededById,
testbean0_.superceededByCreatedDate) is null".
It should be "where testbean0_.superceededById is null and
testbean0_.superceededByCreatedDate is null"
Code follows:
TestBean.hbm.xml
<hibernate-mapping>
<class name="org.test.htest.TestBean" table="TestBean">
<composite-id>
<key-property name="id" type="integer" column="id"
/>
<key-property name="createdDate" type="java.util.Date"
column="createdDate" />
</composite-id>
<property name="name" type="string" column="name"
/>
<many-to-one name="superceededBy"
foreign-key="fk_TestBeanSuperceededBy_TestBeanIdCreatedDate">
<column name="superceededById"/>
<column name="superceededByCreatedDate"/>
</many-to-one>
</class>
</hibernate-mapping>
TestBean.java:
public class TestBean implements Serializable
{
private static final long serialVersionUID = 1L;
private int id;
private Date createdDate;
private String name;
private TestBean superceededBy;
public Date getCreatedDate()
{
return createdDate;
}
public void setCreatedDate(Date createdDate)
{
this.createdDate = createdDate;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public TestBean getSuperceededBy()
{
return superceededBy;
}
public void setSuperceededBy(TestBean superceededBy)
{
this.superceededBy = superceededBy;
}
}
Main.java:
public class Main
{
public static void main(String[] args)
{
Configuration cfg = new Configuration();
cfg.configure("/hibernate.cfg.xml");
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
Query query = session.createQuery("from TestBean where superceededBy is
null");
List result = query.list();
tx.commit();
System.out.println("Got " + result.size() + " results");
System.out.flush();
}
catch(HibernateException e)
{
if (tx != null && tx.isActive())
{
tx.rollback();
}
}
sessionFactory.close();
}
}
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira