Hibernate cant test my session, it's null - java

Hi im using eclipse this hibernate update.
i have this config:
<?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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">protein_tracker</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
</session-factory>
</hibernate-configuration>
i have this class that has the basics to start hibernate:
package com.simpleprogramer;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtilities {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
static{
try{
Configuration configuration = new Configuration().configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
catch(HibernateException exeption)
{
System.out.println("Problem creating session factory!");
}
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
and main that test hibernate session, and i don't know were is the null:
package com.simpleprogramer;
import org.hibernate.Session;
public class program {
public static void main(String[] args) {
System.out.println("Hola mundo");
Session session = HibernateUtilities.getSessionFactory().openSession();
session.close();
}
}
getting this error from console:
Hola mundo
oct 01, 2013 2:12:31 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
oct 01, 2013 2:12:31 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.6.Final}
oct 01, 2013 2:12:31 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
oct 01, 2013 2:12:31 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
oct 01, 2013 2:12:31 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
oct 01, 2013 2:12:31 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
oct 01, 2013 2:12:31 AM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
oct 01, 2013 2:12:31 AM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
oct 01, 2013 2:12:31 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Problem creating session factory!
Exception in thread "main" java.lang.NullPointerException
at com.simpleprogramer.program.main(program.java:9)
Hi there, thank you for your advice exeption.printStacktrace(System.out);. I figure out that my jdbc driver was not loaded, and then realized that it wasn't loaded at all on the proyect (i thought that the config autoload no matter what or were the driver is). So i downloaded the corresponding jar and it works. Thanks for your advice!

Related

Cannot connect to Informix using Hibernate

I'm trying to connect to Informix database using Hibernate 5.2.10, but the program doesn't respond and gets stuck without throwing any error message.
I can't find anything from the console . Any suggest on what mistake I made? I'm new to Hibernate
hibernate.cfg.xml
<property name="connection.driver_class">com.informix.jdbc.IfxDriver</property>
<property name="connection.url">jdbc:informixsqli://****:1528/****:informixserver=*****</property>
<property name="connection.username">*****</property>
<property name="connection.password">*****</property>
Console:
Sep 17, 2017 11:05:55 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.10.Final}
Sep 17, 2017 11:05:55 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Sep 17, 2017 11:06:05 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Sep 17, 2017 11:06:05 AM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH010002: C3P0 using driver: com.informix.jdbc.IfxDriver at URL: jdbc:informix-sqli://d7uat:1528/d7main:informixserver=uatserver
Sep 17, 2017 11:06:05 AM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH10001001: Connection properties: {user=******, password=****}
Sep 17, 2017 11:06:05 AM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH10001003: Autocommit mode: false
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Sep 17, 2017 11:06:07 AM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH10001007: JDBC isolation level: <unknown>
Sep 17, 2017 11:06:08 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.InformixDialect
Sep 17, 2017 11:06:14 AM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess#7a0ef219] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
HibernateUtil
private static final SessionFactory sessionFactory = buildSessionFactory();
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
private static SessionFactory buildSessionFactory(){
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
try {
return new
MetadataSources(registry).buildMetadata().buildSessionFactory();
}
catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
StandardServiceRegistryBuilder.destroy( registry );
throw new ExceptionInInitializerError(ex);
}
}
InformixTest
public static CustomerInvoice lookupInvoice(String invoiceNumber){
Session session = null;
List<CustomerInvoice> customerInvoiceList = new ArrayList<CustomerInvoice>();
CustomerInvoice customerInvoice = null;
try {
session = HibernateUtil.getSessionFactory().openSession();
Query query = session.createQuery(invoiceByNumber);
query.setParameter("invoiceNumber", invoiceNumber+"%");
customerInvoiceList = ((org.hibernate.query.Query) query).list();
} catch (Exception e) {
e.printStackTrace();
session.close();
} finally {
if (session != null && session.isOpen()) {
session.close();
}
}
if(customerInvoiceList.size()>0){
customerInvoice = customerInvoiceList.get(0);
}
return customerInvoice;
}
I did a quick test with 5.2 and the latest jdbc driver and it appears to work fine for me. I'm using your HiberanateUtil and a similar Hibernate config file:
D:\Infx\work\Hibernate>java list
Sep 18, 2017 10:53:49 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.11.Final}
Sep 18, 2017 10:53:49 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Sep 18, 2017 10:53:49 AM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
Sep 18, 2017 10:53:50 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Sep 18, 2017 10:53:50 AM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-mapping. Use namespace http://www.hibernate.org/dtd/hibernate-mapping instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
Sep 18, 2017 10:53:51 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Sep 18, 2017 10:53:51 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.informix.jdbc.IfxDriver] at URL [jdbc:informix-sqli://420ito:9088/stores7:INFORMIXSERVER=ids1210;user=informix;password=dummy;]
Sep 18, 2017 10:53:51 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {}
Sep 18, 2017 10:53:51 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Sep 18, 2017 10:53:51 AM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Sep 18, 2017 10:53:52 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.InformixDialect
Sep 18, 2017 10:53:53 AM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess#37ebc9d8] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Sep 18, 2017 10:53:55 AM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select state0_.id as id1_0_, state0_.code as code2_0_, state0_.sname as sname3_0_, state0_.next_state_id as next_sta4_0_ from States state0_ order by state0_.id asc
test, test
test, test
Sep 18, 2017 10:53:55 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:informix-sqli://420ito:9088/stores7:INFORMIXSERVER=ids1210;user=informix;password=dummy;]
D:\Infx\work\Hibernate>
Only way I get something like a 'hang' is if I get an exception during the open session(). I forced one compiling the method with an old Hibernate version library (3.x) which doesn't have 'classic' methods:
D:\Infx\work\Hibernate>java list
Sep 18, 2017 10:54:42 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.11.Final}
Sep 18, 2017 10:54:42 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Sep 18, 2017 10:54:42 AM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
Sep 18, 2017 10:54:42 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Sep 18, 2017 10:54:42 AM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-mapping. Use namespace http://www.hibernate.org/dtd/hibernate-mapping instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
Sep 18, 2017 10:54:44 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Sep 18, 2017 10:54:44 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.informix.jdbc.IfxDriver] at URL [jdbc:informix-sqli://420ito:9088/stores7:INFORMIXSERVER=ids1210;user=informix;password=dummy;]
Sep 18, 2017 10:54:44 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {}
Sep 18, 2017 10:54:44 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Sep 18, 2017 10:54:44 AM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Sep 18, 2017 10:54:44 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.InformixDialect
Sep 18, 2017 10:54:46 AM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess#37ebc9d8] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;
at list.main(list.java:12)
....
....
....
As yours, 'last' INFO message is "Connection obtained from JdbcConnectionAccess" , but after that I'm also getting the nosuchmethod exception. I have to press control+c to get out.
Maybe your exceptions are been sent somewhere else ;)
EDIT
This is the code I'm using to test it directly from the console, should be everything you need to run it.
D:\Infx\work\Hibernate>cat set5.cmd
#echo off
set CLASSPATH=
set CLASSPATH=%CLASSPATH%;D:\infx\work\Hibernate\lib5\javassist-3.20.0-GA.jar
set CLASSPATH=%CLASSPATH%;D:\infx\work\Hibernate\lib5\dom4j-1.6.1.jar
set CLASSPATH=%CLASSPATH%;D:\infx\work\Hibernate\lib5\hibernate-jpa-2.1-api-1.0.0.Final.jar
set CLASSPATH=%CLASSPATH%;D:\infx\work\Hibernate\lib5\hibernate-core-5.2.11.Final.jar
set CLASSPATH=%CLASSPATH%;D:\infx\work\Hibernate\lib5\hibernate-commons-annotations-5.0.1.Final.jar
set CLASSPATH=%CLASSPATH%;D:\infx\work\Hibernate\lib5\ifxjdbc.jar
set CLASSPATH=%CLASSPATH%;D:\infx\work\Hibernate\lib5\jta-1.1.jar
set CLASSPATH=%CLASSPATH%;D:\infx\work\Hibernate\lib5\classmate-1.3.0.jar
set CLASSPATH=%CLASSPATH%;D:\infx\work\Hibernate\lib5\jboss-logging-3.3.0.Final.jar
set CLASSPATH=%CLASSPATH%;D:\infx\work\Hibernate\lib5\antlr-2.7.7.jar
set CLASSPATH=%CLASSPATH%;.
D:\Infx\work\Hibernate>cat hibernate.cfg.xml
<?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 >
<property
name="connection.driver_class">com.informix.jdbc.IfxDriver
</property>
<property
name="connection.url">jdbc:informix-sqli://420ito:9088/stores7:INFORMIXSERVER=ids1210;user=informix;password=dummy;
</property>
<property
name="dialect">org.hibernate.dialect.InformixDialect
</property>
<property name="hibernate.show_sql">true
</property>
<property
name="current_session_context_class">thread
</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="State.hbm.xml"/>
</session-factory>
</hibernate-configuration>
D:\Infx\work\Hibernate>cat State.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="State"
table="States">
<id
name="id"
column="id">
<generator class="increment"/>
</id>
<property
name="code"
column="code"/>
<property
name="sname"
column="sname"/>
<many-to-one
name="nextState"
cascade="all"
column="next_state_id"/>
</class>
</hibernate-mapping>
D:\Infx\work\Hibernate>cat HibernateUtil.java
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory();
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
private static SessionFactory buildSessionFactory(){
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
try {
return new
MetadataSources(registry).buildMetadata().buildSessionFactory();
}
catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
StandardServiceRegistryBuilder.destroy( registry );
throw new ExceptionInInitializerError(ex);
}
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
D:\Infx\work\Hibernate>cat create.java
import java.util.*;
import org.hibernate.*;
import javax.persistence.*;
public class create {
public static void main(String[] args) {
java.util.logging.Logger.getLogger("org.hibernate").setLevel(java.util.logging.Level.FINEST );
java.util.logging.Logger.getLogger("org.hibernate").setLevel(java.util.logging.Level.SEVERE );
Session Session = HibernateUtil.getSessionFactory().openSession();
Transaction Transaction = Session.beginTransaction();
State myState = new State();
myState.setcode(args[0]);
myState.setsname(args[1]);
Long stateId = (Long) Session.save(myState);
System.out.println("Stated added: "+myState.getcode()+", "+myState.getsname() );
Transaction.commit();
Session.close();
// Shutting down the application
HibernateUtil.shutdown();
}
}
D:\Infx\work\Hibernate>cat list.java
import java.util.*;
import org.hibernate.*;
import javax.persistence.*;
public class list {
public static void main(String[] args) {
java.util.logging.Logger.getLogger("org.hibernate").setLevel(java.util.logging.Level.FINEST );
java.util.logging.Logger.getLogger("org.hibernate").setLevel(java.util.logging.Level.SEVERE );
Session newSession = HibernateUtil.getSessionFactory().openSession();
Transaction newTransaction = newSession.beginTransaction();
List states = newSession.createQuery("from State order by id asc").list();
for ( Iterator iter = states.iterator();
iter.hasNext(); ) {
State state = (State) iter.next();
System.out.println(state.getcode() +", " + state.getsname() );
}
newTransaction.commit();
newSession.close();
HibernateUtil.shutdown();
}
}
D:\Infx\work\Hibernate>
create.java will insert a new row, and list.java will select from the 'State' table:
D:\Infx\work\Hibernate>java com.informix.jdbc.Version
IBM Informix JDBC Driver Version 4.10.JC9
D:\Infx\work\Hibernate>javac create.java
D:\Infx\work\Hibernate>javac list.java
D:\Infx\work\Hibernate>java create test1 test2
Hibernate: select max(id) from States
Stated added: test1, test2
Hibernate: insert into States (code, sname, next_state_id, id) values (?, ?, ?, ?)
D:\Infx\work\Hibernate>java list
Hibernate: select state0_.id as id1_0_, state0_.code as code2_0_, state0_.sname as sname3_0_, state0_.next_state_id as next_sta4_0_ from States state0_ order by state0_.id asc
test, test
test, test
test1, test2
D:\Infx\work\Hibernate>
but I guess it shouldn't be much different than what you already have.
Thanks for trying to help me out. The issue got resolved after I removed this property from hibernate config file.
<property name="hbm2ddl.auto">update</property>

Hibernate working with queries

Started to learn hibernate. Doing everything by tutorial, but when I try to do simple select, nothing happens. Сan't understand what the problem is.
Logs:
мар 28, 2016 12:54:44 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.1.0.Final}
мар 28, 2016 12:54:44 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
мар 28, 2016 12:54:44 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
мар 28, 2016 12:54:45 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/taskboard]
мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
мар 28, 2016 12:54:45 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
мар 28, 2016 12:54:45 PM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 5.2.4.Final
мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/taskboard]
User.java
#Entity
#Table(name="Users")
public class User {
#Id
#Column(name="id_user")
private Long id;
#Column(name="first_name")
private String firstName;
#Column(name="last_name")
private String lastName;
#Column(name="login")
private String login;
#Column(name="password")
private String password;
}//get&set
Main.java
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
List<User> users = null;
try {
session.beginTransaction();
Criteria criteria = session.createCriteria(User.class);
users = criteria.list();
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
e.printStackTrace();
} finally {
session.close();
sessionFactory.close();
}
for (User user: users) {
System.out.println(user.toString());
}
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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/taskboard</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="User" />
</session-factory>
</hibernate-configuration>
HibernateUtil.java
public class HibernateUtil {
private static SessionFactory sessionFactory = null;
static {
Configuration cfg = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties());
sessionFactory = cfg.buildSessionFactory(builder.build());
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
As far as I remember, the newer version of hibernate do not support <mapping class= .../> in the hibernate configuration file any longer.
Can you remove that and instead try with
cfg.addAnnotatedClass(User.class) in your HibernateUtil static block.
On a different note, you may want to consider using the latest approach of building the sessionFactory as Configuration way is deprecated. Refer to Native Bootstrapping section for more information.
May be nothing in data base for Users table.First Insert some data. Then check.

Hibernate Exception in thread "main" java.lang.ExceptionInInitializerError

I am brand new to Hibernate so please go easy on me!
I am just trying to open and close a hibernate session as follows:
Program.java is as follows:
package com.simpleprogrammer;
import org.hibernate.Session;
public class Program {
public static void main(String[] args) {
System.out.println("Hello world!");
Session session = HibernateUtilities.getSessionFactory().openSession();
session.close();
}
}
and my HibernateUtilities.java file is as follows:
package com.simpleprogrammer;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateUtilities {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
static {
try
{
Configuration configuration = new Configuration().configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
catch (HibernateException exception)
{
System.out.println("Problem creating session factory");
}
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
}
and finally my hibernate.cfg.xml file is as follows:
<?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>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.password">password</property>
<property name="connection.url">jdbc:mysql://localhost:3306</property>
<property name="connection.username">appuser</property>
<property name="connection.default_schema">protien_tracker</property>
<property name="connection.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.show_sql">true</property>
<!-- <property name="hbm2ddl.auto">update</property> -->
</session-factory>
</hibernate-configuration>
When I try to run this, I am getting a null pointer exception, stack trace is as follows:
"C:\Program Files\Java\jdk1.8.0_45\bin\java" -Didea.launcher.port=7537 "-Didea.launcher.bin.path=C:\Users\John Stafford\AppData\Roaming\JetBrains\IntelliJ IDEA 15.0.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_45\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_45\jre\lib\rt.jar;C:\Java\intellij\ProtienTracker\out\production\ProtienTracker;C:\Java\intellij\ProtienTracker\lib\hibernate-jpa-2.0-api-1.0.1.Final.jar;C:\Java\intellij\ProtienTracker\lib\antlr-2.7.7.jar;C:\Java\intellij\ProtienTracker\lib\hibernate-commons-annotations-4.0.2.Final.jar;C:\Java\intellij\ProtienTracker\lib\jboss-logging-3.1.0.GA.jar;C:\Java\intellij\ProtienTracker\lib\dom4j-1.6.1.jar;C:\Java\intellij\ProtienTracker\lib\jboss-transaction-api_1.1_spec-1.0.1.Final.jar;C:\Java\intellij\ProtienTracker\lib\javassist-3.15.0-GA.jar;C:\Java\intellij\ProtienTracker\lib\hibernate-core-4.2.2.Final.jar;C:\Java\intellij\ProtienTracker\lib\mysql-connector-java-5.0.4-bin.jar;C:\Users\John Stafford\AppData\Roaming\JetBrains\IntelliJ IDEA 15.0.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.simpleprogrammer.Program
Hello world!
Jan 20, 2016 9:03:37 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Jan 20, 2016 9:03:37 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.2.Final}
Jan 20, 2016 9:03:37 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jan 20, 2016 9:03:37 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jan 20, 2016 9:03:37 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Jan 20, 2016 9:03:37 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Jan 20, 2016 9:03:38 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Jan 20, 2016 9:03:38 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Jan 20, 2016 9:03:38 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
Jan 20, 2016 9:03:38 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
Jan 20, 2016 9:03:38 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306]
Jan 20, 2016 9:03:38 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=appuser, password=****, dialect=org.hibernate.dialect.MySQLDialect, show_sql=true, default_schema=protien_tracker}
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.simpleprogrammer.Program.main(Program.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.NullPointerException
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:214)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:242)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:117)
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1797)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1755)
at com.simpleprogrammer.HibernateUtilities.<clinit>(HibernateUtilities.java:18)
... 6 more
Process finished with exit code 1
Does anybody know what I have done wrong here?
Following is an example for creating session: HibernateUtil
public class HibernateUtil {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if(sessionFactory==null)
{
try{
sessionFactory = new AnnotationConfiguration().
configure().
//addPackage("com.xyz") //add package if used.
addAnnotatedClass(User.class).
buildSessionFactory();
}catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
}
return sessionFactory;
}
}
Using session
public class Dao {
public void save(Object obj) {
SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
Session session=sessionFactory.openSession();
try{
session.beginTransaction();
session.save(obj);
session.getTransaction().commit();
session.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Make sure you have jdbc jar in lib
If you are using Hibernate 4.3++, change the following lines in HibernateUtilities class.
configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
StandardServiceRegistryBuilder serviceBuilder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
sessionFactory = configuration.buildSessionFactory(serviceBuilder.build());
And make the following changes to hibernate.cfg.xml
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/protien_tracker</property>
<property name="connection.username">appuser</property>
<property name="connection.password">password</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>

Hibernate: Connection to DB2 database fails (dialect?)

I am desperate trying to connect to a DB2 database via hibernate. Problem is here, I know, where the error comes from but I can't solve it or rather don't understand it. Maybe you can help me fix this problem. I would be very grateful!
The following code shows my hibernate.cfg.xml:
<!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.dialect">org.hibernate.dialect.DB2Dialect</property>
<property name="hibernate.connection.driver_class">com.ibm.db2.jcc.DB2Driver</property>
<property name="hibernate.connection.url">jdbc:db2://db2.example.com:50001/databasename</property>
<property name="hibernate.connection.username">mydbuser</property>
<property name="hibernate.connection.password">mydbpass</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="media.definitions.Product" />
</session-factory>
</hibernate-configuration>
This following code is a class which I've built by a tutorial:
package media.frontend;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
StandardServiceRegistryBuilder sb = new StandardServiceRegistryBuilder();
sb.applySettings(cfg.getProperties());
StandardServiceRegistry standardServiceRegistry = sb.build();
sessionFactory = cfg.buildSessionFactory(standardServiceRegistry);
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
And the following error comes immediately when I start the program (which only tries to catch information of a single column). I've pasted the whole output:
Aug 14, 2014 5:45:45 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Aug 14, 2014 5:45:45 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.6.Final}
Aug 14, 2014 5:45:45 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Aug 14, 2014 5:45:45 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Aug 14, 2014 5:45:45 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
Aug 14, 2014 5:45:45 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
Aug 14, 2014 5:45:45 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.ibm.db2.jcc.DB2Driver] at URL [jdbc:db2://anger.informatik.uni-leipzig.de:50001/PRAK14B]
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=dbprak12, password=****}
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 10 (min=1)
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.internal.JdbcServicesImpl configure
WARN: HHH000341: Could not obtain connection metadata : DatabaseMetaData information is not known for server DB2/SUNX8664SQL10010 by this version of JDBC driver
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000422: Disabling contextual LOB creation as connection was null
Exception in thread "main" java.lang.ExceptionInInitializerError
at media.frontend.HibernateTest.main(HibernateTest.java:15)
Caused by: java.lang.NullPointerException
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:244)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
at media.frontend.HibernateUtil.<clinit>(HibernateUtil.java:17)
... 1 more
Thanks in advance for your help!
Greets,
Luke
I was facing similar issue in my code. Then i tried updating the recent versions of DB2 jars into my server and the code started working. Try to update your DB2 jar.

Exception in thread "main" org.hibernate.InvalidMappingException: Unable to read XML

> I am using eclipse .I tried to do java hibernate program for the first time but got the above error.please help me to solve it.
downloaded hibernate release 4.2.5 final.zip and added jar files to user library
antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.5.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
hibernate-entitymanager-4.2.5.Final.jar
hibernate-envers-4.2.5.Final.jar
hibernate-osgi-4.2.5.Final.jar
org.osgi.core-4.3.1.jar
is these jar files enough for hibernate program.
hibernate.cfg.xml
<?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>
<!-- Related to the connection START -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">jdbc:oracle:thin:#192.168.1.2:1521:xe</property>
<property name="connection.user">system</property>
<property name="connection.password">siddhu</property>
<!-- Related to the connection END -->
<!-- Related to hibernate properties START -->
<property name="show_sql">true</property>
<property name="dialet">org.hibernate.dialect.OracleDialect</property>
<property name="hbm2ddl.auto">update</property>
<!-- Related to hibernate properties END -->
<!-- Related to mapping START -->
<mapping resource="Details.hbm.xml"/>
<!-- Related to the mapping END -->
</session-factory>
</hibernate-configuration>
Details.hbm.xml
- <hibernate-mapping>
- <class name="Details" table="stu">
- <id name="stNo" column="SNo">
<generator class="assigned" />
</id>
<property name="stName" column="SName" />
<property name="stAddress" />
</class>
</hibernate-mapping>
mainclass.java
import org.hibernate.*;
import org.hibernate.cfg.*;
public class mainclass {
public static void main(String[] args)
{
Configuration c = new Configuration();
c.configure("hibernate.cfg.xml");
SessionFactory factory = c.buildSessionFactory();
Session session = factory.openSession();
Details p=new Details();
p.setStno(101);
p.setStName ("iPhone");
p.setStAddress("dfdsgdf");
Transaction tx = session.beginTransaction();
session.save(p);
System.out.println("Object saved successfully.....!!");
tx.commit();
session.close();
factory.close();
}
}
Details.java
public class Details {
private int stNo;
private String stName;
private String stAddress;
public void setStno(int stNo)
{
this.stNo=stNo;
}
public int getStNo()
{
return stNo;
}
public void setStName(String stName)
{
this.stName=stName;
}
public String getStName()
{
return stName;
}
public void setStAddress(String stAddress)
{
this.stAddress=stAddress;
}
public String getStAddress()
{
return stAddress;
}
}
Console Error
===========================================
Sep 20, 2013 6:49:30 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Sep 20, 2013 6:49:30 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.5.Final}
Sep 20, 2013 6:49:30 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Sep 20, 2013 6:49:30 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Sep 20, 2013 6:49:30 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
Sep 20, 2013 6:49:30 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
Sep 20, 2013 6:49:31 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Sep 20, 2013 6:49:31 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Details.hbm.xml
Exception in thread "main" org.hibernate.InvalidMappingException: Unable to read XML
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:109)
at org.hibernate.cfg.Configuration.add(Configuration.java:488)
at org.hibernate.cfg.Configuration.add(Configuration.java:484)
at org.hibernate.cfg.Configuration.add(Configuration.java:657)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:740)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2188)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2160)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2140)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2093)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2008)
at mainclass.main(mainclass.java:9)
Caused by: org.dom4j.DocumentException: Error on line 1 of document : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
... 10 more
Your XML file, for whatever reason, might contain a BOM as described in this answer and its linked article.
I suggest you create a new file and write (copy) over the configuration from your mapping file.

Categories

Resources