Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HibernateException and Throwable

Tags:

java

hibernate

I'm trying to test does hibernate work in my project, for that I'm trying to create session, and IDEA tells me that org.hibernate.HibernateException and java.lang.Throwable are incompatible and my attempts to cast one to another also failed. Project is desktop app on Java SE 8, has only hibernate-core-5.2.4.Final and hibernate-commons-annotations-5.0.1.Final libraries connected. Main class:

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

    public class Main {
        public static void main(String[] args) throws Exception {
            try {
                SessionFactory session;
                session = new Configuration().configure().buildSessionFactory();
                session.openSession();
            } catch (HibernateException e) { //fail
                e = (Throwable)e; //fail
                e.printStackTrace();
            }
        }
    }

There's hibernate.cfg:

<hibernate-configuration>
  <session-factory>
    <property name=«show_sql»>true</property>
    <property name=«connection.url»>jdbc:sqlite://db/DB.db</property>
    <property name=«connection.driver_class»>org.sqlite.jdbc</property>
    <property name=«connection.username»>root</property>
    <property name=«connection.password»/>
    <property name=«connection.pool_size»>1</property>
    <property name=«current_session_context_class»>thread</property>
    <property name=«dialect»>main.java.SQLiteDialect</property>

<!--> there're some mapping resouces   
    <mapping resource=«logic/File.hbm.xml»/>
<-->    
  </session-factory>
</hibernate-configuration>

I don't understand reasons of this error, because googling has showed following hierachy:

java.lang.Object
└extended by java.lang.Throwable
 └extended by java.lang.Exception
  └extended by java.lang.RuntimeException
   └extended by org.hibernate.HibernateException

Maybe Java 8 doesn't support hibernate correctly, or smthng like this?

like image 704
Dmitresso Avatar asked Dec 01 '25 09:12

Dmitresso


1 Answers

I had the exact same problem. It turned out, that I had missed to import javax.persistence (as seems to be your case also).

Just import hibernate-jpa as well. In my case it was hibernate-jpa-2.1-api-1.0.0.Final.jar.

To import the appropriate hibernate-jpa, you can just use "Configure Module Library" in Intellij and choose "Change Version".

like image 86
Vering Avatar answered Dec 07 '25 01:12

Vering



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!