Initial SessionFactory creation failed: org.hibernate.MappingException: invalid configuration - java

SOLVED
This question is about java hibernate.
When i run the Main class i get this error
Initial SessionFactory creation failed: org.hibernate.MappingException:
invalid configuration
Exception in thread "main" java.lang.NullPointerException
at principal.ClienteDAO.guardaCliente(ClienteDAO.java:38)
at principal.Main.main(Main.java:31)
C:\Users\Nico\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)
Have no idea why im getting that exception
my hibernate.cfg.xml is
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--TODA LA INFORMACION FUE SACADA DE: http://www.javatutoriales.com/2009/05/hibernate-parte-1-persistiendo-objetos.html-->
<!-- parametros para la conexion a la base de datos -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/basededatosprueba</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- Configuracion del pool interno -->
<property name="connection.pool_size">1</property>
<!-- Dialecto de la base de datos -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Otras propiedades importantes -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create-drop</property>
<!-- Archivos de mapeo -->
<mapping resource="mapeos/Cliente.hbm.xml"/>
</session-factory>
</hibernate-configuration>
EDIT 2:
So, i dont know why, but i change the hibernate.cfg.xml and now it works, here it is
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/basededatosprueba</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="mapeos/Cliente.hbm.xml"/>
</session-factory>
</hibernate-configuration>
solved? we'll see

I think you might need to close your hibernate-configuration tag in your xml.

Related

Hibernate's createSQLQuery won't retreive data

Hibernate won't retrieve data even though the data exists in the table. Running the query directly from SQL Server Management Studio shows the expected results. Any idea what am I missing?
Java:
List<User> userCollection= session.createSQLQuery("select column1, column2 from myTable").addEntity(User.class).list();
Hibernate.cfg.xml
<?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">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name = "hibernate.connection.url">jdbc:sqlserver://server:1433;databaseName=MyDatabase</property>
<property name = "hibernate.connection.username">user</property>
<property name = "hibernate.connection.password">123</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">validate</property> -->
<!-- Names the annotated entity class -->
<mapping class="com.app.User"/>
</session-factory>
</hibernate-configuration>

Cannot find the declaration of element 'hibernate-configuration' but it exists

This is my configuration:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-5.0.dtd">
<hibernate-configuration
xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>
<mapping class="Mapping.AccountMap"
</session-factory>
</hibernate-configuration>
I read that i should change the "Doctype" and i did from other answers i have found, yet this error occurs:
Unable to perform unmarshalling at line number 8 and column 63 in RESOURCE hibernate.cfg.xml. Message: cvc-elt.1: Cannot find the declaration of element 'hibernate-configuration'.
Your dtd url is false ... seems like hibernate-configuration-5.0.dtd doesn't exist ... use this :
http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd
change the whole header :
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-5.0.dtd">
<hibernate-configuration
xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
TO
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
Try to add this header block. It works for me and also check parse error in your XML 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">
Here i am posting some sample xml file may be it will help you.
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- Names the annotated entity class -->
<mapping class="org.java.dto.UserDetails"/>
</session-factory>
</hibernate-configuration>

first Hibernate test gives error [EDITED]

I'm an Hibernate beginner user, I created a very simple application to test it !
But I get this error in the console :
Initial SessionFactory creation failed.org.hibernate.HibernateException: Dialect class not found: org.openmeetings.app.hibernate.utils.MySQL5MyISAMDialect
Exception in thread "main" java.lang.ExceptionInInitializerError
at util.HibernateUtil.buildSessionFactory(HibernateUtil.java:20)
at util.HibernateUtil.<clinit>(HibernateUtil.java:10)
at DAO.services.addProduit(services.java:9)
at test.main(test.java:11)
Caused by: org.hibernate.HibernateException: Dialect class not found: org.openmeetings.app.hibernate.utils.MySQL5MyISAMDialect
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:81)
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:42)
at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:422)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:128)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
at util.HibernateUtil.buildSessionFactory(HibernateUtil.java:15)
... 3 more
My Test class :
import DAO.services;
import DAO.Produit;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
services s = new services();
Produit p = new Produit("PC","Sony Vaio",(double )7500);
s.addProduit(p);
}
}
Hibernate.cfg.xml :
<?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">
<!-- Generated file - Do not edit! -->
<hibernate-configuration>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory>
<!-- User / Password -->
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- Database Settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- for performance reasons changed to MyISAM from org.hibernate.dialect.MySQLInnoDBDialect -->
<property name="dialect">org.openmeetings.app.hibernate.utils.MySQL5MyISAMDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/gestProd</property>
<property name="hibernate.connection.CharSet">utf8</property>
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.useUnicode">true</property>
<!-- Database Scheme Auto Update -->
<property name="hbm2ddl.auto">update</property>
<!-- properties -->
<property name="show_sql">true</property>
<property name="use_outer_join">false</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!--
<property name="connection.provider_class ">org.hibernate.connection.C3P0ConnectionProvider</property>
-->
<property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.generate_statistics">false</property>
<property name="hibernate.cache.use_structured_entries">false</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">2</property>
<property name="hibernate.c3p0.idle_test_period">100</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.timeout">100</property>
<!-- mapping files -->
<mapping resource="DAO/Categorie.hbm.xml"/>
<mapping resource="DAO/Produit.hbm.xml"/>
</session-factory>
</hibernate-configuration>
service.java :
package DAO;
import org.hibernate.Session;
import util.HibernateUtil;
public class services {
public void addProduit(Produit p){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(p);
session.getTransaction().commit();
}
}
AND finally 2 classes for my objects ( Produit and Categorie ) and their Configuration files !
What do you think may be the source of this error ?
Thank you :)
The dialect for MySql with MyISAM(if that is what you have) should be:
org.hibernate.dialect.MySQLMyISAMDialect

Hibernate, Could not parse configuration

I am new to Hibernate .... i tried this code but i got Could not parse configuration: NewFile.cfg.xml (name of .cfg.xml 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>
<!-- connection to oracle -->
<propertyname="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver </property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#localhost:1521:XE
</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.connection.password">system</property>
<!-- autocommit -->
<property name="hibernate.connection.autocommit">false</property>
<!-- to display sql query generated while running program -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<!-- for jdbc transaction -->
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.
JDBCTransactionFactory</property>
<!-- mapping file -->
<mapping resource="Mapping.hbm.xml"/>
</session-factory>
The end tag for <hibernate-configuration> is missing:
</hibernate-configuration>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver </property>
Your opening tag not match the ending tag.

Hibernate 4, EnversIntegrator could not be instantiated

I'm trying to set up Hibernate, and since 4.0.0 just came out I naturally decided to go with that. It seems that no matter what way I try to create a SessionFactory, it always leads to the same error:
Initial SessionFactory creation failed.java.util.ServiceConfigurationError: org.hibernate.integrator.spi.Integrator: Provider org.hibernate.envers.event.EnversIntegrator could not be instantiated: java.lang.ClassCastException: Cannot cast org.hibernate.envers.event.EnversIntegrator to org.hibernate.integrator.spi.Integrator
It seems like there is something wrong with my Hibernate configuration but I can't figure out what. Here's my hibernate.cfg.xml:
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://192.168.0.17:5432/mydb</property>
<property name="connection.username">myusrname</property>
<property name="connection.password">mypasswd</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- c3p0 configuration -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>
Does anyone spot anything out of the ordinary, or do you have other ideas?
I found no way to get Hibernate 4.0.0 CR4 working. Switching to 3.6.7 resolved the problem and works just fine. I conclude that there must be some bug in the release, and will file a bug report.

Categories

Resources