Hibernate: “No persister for” error

I’m pulling my hair out trying to fix this error. I’ve used the same technique, similar datatypes, similar mapping files…yet, when I try to map my Hibernate “Historical” data object class, I get a “No persister for” error. I’ve googled for solutions to this problem, but nothing has come up applicable to what I’m looking to fix. So, I’m turning to the blogosphere for help here.

In order to debug, I’ve created a servlet which attempts to write a data object (Historical) to MySQL. I do this twice, with the same code (copy & paste for this junk debug code). One method (writeDailysumsToDB) writes to a different data object, with similar types. The second method (writeHistoricalToDB) writes the Historical data object. writeDailysumsToDB works perfectly, yet writeHistoricalToDB fails with the mentioned “No persister for” error. This leads me to believe it’s a mapping error, yet I can’t detect where. So I’m dumping everything here in the hopes someone can browse and point out my obvious error.


----- Exception stacktrace -----

net.sf.hibernate.MappingException: No persister for: db.Historical
at net.sf.hibernate.impl.SessionFactoryImpl.getPersister(SessionFactoryImpl.java:420)
at net.sf.hibernate.impl.SessionImpl.getPersister(SessionImpl.java:2302)
at net.sf.hibernate.impl.SessionImpl.getPersister(SessionImpl.java:2309)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:599)
at spike.WriteHistoricalServlet.writeHistoricalToDB(WriteHistoricalServlet.java:192)
at spike.WriteHistoricalServlet.handleRequest(WriteHistoricalServlet.java:126)
at org.apache.velocity.servlet.VelocityServlet.doRequest(VelocityServlet.java:372)
at org.apache.velocity.servlet.VelocityServlet.doGet(VelocityServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:126)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:103)
at com.caucho.server.http.FilterChainServlet.doFilter(FilterChainServlet.java:96)
at com.caucho.server.http.Invocation.service(Invocation.java:315)
at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:135)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:246)
at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:164)
at com.caucho.server.TcpConnection.run(TcpConnection.java:139)
at java.lang.Thread.run(Thread.java:536)

----- hibernate mapping File -----

<?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="db.Historical" table="historical" >
<id name="id" column="id" type="int" unsaved-value="null">
<generator class="native">
</generator>
</id>
<property name="datelog" type="java.util.Date" column="datelog" not-null="true"/>
<property name="opened_this_day" type="int" column="open_this_day" not-null="true"/>
<property name="closed_this_day" type="int" column="closed_this_day" not-null="true"/>
<property name="open_all" type="int" column="open_all" not-null="true"/>
</class>
</hibernate>

CREATE TABLE historical (
id bigint(5) unsigned NOT NULL auto_increment,
datelog date default '2001-01-01',
opened_this_day bigint(5) unsigned default '0',
closed_this_day bigint(5) unsigned default '0',
open_all bigint(5) unsigned default '0',
PRIMARY KEY (id)
) TYPE=ISAM;

------Data object------

public class Historical {
...
private int id;
private Date datelog;
private int opened_this_day;
private int closed_this_day;
private int open_all;
...

------method in servlet to write data Object------

private boolean writeHistoricalToDB ( Historical historical ) {
boolean result = true;
Transaction tx = null;
try {
SessionFactory sf = new Configuration().configure("/config.cfg.xml").buildSessionFactory();
Session sess = sf.openSession();
try {
tx = sess.beginTransaction();
sess.save(historical);
tx.commit();
} catch (HibernateException he) {
if (tx != null) tx.rollback();
result = false;
log.warn("EXCEPTION: " + he.getMessage(), he);
} finally {
sess.close();
}
} catch (HibernateException he) {
result = false;
log.warn("EXCEPTION: " + he.getMessage(), he);
}
return result;
}//writeHistoricalToDB

UPDATE: Matt Raible solved the problem for me (an hour after posting!). As I suspected, a stupid mistake…I forgot to add the .hbm.xml file to the cfg.xml file. Hopefully google will index and it’ll be a reference in case others have the same problem.

This entry was posted in Java, Software. Bookmark the permalink.

57 Responses to Hibernate: “No persister for” error

  1. upforever says:

    I have a same problem No persister for: com.joyinter.avatar.emoticon.EmoticonSubject
    0.net.sf.hibernate.impl.SessionFactoryImpl.getPersister() (SessionFactoryImpl.java:line=344)
    1.net.sf.hibernate.impl.SessionImpl.getClassPersister() (SessionImpl.java:line=2656)
    2.net.sf.hibernate.impl.SessionImpl.getSQLLoadable() (SessionImpl.java:line=3775)
    3.net.sf.hibernate.impl.SessionImpl.findBySQL() (SessionImpl.java:line=3754)
    4.net.sf.hibernate.impl.SQLQueryImpl.list() (SQLQueryImpl.java:line=52)
    5.com.joyinter.avatar.emoticon.EmoticonSubjectUtil.genTreeFolder() (EmoticonSubjectUtil.java:line=278)
    6._emoticonsidebar__jsp._jspService() (_emoticonsidebar__jsp.java:line=40)
    7.com.caucho.jsp.JavaPage.service() (JavaPage.java:line=75)
    Somebody who can help me,my msn is up_zsm@hotmail.com

  2. vp says:

    wow.. google kicks ass!! and thanks for posting it – you saved a lot of time for many people.

  3. Skybrite says:

    Thaaaaank you so much. This idiotic programmer didn’t update the mapping file….then again that’s good, because now i’ll know the first thing to check if this happens again. Thanks again.

  4. Chaikou says:

    Thank u guyz. 2007 next week and still having the same problem (since the first in 2004 !!!!!)

  5. jero0208 says:

    believe it or not, I had the same problem.

    but I solve the problem after seen your post.

    Thanks!!

  6. Ignacio says:

    A very simple mistake, yet this post saved me from wasting any more time on the debugger. Thanks! :-)

  7. aquadan says:

    I got the same error, but it’s not the same reason. That’s why I add a comment.

    In my case, the Entity to persist (Element) contains a mapping to another entity (Specialization).

    In Element.java, I then have the methods (getter/setter):
    /**
    * Return the value associated with the column: SPEC_ID
    * not-null=true
    */
    public Specialisation getSpecialisation () {
    return this.specialisation;
    }

    /**
    * Set the value related to the column: SPEC_ID
    * @param specialisation the specialisation (Specialisation) value
    */
    public void setSpecialisation (Specialisation specialisation) {
    this.specialisation = specialisation;
    }

    The problem is that I also have a utility method:

    public boolean isSpecialisation() {
    return getSpecialisation != null;
    }

    Then when trying to fetch an Element, it seems that Hibernate uses the isSpecialisation accessor instead of the getSpecialisation one to get the field and then throws the Mapping exception!

    Hope this helps

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>