Matteo Pacini Matteo Pacini - 7 months ago 25
Java Question

Hibernate 4: ViolatedConstraintNameExtracter exception

I'm trying to setup Hibernate 4 in a Maven project.
It doesn't work and it's driving me mad.

I get this exception:

java.lang.NoClassDefFoundError: org/hibernate/exception/ViolatedConstraintNameExtracter


These are the dependencies declared in the pom.xml file:

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.1.Final</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.7.2</version>
</dependency>
<dependency>
<groupId>org.hibernate.dialect</groupId>
<artifactId>sqlite-dialect</artifactId>
<version>0.1.0</version>
<scope>provided</scope>
</dependency>


This is my Hibernate configuration file:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.sqlite.JDBC</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.url">jdbc:sqlite:test.db</property>
<property name="hibernate.connection.username"></property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLiteDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>


And this is the "crashy" code:

Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);


I have only a test model, called "User", and is decorated with Hibernate annotations.

Can you help me with this? Cheers.

UPDATE: Stack trace

Initial SessionFactory creation failedjava.lang.NoClassDefFoundError: org/hibernate/exception/ViolatedConstraintNameExtracter
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.matteopacini.RestaurantManager.HibernateUtil.<clinit>(HibernateUtil.java:27)
at com.matteopacini.RestaurantManager.App.main(App.java:14)
Caused by: java.lang.NoClassDefFoundError: org/hibernate/exception/ViolatedConstraintNameExtracter
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:242)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor (StrategySelectorImpl.java:125)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveDefaultableStrategy(StrategySelectorImpl.java:155)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:136)
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:78)
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:68)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:165)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
at com.matteopacini.RestaurantManager.HibernateUtil.<clinit>(HibernateUtil.java:21)
... 1 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.exception.ViolatedConstraintNameExtracter
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 16 more

Answer

The dialect you're using is not compatible with Hibernate 4. See https://github.com/gwenn/sqlite-dialect/blob/master/src/main/java/org/hibernate/dialect/SQLiteDialect.java: it uses org.hibernate.exception.ViolatedConstraintNameExtracter, which has been moved to org.hibernate.exception.spi.ViolatedConstraintNameExtracter in Hibernate 4.

Comments