December 31, 2005 16:00
Programming, Java, Blog
I have a question. Hibernate for the most part does exactly what I expect it to, and all is good. However, the elements() function doesn't seem to be working as it should. I suspect my mapping may be off.

Is this legal?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.tersesystems.blog.dao.hibernate">
  
  <!--     
    CREATE TABLE blog_category (
    category 		varchar2(40)	NOT NULL,
    PRIMARY KEY(category)
    );    
  -->
  
  <class name="CategoryImpl" table="BLOG_CATEGORY">    
    <id name="name" column="CATEGORY">      
    </id>
  </class>
  
</hibernate-mapping>

and

public class CategoryImpl implements Category
{
    private String mName;

    /**
     * @return Returns the name.
     */
    public String getName() {
        return mName;
    }

    /**
     * @param pName The name to set.
     */
    public void setName(String pName) {
        mName = pName;
    }
    
    public String toString() {
        return "CategoryImpl: " + mName;
    }
}

Because when I do this query:

String queryString = "from PostImpl as post where :category in elements(post.categories) "
             + " and post.state = :state order by post.date desc";
        
Query query = session.createQuery(queryString);

query = query.setParameter("category", pCategory);
query = query.setInteger("state", PostState.PUBLISH.asCode());
List posts = (List) query.list();

I get this:

2005-12-31 13:48:46,046 INFO [org.hibernate.event.def.DefaultLoadEventListener] - <Error performing load command>
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.tersesystems.blog.dao.hibernate.CategoryImpl#
Programming, Programming]
        at org.hibernate.ObjectNotFoundException.throwIfNull(ObjectNotFoundException.java:27)
        at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:118)
        at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:75)
        at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:643)
        at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:59)
        at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84)
        at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
        at com.tersesystems.blog.dao.hibernate.CategoryImpl$$EnhancerByCGLIB$$6d6de9a8.toString(<generated>)
        at java.text.MessageFormat.subformat(MessageFormat.java:1237)
        at java.text.MessageFormat.format(MessageFormat.java:828)
        at java.text.Format.format(Format.java:133)
        at java.text.MessageFormat.format(MessageFormat.java:804)
        at com.tersesystems.blog.dao.hibernate.HibernatePostDAO.findPublishedByCategory(HibernatePostDAO.java:192)
        at com.tersesystems.blog.action.DisplayPostsAction.execute(DisplayPostsAction.java:130)

It's disconcerting. I'm not even sure what the error message means.

« I lost the game | Home | Hibernate Session Management »

Interesting. Some kiddie thinks it is fun to forge comments on your blog. FYI, those were certainly not typed by Christian or myself.

As to your question, the exception has nothing to do with the query. It occurs during lazy loading of an association, just look at the stack trace.

Looks like either an association mapping problem (but you did not show the actual association mapping), or a lack of referential integrity in your database.

Okay, I see what the problem is now: I assumed if I defined a String property 'categoryName' and did

<ww:param name="'categoryName'" value="#parameters.category"/>

that it would just pick out the first element of the array.

Instead, it turned the array into a comma separated string, which explains the "Programming, Programming" element.

Proper syntax is really:

<ww:param name="'categoryName'" value="#parameters.category[0]"/>

And I should find out why categoryDAO.findById didn't die with that input...

Thanks for your help: Hibernate is looking very good so far.

name
url