Java hibernate throws Exception at boot - java

I'm working on a project with Hibernate now but I get an error when running my project.
On server.java (my entry point) I got this:
private static HibernateUtil hibernateUtil;
And in my entry point:
hibernateUtil = new HibernateUtil();
This is my HibernateUtil class:
public class HibernateUtil {
private SessionFactory sessionFactory;
public HibernateUtil() {
StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
try {
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
}
catch (Exception e) {
StandardServiceRegistryBuilder.destroy( registry );
}
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Now my Hibernate.cf.xml:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/dynamicdb</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="show_sql">false</property>
<property name="connection.pool_size">1</property>
</session-factory>
And now this is the error I get:
Jun 18, 2016 1:17:17 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.0.Final}
Jun 18, 2016 1:17:17 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jun 18, 2016 1:17:17 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jun 18, 2016 1:17:18 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Jun 18, 2016 1:17:18 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Jun 18, 2016 1:17:18 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.cj.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/dynamicdb]
Jun 18, 2016 1:17:18 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Jun 18, 2016 1:17:18 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Jun 18, 2016 1:17:18 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Jun 18, 2016 1:17:18 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/dynamicdb]
Jun 18, 2016 1:17:18 PM org.hibernate.service.internal.AbstractServiceRegistryImpl stopService
INFO: HHH000369: Error stopping service [class org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] : java.lang.NullPointerException
I'm running Eclipse mars 2 on Mac OS X 10.10 with MAMP. MySQL is running and the database user and database exists and the password is correct. What's the problem?

I have the same problem like you. It seems that we both copy the code from Hibernate Official code samples. I think maybe it's bug of mysql connecotrj 6.0.After I add throw statement to the catch statement:
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure()
.build();
try {
sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
} catch (Exception e) {
StandardServiceRegistryBuilder.destroy(registry);
throw new RuntimeException(e);
}
And I found the origin exception:
Caused by: com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
This case doesn't appear in mysql connectorj 5.1
So, there are two solutions:switch mysql jdbc drivers to 5.1 or add timezone argument to jdbc url string like this. In hibernate.cfg.xml file don't forget to change & to &

I got the same issue when my mapping file contained one column twice
<property column="STATE" name="state" type="java.lang.String" not-null="true"/>
<property column="BRANCH" name="branch" type="java.lang.String" not-null="true"/>
<property column="COUNTRY" name="country" type="java.lang.String" not-null="true"/>
<property column="STATE" name="state" type="java.lang.String" not-null="true"/>
So once I changed one state to appropriate property e.g.
<property column="SITE" name="site" type="java.lang.String" not-null="true"/>
The issue was solved.
So be careful with your mappings

In my case, I was using Netbeans and had my entities generated using Netbeans only.
after fiddling with mysql and hibernate-core jar versions, after commenting #NamedQueries fixed it for me.

For others who may still encounter this issue, the database provided in the connection URL needs to exist, in this case, we need to ensure the database dynamicdb exists. I had the same issue and this fixed it.

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>

Initial SessionFactory creation failed.org.hibernate.service.spi.ServiceException: Unable to create requested service

I'm getting this error in my Hibernate desktop application:
Initial SessionFactory creation failed.org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
It's not creating SessionFactory and giving this exception:
Executing E:\Vishal\summer\EmpMan\dist\run1432002073\EmpMan.jar using platform C:\Program Files (x86)\Java\jdk1.8.0_131\jre/bin/java
Jul 04, 2017 2:24:35 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.10.Final}
Jul 04, 2017 2:24:35 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jul 04, 2017 2:24:35 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Jul 04, 2017 2:24:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Jul 04, 2017 2:24:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.h2.Driver] at URL [jdbc:h2:~/EmpMan]
Jul 04, 2017 2:24:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=sa, password=****}
Jul 04, 2017 2:24:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Jul 04, 2017 2:24:35 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Initial SessionFactory creation failed.org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
That's it. I'm getting only this much information.
My hibernate.cfg.xml file configuration:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:~/EmpMan</property>
<property name="connection.username">sa</property>
<property name="connection.password">sa</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.H2Dialect</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>
<!-- Names the annotated entity class -->
<mapping class="models.AdminDetails"/>
<mapping class="models.EmployeeDetails" />
<mapping class="models.DepartmentDetails" />
<mapping class="models.DailyDataDetails"/>
</session-factory>
</hibernate-configuration>
Notice:
=> I'm using h2 database and I want to configure embedded database
=> I'm using netbeans and Java8 with hibernate version 5.2.10
=> I'm new to h2 database
=> Where to locate my EmpMan database in my project?
Problem Solved : I have already exist same named database so i changed name and now it's working :) Thank you guys

Hibernate Configuration: Jar files?

im noobie here but i have been finding the fix of my problem here i dont see it... Im new with hibernate and im using my free time to learn this.
package negocio;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.Service;
import org.hibernate.service.ServiceRegistry;
import integracion.HibernateUtil;
import integracion.dao.BodegaDAOImpl;
import integracion.dao.VinoDAO;
import integracion.dao.VinoDAOImpl;
import integracion.entidades.Bodega;
public class Main {
public static void main(String[] args) {
SessionFactory sessionFactory;
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry
= new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
//The problem is here
**sessionFactory = configuration.buildSessionFactory(serviceRegistry);**
Session session = sessionFactory.openSession();
BodegaDAOImpl bodegaDao = new BodegaDAOImpl<>();
bodegaDao.get(1);
}
}
This is the list of the jar added1
And this is the mapping 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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/vinoteca</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.show_sql">true</property>
<mapping class="integracion.entidades.Vino"/>
<mapping class="integracion.entidades.Bodega"/>
<mapping class="integracion.entidades.Denominacion"/>
</session-factory>
</hibernate-configuration>
dic 15, 2016 12:37:51 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.9.Final}
dic 15, 2016 12:37:51 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
dic 15, 2016 12:37:51 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
dic 15, 2016 12:37:51 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
dic 15, 2016 12:37:51 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
dic 15, 2016 12:37:51 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/vinoteca]
dic 15, 2016 12:37:51 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
dic 15, 2016 12:37:51 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
dic 15, 2016 12:37:51 AM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Thu Dec 15 00:37:51 CET 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
dic 15, 2016 12:37:51 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/NamedStoredProcedureQuery
at org.hibernate.cfg.AnnotationBinder.bindDefaults(AnnotationBinder.java:259)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.prepare(AnnotationMetadataSourceProcessorImpl.java:184)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.prepare(MetadataBuildingProcess.java:156)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:253)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at negocio.Main.main(Main.java:31)
Caused by: java.lang.ClassNotFoundException: javax.persistence.NamedStoredProcedureQuery
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 9 more
So.. this is the problem. I was seeing that the problem when eclipse says java.lang.ClassNotFoundException: javax.persistence.NamedStoredProcedureQuery
was because the jar (javax.persistence.jar or hibernate-jpa-2.1-api-1.0.0...jar) was not added, but i added them and i dont resolve this.
anyone could say me why is working bad? Thanks!
PD:Sorry for my english, im not Native english speaker =)
Use a build system like maven or gradle to build your project, and add a dependency on hibernate. That will almost certainly solve all of your basic dependency-related classpath problems.
Add the required jar files into the classpath.

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: Disable logging to console WITHOUT log4j

I have hibernate and I would like to decrease the logging that happens in console everytime it runs. I do not have log4j in my application nor do I want to add it in.
Here is an example of the logs I get everytime I run my app
Feb 25, 2014 6:36:41 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Feb 25, 2014 6:36:41 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.1.Final}
Feb 25, 2014 6:36:41 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Feb 25, 2014 6:36:41 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Feb 25, 2014 6:36:41 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Feb 25, 2014 6:36:41 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Feb 25, 2014 6:36:42 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!
Feb 25, 2014 6:36:42 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Feb 25, 2014 6:36:42 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Feb 25, 2014 6:36:42 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost:5432/hibernatedb]
Feb 25, 2014 6:36:42 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=Sidney, password=****}
Feb 25, 2014 6:36:42 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Feb 25, 2014 6:36:42 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Feb 25, 2014 6:36:43 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
Feb 25, 2014 6:36:43 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
Feb 25, 2014 6:36:43 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Feb 25, 2014 6:36:43 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Feb 25, 2014 6:36:44 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Feb 25, 2014 6:36:44 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Here is my 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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<!-- <property name="connection.url">jdbc:mysql://localhost:3306/hibernateDB</property> -->
<property name="connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
<property name="connection.username">**</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> -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</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 the existing tables and create new one -->
<property name="hbm2ddl.auto">create</property>
<!-- Mention here all the model classes along with their package name -->
<!-- <mapping class="edu.cs157b.hibernate.Student"/> -->
<mapping class="edu.cs157b.hibernate.Doctor"/>
</session-factory>
</hibernate-configuration>
As per your log details I understood that hibernate version is- Hibernate 4.0.4.Final.Since version 4.0, Hibernate has used the JBoss logging library and so even your application does not contain any log4j still hibernate is using JBoss logging provider as default provider.
For more please go through this link:
http://docs.jboss.org/hibernate/orm/4.3/topical/html/logging/Logging.html
Hope this info will help you to understand why logs are still available without any log4j.
You can try to disable all Logger functions using java.util.logging :
private void disableLogging() {
LogManager logManager = LogManager.getLogManager();
Logger logger = logManager.getLogger("");
logger.setLevel(Level.SEVERE); //could be Level.OFF
}
It would affect any other Logger you're using, but in some cases may be useful.

Categories

Resources