Search code examples
javaspringhibernateormhibernate-mapping

HibernateQueryException with mapping a class with a composite key with Hibernate version 2.1.7


I'm maintaining an old software that is using Hibernate 2.1.7c version (released 2004) as the Object-relational mapping with a MySQL-database. The database table itself is old as well and it contains following columns:

remoteId bigint(20)
label    varchar(255)
locale   varchar(255)
PRIMARY KEY ('remoteId', 'locale')

I'm trying to create a new Class that uses a Hibernate-mapping file to map the columns. The mapping-file looks like this:

LocalizedNames.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
    <class name="package.package.LocalizedNames" table="localizednames">
        <composite-id>
            <key-property access="field" column="remoteId" name="remoteId" type="java.lang.Long"/>
            <key-property access="field" column="locale" name="locale" type="java.lang.String"/>
        </composite-id>
        <property column="label" name="label" not-null="true" access="field" type="java.lang.String"/>
    </class>
</hibernate-mapping>

I've tried to read the old Hibernate reference -book a bit to get a catch how this should be working. The book also says like this:

Your persistent class must override equals() and hashCode() to implement composite identifier equality. 
It must also implements Serializable.

So this is how the class itself looks like:

public class LocalizedNames implements Serializable {
    private static final long serialVersionUID = 7526472295622776147L;
    private Long remoteId;
    private String label;
    private String locale;
    /*Getters and setters for each field*/

    @Override
    public boolean equals(Object o) {
        if (o == null) return false;
        if (getClass() != o.getClass()) return false;
        LocalizedNames obj = (LocalizedNames) o;

        if (obj.remoteId == this.remoteId &&
                obj.locale.equals(this.locale)) {
            return true;
        }

        return false;
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(5, 25).
                append(label).
                toHashCode();
    }
}

Okay, so that's the implementation for the class and the Hibernate-mapping. Now, when I'm trying to get data with a Class that extends the HibernateDaoSupport, I'm getting a HibernateQueryException that doesn't help me much. The query is roughly like this:

public class LocalizedNameDao extends BaseDao {
    public List findAllChoiceLocalizedNames(String locale) {
        List result = getHibernateTemplate().find("from LocalizedNames ln where ln.locale=?", locale);
        return result;
    }

 }

 public class BaseDao extends HibernateDaoSupport { ... }

And the stack trace I'm getting is like this:

org.springframework.orm.hibernate.HibernateQueryException: in expected: ln [from LocalizedNames ln where ln.locale=?]; nested exception is net.sf.hibernate.QueryException: in expected: ln [from LocalizedNames ln where ln.locale=?]
net.sf.hibernate.QueryException: in expected: ln [from LocalizedNames ln where ln.locale=?]
    at net.sf.hibernate.hql.FromParser.token(FromParser.java:102)
    at net.sf.hibernate.hql.ClauseParser.token(ClauseParser.java:87)
    at net.sf.hibernate.hql.PreprocessingParser.token(PreprocessingParser.java:123)
    at net.sf.hibernate.hql.ParserHelper.parse(ParserHelper.java:29)
    at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:149)
    at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:138)
    at net.sf.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:295)
    at net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1571)
    at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1542)
    at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
    at org.springframework.orm.hibernate.HibernateTemplate$25.doInHibernate(HibernateTemplate.java:641)
    at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:312)
    at org.springframework.orm.hibernate.HibernateTemplate.find(HibernateTemplate.java:631)
    at org.springframework.orm.hibernate.HibernateTemplate.find(HibernateTemplate.java:626)
    ... more stack trace ...

So, anyone knows what I'm doing wrong here? I'm not that familiar with old Hibernate versions and never used composite keys before, so it might something really simple. Exception and the cause it gives doesn't give me much information where to look eather. The issue seems to be with the Hibernate mapping -file, but just can't pinpoint where it is.

Thanks for all the answers :)


Solution

  • Managed to get this one sorted, it was an simple issue like I said, was just missing some necessary configuration :)