1
votes

I'm trying to use Hibernate on Android. (Eclipse Android 2.2 SDK)
It seems there is a compile error in line containing

HibernateUtility.getSessionFactory().openSession();  

Here's HibernateUtility Class:

public class HibernateUtility {

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

The compile error in eclipse is:

The type javax.naming.Referenceable cannot be resolved. It is indirectly referenced from required .class files

I've copied a file rt.jar from my java sdk (Sun JDK macosx) which contains the package javax.namaing.* and the compile error seems to be solved. But I now get the runtime error below from Android SDK Logcat.

08-30 15:21:05.123: W/dalvikvm(240): VFY: unable to find class referenced in signature (Lorg/hibernate/SessionFactory;) 08-30 15:21:05.123: I/dalvikvm(240): Could not find method org.hibernate.SessionFactory.openSession, referenced from method com.example.testfordatabase.EtityDao.insert 08-30 15:21:05.123: W/dalvikvm(240): VFY: unable to resolve interface method 102: Lorg/hibernate/SessionFactory;.openSession ()Lorg/hibernate/classic/Session; 08-30 15:21:05.123: D/dalvikvm(240): VFY: replacing opcode 0x72 at 0x000b 08-30 15:21:05.123: D/dalvikvm(240): VFY: dead code 0x000e-003f in Lcom/example/testfordatabase/EtityDao;.insert (Lcom/example/testfordatabase/entity;)V 08-30 15:21:05.123: D/step1(240): Step1 08-30 15:21:05.133: E/dalvikvm(240): Could not find class 'org.hibernate.cfg.Configuration', referenced from method com.example.testfordatabase.HibernateUtility. 08-30 15:21:05.133: W/dalvikvm(240): VFY: unable to resolve new-instance 65 (Lorg/hibernate/cfg/Configuration;) in Lcom/example/testfordatabase/HibernateUtility; 08-30 15:21:05.133: D/dalvikvm(240): VFY: replacing opcode 0x22 at 0x0000 08-30 15:21:05.133: D/dalvikvm(240): VFY: dead code 0x0002-000f in Lcom/example/testfordatabase/HibernateUtility;. ()V 08-30 15:21:05.133: W/dalvikvm(240): VFY: unable to find class referenced in signature (Lorg/hibernate/SessionFactory;) 08-30 15:21:05.133: W/System.err(240): Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org.hibernate.cfg.Configuration 08-30 15:21:05.133: W/dalvikvm(240): Exception Ljava/lang/ExceptionInInitializerError; thrown during Lcom/example/testfordatabase/HibernateUtility;. 08-30 15:21:05.133: D/AndroidRuntime(240): Shutting down VM 08-30 15:21:05.133: W/dalvikvm(240): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 08-30 15:21:05.173: E/AndroidRuntime(240): FATAL EXCEPTION: main 08-30 15:21:05.173: E/AndroidRuntime(240): java.lang.ExceptionInInitializerError 08-30 15:21:05.173: E/AndroidRuntime(240): at com.example.testfordatabase.EtityDao.insert(EtityDao.java:35) 08-30 15:21:05.173: E/AndroidRuntime(240): at com.example.testfordatabase.MainActivity$1.onClick(MainActivity.java:34) 08-30 15:21:05.173: E/AndroidRuntime(240): at android.view.View.performClick(View.java:2408) 08-30 15:21:05.173: E/AndroidRuntime(240): at android.view.View$PerformClick.run(View.java:8816) 08-30 15:21:05.173: E/AndroidRuntime(240): at android.os.Handler.handleCallback(Handler.java:587) 08-30 15:21:05.173: E/AndroidRuntime(240): at android.os.Handler.dispatchMessage(Handler.java:92) 08-30 15:21:05.173: E/AndroidRuntime(240): at android.os.Looper.loop(Looper.java:123) 08-30 15:21:05.173: E/AndroidRuntime(240): at android.app.ActivityThread.main(ActivityThread.java:4627) 08-30 15:21:05.173: E/AndroidRuntime(240): at java.lang.reflect.Method.invokeNative(Native Method) 08-30 15:21:05.173: E/AndroidRuntime(240): at java.lang.reflect.Method.invoke(Method.java:521) 08-30 15:21:05.173: E/AndroidRuntime(240): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 08-30 15:21:05.173: E/AndroidRuntime(240): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 08-30 15:21:05.173: E/AndroidRuntime(240): at dalvik.system.NativeStart.main(Native Method) 08-30 15:21:05.173: E/AndroidRuntime(240): Caused by: java.lang.ExceptionInInitializerError 08-30 15:21:05.173: E/AndroidRuntime(240): at com.example.testfordatabase.HibernateUtility.(HibernateUtility.java:31) 08-30 15:21:05.173: E/AndroidRuntime(240): ... 13 more 08-30 15:21:05.173: E/AndroidRuntime(240): Caused by: java.lang.NoClassDefFoundError: org.hibernate.cfg.Configuration 08-30 15:21:05.173: E/AndroidRuntime(240): at com.example.testfordatabase.HibernateUtility.(HibernateUtility.java:27)

plz help :D thanks

1
According to this answer stackoverflow.com/questions/9033289/… Hibernate is not supported on Android. Did you hear differently somewhere? - John Watts
maybe people didn't try hard :P - TheQuickBrownFox
I am pretty sure Hibernate relies on bytecode manipulation. As the bytecode is completely different in Dalvik, I doubt it would ever work without a major compatibility effort. - John Watts
Hibernate in fact is one of the few persistence providers (and the only JPA provider AFAIK) that actually does not rely on bytecode manip. It's modus operandi is actually to use runtime generated proxies. Just FYI... - Steve Ebersole

1 Answers

1
votes

Not all standart java classes are available on android. One of them is package javax.naming - that's why you get this porblem. You may add missing classes from other sources though. ( or even rip them orr rt.jar ) - but doing this you may end up duplicating complete java runtime. I find that hibernate is a bit too heavyweight for android anyway.