org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister


Self Advertisement
—–Start of Advertisement——-
BUILD CAR POOL SOLUTIONS ON ANY DEVICE TO RUN ANYWHERE ( Introducing mCruiseOn, the java library /json api’s that you can use to build a car pool solution. Be the next,, mCruiseOn is your one stop API on EC2.
——End of Advertisement——-

I just enabled log4j, and stopped printing stacktrace on exceptions. So the errors I started to get were very cryptic. Printing stacktrace is important even after you have log4j consuming the exception object.
Once I uncommented e.printStackTrace(), I got more meaningful exception stacktraces which helped identify the root cause.
In my case
– Did not have the setMethod for a new primary key defined, in the class refered by the hbm file.
– Had java.util.Integer instead of java.lang.Integer
– for a variable acPreference I had getACPreference, instead I should have had getAcPreference

Include Log4j to my java project in eclipse


Well, like any programmer, I started with System.out.println’s for my logging and debugging needs. But only after I wrote myself a set of test cases. As you assumed right I ran out of thought process to organize my log’s very soon. As I was debugging stuff, I realized that my logs not only need to be categorized they need to be “intended”. When I “intended” I meant that sometime in future I want to be able to say something like “show me logs of all threading happening on my server”. So I know who is using my threads, when and is some client eating up my threadpool ? I also want to be able to drive down to the client level to figure out who exactly is eating it up.

The that is “important” for me, not urgent as yet. As any good time management advice will tell you, to focus on your urgent and important. So currently I need to be able to just organize my logs into the regular 4 levels, fatal, error, info and debug. So here goes. I choose Log4j for many reasons
– Tried and tested
– A lot of help on the net for log4j
– Best Practices are already identified, documented and followed (
* Logger Design
* Should be greppable
* Separate files for separate areas of interest
* Device/Connection/StreamManager threads
* Stream Management
* Request Issues
* Database Issues
* Memcached Issues
* TimeStamps with timezones from server and client and down to nanoseconds (yyyy-MM-dd/HH:mm:ss.SSS/zzz) Java does not support nanoseconds
* Identity (who tried to do what)
* Single Line (no multiple line’s. Except, exceptions
* When did user last login ?
* Error reported to user “must” be logged (exact Error)
* Every Class to have
* public static final Logger log = Logger.getLogger(MyClass.class) ;

I did encounter some trouble starting up. So this blog will help those who want to startup too. The the above advice, I also did the following
– In my main method I did
– Added under the same level as src directory on my eclipse project
– My contents are
log4j.rootLogger=DEBUG, CA
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x – %m%n


#Attribute is one of the following:
#0 -> Reset All Attributes (return to normal mode)
#1 -> Bright (Usually turns on BOLD)
#2 -> Dim
#3 -> Underline
#5 -> link
#7 -> Reverse
#8 -> Hidden
#Foreground is one of the following:
#30 -> Black
#31 -> Red
#32 -> Green
#33 -> Yellow
#34 -> Blue
#35 -> Magenta
#36 -> Cyan
#37 -> White
#Background is one of the following:
#40 -> Black
#41 -> Red
#42 -> Green
#43 -> Yellow
#44 -> Blue
#45 -> Magenta
#46 -> Cyan
#47 -> White;31;35;36;32;34

I am still trying to get colors in my log, but that does not work. When I get it up and running, I’ll edit this post with more. If you have a solution on this, please comment.

WARN net.spy.memcached.transcoders.SerializingTranscoder: Caught IOException decoding bytes of data


I started to get a weird exception; no valid constructor

The stack trace was very weird, it hinted at a launch stack, and a exception from a table other than what I was sending data to. Infact after doing some debugging, step by step, this exception happened on a get on a SomeOther_Hibernate_Table.

Crazy root cause.. 🙂
Old code
List tableRow = null ;
if (isCacheable) {
tableRow value = cache.get(key) ;
return tableRow
String rowQuery = “Select * from ” + tableName + ” where ” + column + “=” + “\”” + key + “\””;
Query rowResult = currentSession().createSQLQuery(rowQuery);
tableRow = rowResult.list();

The cache returns a Object, but hibernate.createSQLQuery returns a List. So to keep things clean, the fix below.
Object value = cache.get(key) ;
if (isCacheable) {
if (value != null) {
tableRow = new ArrayList() ;
return tableRow;
String rowQuery = “Select * from ” + tableName + ” where ” + column + “=” + “\”” + key + “\””;
Query rowResult = currentSession().createSQLQuery(rowQuery);
tableRow = rowResult.list();

Hibernate Class cast expection when reading items from createSQLQuery().list


Got some good advice from
String tableQuery = “Select * from ” + tableName ;
Query tableResult = null;
List table = null;
tableResult = currentSession().createSQLQuery(tableQuery);
table = tableResult.list();

I modified the code to
String tableQuery = “Select {t.*} from ” + tableName + ” t”;
Query tableResult = null;
List table = null;
tableResult = currentSession().createSQLQuery(tableQuery).addEntity(“t”, tableClass);
table = tableResult.list();

Hibernate Getting Started, basic fundamentals


My expectations : My Server has some data which I need my clients to access. Now, obviously first technology I need to explore is JDBC. So I went to the site, and guess what they advice use of Hibernate. So I need to learn Hibernate. The first definition I came across is “Hibernate is a solution for object relational mapping and a persistence management solution or persistent layer.”

This is probably not understandable for anybody learning Hibernate.

So persistence means, saving data to database. That way data is persisted, and not lost on a reboot :). Simple. And “object relational mapping” means copying of tables from database to method variables of a object. Thus making it dead easy to use the data in code.

The main reason to use Hibernate on a server platform is to get rid of the complex JDBC hell hole. The only reason you thought u needed a object relational mapping solution was to get some neatness in code and good old re-usability built into the design. So in short, for Android, people roll up your sleeves and get using some design patterns to get re-usability the right way. There is no short cut 🙂

Research to rule out

So how to get started with Hibernate. Found one fantastic blog to get started with Hibernate, now I can focus on my table design :). But MyEclipse is a priced product :). So personally I had to skip it for now. I researched more and got Hibernator. Tried Hibernator which is a eclipse plugin to make Hibernate integration super easy (seemed too good to be true), but turned out to be a disaster. Gave it up. Also noticed that the code was as old as 2006, no one working on it now.

Finally, I hit GOLD

Then moved to Hibernate Tools from JBoss Hibernate Tools – worked like a charm.

To get hibernate working you need to understand 3 parts of hibernate

1) file : This helps you connect to your database. Select your database, server location where it is hosted, root login, password and other connection properties.

2) hibernate.hbm file : This helps you connect your tables to your objects. All you need is reference the classpath of your java class (POJO) with your table. And then connect each variable to columns. Hibernate also needs to know which column is a the primary key, and the type of each variable.

3) The hibernate library calls, to connect the database, and pass on a reference which you can use in your code to write and read from. As you want to write to the database, you need create objects and call the hibernate <b>save</b> call to issue the write transaction.