1

I had to replace the Hibernate call Connection conn = sessionImpl.connection() with the following, due to ".connection()" being deprecated in Hibernate. ref: session.connection() deprecated on Hibernate? A try;catch; is required, as doWork() throws a HibernateException.

try {
    session.doWork(
        new Work() {
            public void execute(Connection connection) throws SQLException 
            { 
                doSomething(connection); 
            }
        }
    );
} catch (HibernateException e) {
}

The problem is, I'm getting the following in Eclipse:

No exception of type HibernateException can be thrown; an exception type must be a subclass of Throwable

As far as I can tell, HibernateException chains back to Throwable, so I don't know what it's talking about.

I'm using hibernate-core 6.1.0

Any suggestions would be greatly appreciated.

Update/Fix

So it turns out I didn't have the right library on my classpath. HibernateException extends jakarta.persistence.PersistenceException I did have the following in my POM

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>jakarta.persistence</artifactId>
    <version>2.2.3</version>
</dependency>

But I did NOT have the persistence API in my POM

<dependency>
    <groupId>jakarta.persistence</groupId>
    <artifactId>jakarta.persistence-api</artifactId>
    <version>3.1.0</version>
</dependency>

I had checked my POM before, but didn't realize there was a difference between the two. Thanks rzwitserloot for enticing me to look again.

3
  • The compiler isn't lying to you. Possibly you have written your own class, perhaps to try this out as example, and you named it HibernateException. Commented Jun 24, 2022 at 3:28
  • Thanks. This is an inherited solution, but I did search for a custom HibernateException class that a prior dev may have added, but couldn't find one. When I trace the catch (HibernateException e) object, it's pointing to package org.hibernate; public class HibernateException extends PersistenceException which extends from package javax.persistence; public class PersistenceException extends RuntimeException. These are both Hibernate packages, not something built in the solution
    – Grant
    Commented Jun 24, 2022 at 17:02
  • The compiler error has only two explanations: [A] HibernateException or PersistenceException isn't on the classpath, or [B] you have a class named HibernateException in the same package or otherwise imported it, and it isn't hibernate-core's exception. Commented Jun 24, 2022 at 17:24

0

Browse other questions tagged or ask your own question.