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.

July 25th, 2004 at 3:10 pm
oh man, believe it or not, I just had the same problem. I found your post, and realized that I also forgot to add my new .hbm.xml file to my application.
Thanks!
-Frank Merenda
July 29th, 2004 at 1:54 pm
Thanks, this saved me a lot of time… haha
August 17th, 2004 at 1:15 pm
google works, thx a log
August 18th, 2004 at 12:05 pm
And thanks from me!
August 19th, 2004 at 8:21 am
Heya, well fortunately that fixes problem is at index #1 @ google
Thx a lot…
August 26th, 2004 at 5:47 am
Thanks from here as well! All too much to take in…
August 31st, 2004 at 2:48 am
Me too…and it doesn’t appear in the Hibernate FAQ!
September 6th, 2004 at 9:14 am
I´m having the same exception, the problem is that i doesn´t have another .hbm.xml file to add because there is a class hierarchy of 4 classes with only 1 file, the idea is:
class p;
class t extends p;
class c extends t;
class l extends t;
I can work fine with class p and class t but i cant work with classes c and l.
Please if anybody can help me I would be very pleased.
September 10th, 2004 at 1:11 pm
“Hopefully google will index and it’ll be a reference in case others have the same problem.”
You rule! I had the same mistake, googled, found (#1st), solved! Tnx
September 13th, 2004 at 9:51 pm
Ditto above. Thanks to you and to Google.
September 16th, 2004 at 7:41 pm
[...] the same problem.” Every few days I get comments added to the original post on the No persister for error saying that m [...]
September 16th, 2004 at 10:06 pm
Google worked by indexing and thanks 2 u
September 22nd, 2004 at 3:00 pm
Google rocks!
November 2nd, 2004 at 8:14 pm
doesn’t fix my problem..I have three methods:
public interface TransactionDAO extends DAO {
public Transaction getTransaction(Long transId);
public void saveTransaction(Transaction transaction);
public void removeTransaction(Long transId);
}
all tests pass, except the remove method on the DAO. I don’t see the problem yet..
November 17th, 2004 at 11:26 pm
And thanks from me too!
November 19th, 2004 at 3:59 pm
I STILL have that problem!
I get this error:
Exception in thread “main” net.sf.hibernate.QueryException: persister not found: test.hibernate.Product [SELECT product FROM products IN CLASS test.hibernate.Product WHERE products.name=test1]
I have 1 table in a MySql database( table name = products) and 3 classes - a Product.class, InsertProduct.class, and FindProductByName.class.
I can successfully insert into the database with InsertProduct.class, but CAN’T do a FindByProductName!!! grrrr..
Is driving me nuts!!
The code for FindByProductName.java:
======================================
import java.util.List;
import net.sf.hibernate.Hibernate;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
// Usage:
// java FindProductByName
public class FindProductByName {
public static void main(String[] args) throws Exception {
String query = “SELECT product ” +
“FROM products IN CLASS test.hibernate.Product ” +
“WHERE products.name=test1″;
String name = args[0];
Configuration cfg = new Configuration().addClass(Product.class);
SessionFactory sf = cfg.buildSessionFactory();
Session sess = sf.openSession();
List list = sess.find(query, name, Hibernate.STRING);
if (list.size() == 0) {
System.out.println(”Product [" + name + "] not found!”);
System.exit(0);
}
Product p = (Product) list.get(0);
sess.close();
System.out.println(”Found product: ” + p);
}
}
==================================
Any help/advice, and thanks!
Ed
November 24th, 2004 at 9:32 am
God bless google and (your!) blogs!
I’ve been racking my brains for hours on this problem and a quick search and finding your original blog has solved it for me!
Many thanks!
Bri…
December 16th, 2004 at 4:46 am
Merry Christmas and Thanx for your blog !
Stupid me … :o))
January 7th, 2005 at 6:59 am
Problem solved in under 2mins.
Cheers
January 14th, 2005 at 1:13 pm
Goddammit, I had the same problem, and forgot to add the .hbm.xml to hibernate.cfg.xml too. Hahahaha…
January 23rd, 2005 at 4:38 pm
Just joining the fray of brainless developers who forgot to update their cfg files. I suppose we’re all walking around looking for our glasses when they’re right on the tops of our heads too…
This doesn’t make us hibernate users look like a very smart bunch.
January 25th, 2005 at 10:20 am
I was reading this blog trying to resolve this same problem, except I am running my examples from my IDE (via a main, to test…)…the problem was essentially the same, I didn’t add all of the classes to my configuration, thanks to Ed; while reading his code –it hit like a lead pipe…
It’s always the small stuff…
February 23rd, 2005 at 8:55 am
What if i didnt forget to add the hbm.xml and it still doesnt work ???
Yep for me it still doesnt work but i am not using a hibernate.cfg.xml because i use springapp-context.xml spring framework. I have the same config somewhere else and the hbm files are still not found…
March 10th, 2005 at 7:45 am
King… Thx. I’m beginner at hibernate technology… So, your help was useful! ohh, ofcourse, it was so simple!!! Grrr
When you come to sweden… come to se me on the track:-)
/Jorpes
March 11th, 2005 at 1:21 am
I am not using the .cfg.xml. I am using hibernate.properties. So How to solve this problem in that case!!!
March 17th, 2005 at 4:14 am
Well, I do have my hbm.xml in my cfg.xml, and I can insert data fine into that table/object. goes right into mysql fine. but, i get the “no persister” error message when i try and run a query using the class in question. anyone else have this?
March 29th, 2005 at 6:28 pm
Many thanks. I too was scratching my head. Google and this site saved the day.
April 17th, 2005 at 8:40 pm
Aii! Thanks (add to the chorus). Solved my problem.
April 19th, 2005 at 5:52 pm
same problem, different solution.
my mapping was added to my cfg file just fine. the problem was, the bean class name defined in the hbm file did not match the actual bean name because i forgot that i had renamed the javabean.
April 21st, 2005 at 12:54 am
Good! Thanks ! Solved my problem too.
April 29th, 2005 at 10:12 am
right on man, right on!
April 29th, 2005 at 9:18 pm
Sorry, gotta tough one!!!
My “core.jar” file contains my backend database classes which includes my hibernate classes and mappings. Using this “core.jar”, I run JUnit tests that all pass with flying colors. I use this same jar within my WAR file and it is used by my servlet classes to get stuff done. When deploying the WAR into Tomcat (5.0.28) most all classes and mappings work really great.
Ahhhh… but when attempting to persist my “User” object graph (which passed all JUnit tests above) from the servlet I get “net.sf.hibernate.MappingException: No persister for: java.lang.String”.
The last thing in the hibernate debug logging statements is what looks like an attempt to save my mapped Set.
Anyone have any guesses???
Thanks in advance
April 30th, 2005 at 5:47 pm
Still stuck…. Arrrrrgggghhhh
I package all of my backend hibernate stuff into a “core.jar”. I have plenty of JUnit tests against all of my Hibernate DAO classes which all pass wonderfully against this core.jar file. This jar file is then deployed in a WAR file where my servlet classes use these DAOs to retrieve/persist my app stuff. This app is deployed in Tomcat (5.0.28) where most all of the mappings in my web app work wornderfully.
Ahhhh….. This is where it all falls apart! When persisting a specific object graph, I get the following line in the stack trace:
net.sf.hibernate.MappingException: No persister for: java.lang.String
Any hiber-gurus out there have any ideas? (String?)
If it helps… The last thing the Hibernate debug statements belched out were saving my Set collection that has a bi-directional mapping and is deployed using Hibernate 2.1.8.
May 16th, 2005 at 5:31 pm
Yet another thankful soul
June 7th, 2005 at 5:52 am
Thanks,,it worked for me !!
I did the same mistake.
June 17th, 2005 at 11:07 am
I have a table which contains the IDs of other two tables
(many-to-one and many-to-one relationship)
When mapping with MyEclipse, the generated class for this table does not expose methods for setting data, instead it creates an additional class named with “Key” at the end (SeClaseorgxtiposeg.java ==> SeClaseorgxtiposegKey.java) which does expose the methods (setter and getters). However, can’t save this kind of object ’cause it has no .hbm.xml related, so it cannot be persisted.
-Am I doing wrong when trying to save this kind of object (oSeClaseorgxtiposegKey)???
-Should I create a .hbm.xml file for the oSeClaseorgxtiposegKey class and add it in the hibernate.cfg.xml??
July 16th, 2005 at 2:59 am
I Didn’t forget to update .cfg. What i did do was put my new Object in a new package, changed all neccessary info in .hbm file to point to correct dbase table. Added new package name as a resource in .cfg file. Run a junit test, but alas …
|——————|
| any idea s? |
|——————|
/
.
/|\
/ \
July 16th, 2005 at 4:39 am
Okay, i was shooting in the dark posting on to here ( desperation ) , but just for the sake of good form let me say that I’ve discovered that my web app runs perfectly when I jar it and deploy. It seems that when I use junit it struggles to see the .cfg file. I’m using :-
Intellij
tomcat 4.3
struts & tiles
Hibernate
postgresql - I thought for a moment that it was a permissions problem (some tables owned by someone else) but I discounted(tested) that.
Also I returned my new Object to the same package as all my other .java files. dont know if that made a difference. My thinking-intuition tells me I haven’t set Intellij properly somewhere to use copyed over(maybe from ant)conf files when running junit. I will check that too.
. — Cheers
/|\
/\
July 22nd, 2005 at 9:32 am
This didn’t help me directly, but it did put me on the right path. I am a JBoss user, and just switched to Hibernate for persistance. We deploy all this as a SAR file.
Anyway, I forgot to add the hbm.xml to the MapResources attribute of my jboss-service.xml. Hope this helps any JBoss people out there that might run into the same problem.
August 1st, 2005 at 7:44 pm
Thanks, solved my problem as well. Grrh!
August 2nd, 2005 at 1:59 pm
I solved this error putting the correct package in class name in file hbm.
With Error: (file .hbm)
WithOut error: (file .hbm)
that’s all
August 9th, 2005 at 11:34 am
hi all,
i was facing the same problem too.
but in my case the mapping was thr in the cfg file.
the problem as we had two vos for the same hbm files.
it is silly mistake though.
but still..
August 30th, 2005 at 4:18 pm
I got the same error but the error message is a bit different. I get the message for java.lang.Boolean.
Can someone advise what might have been the problem?
Thanks
Nary
October 5th, 2005 at 4:47 am
Hi
I have also come across this error after changing the underlying table name in the database. I changed the hbm file and thought that would be fine. Indeed it was… except for one specific class.
In that class it turned out there was native SQL going on all over the place, referencing the old table name - correcting this eliminated the error. Obvious really, but harder to spot!
Cheers
Nick
November 9th, 2005 at 9:43 pm
well,I changed the directory of .cfg.xml,so the new .hbm is not add to it
so stupid,hehe
thanks a lot
December 7th, 2005 at 7:42 am
All you people are shit……………..are thr any real aExamples on thr
December 8th, 2005 at 4:19 pm
Thaaaaaaaaaaaaaaaaaaaaaaaaaanks!
You save me!
December 9th, 2005 at 1:20 am
that worked, didn’t even know there was such a file, thanks for the time saver.
December 19th, 2005 at 6:08 am
hi, friend
i have same problem No persister for class file at net.sf.hibernate.impl.SessionImpl.getPersister(SessionImpl.java:2302)
i stuck at this stage so pls help me also
ajay jain
December 27th, 2005 at 9:37 am
Thanks, It cost my firm a lot of cash until I found this solution
February 16th, 2006 at 12:36 am
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
March 29th, 2006 at 12:19 pm
wow.. google kicks ass!! and thanks for posting it - you saved a lot of time for many people.
April 11th, 2006 at 9:35 pm
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.
December 20th, 2006 at 10:15 am
Thank u guyz. 2007 next week and still having the same problem (since the first in 2004 !!!!!)
September 8th, 2007 at 2:35 am
believe it or not, I had the same problem.
but I solve the problem after seen your post.
Thanks!!
December 17th, 2007 at 7:16 am
A very simple mistake, yet this post saved me from wasting any more time on the debugger. Thanks!
February 28th, 2008 at 7:35 am
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