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

0

Self Advertisement
—–Start of Advertisement——-
BUILD CAR POOL SOLUTIONS ON ANY DEVICE TO RUN ANYWHERE (www.mcruiseon.com). Introducing mCruiseOn, the java library /json api’s that you can use to build a car pool solution. Be the next avego.com, carpooling.com, zimride.com. 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

0

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 (http://juliusdavies.ca/logging.html)
/**
* 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
PropertyConfigurator.configure(“log4j.properties”);
– Added log.properties under the same level as src directory on my eclipse project
Project
src
….
log4j.properties
– My log4j.properties contents are
log4j.rootLogger=DEBUG, CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x – %m%n

#log4j.appender.A1.all=\u001B[1;37m
#log4j.appender.A1.fatal=\u001B[1;31m
#log4j.appender.A1.error=\u001B[0;31m
#log4j.appender.A1.warn=\u001B[1;33m
#log4j.appender.A1.info=\u001B[0;37m
#log4j.appender.A1.debug=\u001B[0;36m
#log4j.appender.A1.stacktrace=\u001B[0;31m
#log4j.appender.A1.defaultcolor=\u001B[1;37m

#AnsiColorLogger.ERROR_COLOR=2;31
#AnsiColorLogger.WARNING_COLOR=2;35
#AnsiColorLogger.INFO_COLOR=2;36
#AnsiColorLogger.VERBOSE_COLOR=2;32
#AnsiColorLogger.DEBUG_COLOR=2;34
#
#AnsiColorLogger.*=Attribute;Foreground;Background
#
#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
#org.apache.tools.ant.listener.AnsiColorLogger=.ERROR_COLOR=2;31
#org.apache.tools.ant.listener.AnsiColorLogger=.WARNING_COLOR=2;35
#org.apache.tools.ant.listener.AnsiColorLogger=.INFO_COLOR=2;36
#org.apache.tools.ant.listener.AnsiColorLogger=.VERBOSE_COLOR=2;32
#org.apache.tools.ant.listener.AnsiColorLogger=.DEBUG_COLOR=2;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

1

I started to get a weird exception
java.io.InvalidClassException: package.com.something.Some_Hibernate_Table; no valid constructor
at java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:730)
.
.
.
Some_Hibernate_Table

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() ;
tableRow.add(value);
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

0

Got some good advice from https://forum.hibernate.org/viewtopic.php?p=2393573
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();

java.lang.RuntimeException: Stub! at junit.framework.TestSuite.(TestSuite.java:7)

10

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

This has been driving me crazy for the last few days. Late nights and my sleep. All I wanted to do was integrate junit early on the project so that I can start unit testing. So my setup is simple, eclipse, java project (writing my server piece), hibernate and junit. Every time I write just a simple

package com.mcruiseon.server.testcase;
import java.util.ArrayList;
import org.hibernate.SessionFactory;
import junit.framework.TestCase;
import org.junit.Test;

public class HibernateConnectionTest extends TestCase {	
	@Test public void testInsertRow() {
		assertEquals(0,0) ;
	}
}

This above code would throw nasty exception

java.lang.RuntimeException: Stub! 	at junit.framework.TestSuite.(TestSuite.java:7) 	at org.junit.internal.runners.JUnit38ClassRunner.(JUnit38ClassRunner.java:67) 	at org.junit.internal.builders.JUnit3Builder.runnerForClass(JUnit3Builder.java:14) 	at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57) 	at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29) 	at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57) 	at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24) 	at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:34) 	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.(JUnit4TestReference.java:29) 	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestMethodReference.(JUnit4TestMethodReference.java:25) 	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:41) 	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:30) 	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452) 	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

I googled like crazy, logged bugs with junit, all I could find is issues reported with maven. Finally I hit a bug on junit, and identified the root cause.

Dont smile!!!

Move the junit dependency up the chain on your referenced libraries and it worked :).

Have fun

Hibernate Getting Started, basic fundamentals

0

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 sun.com 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 :). http://www.myeclipseide.com/documentation/quickstarts/hibernateintroduction/. 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 – http://download.jboss.org/jbosstools/updates/JBossTools-3.1.0.GA worked like a charm.

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

1) hibernate.properties 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.