hibernate error on hibernate.cfg.xml - java

i get this error when i try to start compiling my project
dic 27, 2017 11:55:50 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.12.Final}
dic 27, 2017 11:55:50 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Initial SessionFactory creation
failed.org.hibernate.internal.util.config.ConfigurationException: Unable to
perform unmarshalling at line number 0 and column 0 in RESOURCE
hibernate.cfg.xml. Message: null
Exception in thread "main" java.lang.ExceptionInInitializerError
at progetto1java.HibernateUtil.buildSessionFactory(HibernateUtil.java:15)
at progetto1java.HibernateUtil.<clinit>(HibernateUtil.java:8)
at progetto1java.Gestore_Utenti.VerificaUtente(Gestore_Utenti.java:59)
at progetto1java.Sistema.main(Sistema.java:48)
Caused by: org.hibernate.internal.util.config.ConfigurationException: Unable
to perform unmarshalling at line number 0 and column 0 in RESOURCE
hibernate.cfg.xml. Message: null
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:133)
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65)
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
at org.hibernate.cfg.Configuration.configure(Configuration.java:258)
at org.hibernate.cfg.Configuration.configure(Configuration.java:244)
at progetto1java.HibernateUtil.buildSessionFactory(HibernateUtil.java:12)
... 3 more
Caused by: javax.xml.bind.JAXBException
- with linked exception:
[java.lang.ClassNotFoundException:
com.sun.xml.internal.bind.v2.ContextFactory]
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:241)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:477)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:656)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:599)
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:122)
... 9 more
Caused by: java.lang.ClassNotFoundException:
com.sun.xml.internal.bind.v2.ContextFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
at javax.xml.bind.ContextFinder.safeLoadClass(ContextFinder.java:594)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:239)
... 13 more
i look carefully my configuration file but i don't now what's the problem..
these are in order my configuration file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/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/sistema_musicale</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">1</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hbm2ddl.auto">create</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping class="progetto1java.Utente"/>
<mapping class="progetto1java.Brano"/>
</session-factory>
</hibernate-configuration>
and hibernate class util.
class hibernate to create session
sorry for my using of image instead of code but i had indentation problem...

I just resolved an
failed.org.hibernate.internal.util.config.ConfigurationException: Unable to
perform unmarshalling at line number 0 and column 0 in RESOURCE
hibernate.cfg.xml.
error building my own app.
The solution was actually very simple, namely I was missing dependencies in my pom.xml file. I had just one:
...
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
...
Adding the three below solved the problem:
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
I hope this will help someone.

Please ensure that your "hibernate.cfg.xml" file is present in the "src" folder of your project.

Related

Facing hibernate error: Caused by: java.lang.ClassNotFoundException: net.bytebuddy.NamingStrategy$SuffixingRandom$BaseNameResolver

I am getting an error while running this code in Eclipse. I have created Student.java file:
public class Student {
private int id;
private String name;
private String email;
private int marks;
// getters/setters
}
I have created Student.hbm.xml file:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="bean.Student" table="student">
<id name="id" column="sid"></id>
<property name="name" column="sname"></property>
<property name="email" column="semail"></property>
<property name="marks" column="smarks"></property>
</class>
</hibernate-mapping>
I have created hibernate.cfg.xml file:
<!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">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:#localhost:1521:wind</property>
<property name="connection.username">localuser</property>
<property name="connection.password">localuser</property>
<property name="connection.poolsize">5</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<mapping resource="resources/student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
I have created client.java file:
package testclass;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import bean.Student;
public class Client {
public static void main(String[] args) {
Student st = new Student();
st.setId(11);
st.setEmail("swapnilgthaware#gmail.com");
st.setMarks(98);
st.setName("Swapnil");
// Student object is transient here..
// When it is attached to hibernate object then it will become persistent object.
Configuration cfg = new Configuration();
cfg.configure("resources/hibernate.cfg.xml");
SessionFactory sf = cfg.buildSessionFactory();
Session s =sf.openSession();
s.save(st);
// Student object is persisten now. Even gc() will not take away this object
s.beginTransaction().commit();
// Student object will goto Database side.
s.evict(st);
}
}
I tried adding many jars file but I am unable see student record in my oracle database.
Full error:
Jul 17, 2018 8:11:09 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.3.2.Final}
Jul 17, 2018 8:11:09 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Exception in thread "main" java.lang.NoClassDefFoundError: net/bytebuddy/NamingStrategy$SuffixingRandom$BaseNameResolver
at org.hibernate.cfg.Environment.buildBytecodeProvider(Environment.java:357)
at org.hibernate.cfg.Environment.buildBytecodeProvider(Environment.java:352)
at org.hibernate.cfg.Environment.<clinit>(Environment.java:246)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.<init>(StandardServiceRegistryBuilder.java:78)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.<init>(StandardServiceRegistryBuilder.java:67)
at org.hibernate.cfg.Configuration.reset(Configuration.java:158)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:124)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:118)
at testclass.Client.main(Client.java:21)
Caused by: java.lang.ClassNotFoundException: net.bytebuddy.NamingStrategy$SuffixingRandom$BaseNameResolver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 9 more
I had the same problem with this library.
In the Java Build Path (Project Properties ) did you add the jar files to the Modulepath or to the Classpath ? I initially added the jars under the Modulepath. While debugging the issue I decided to move the jars into the Classpath . This resolved the problem.
Another option is to convert the project to a Maven project
Right click the project name in the package explorer
Scroll down to Configure
Select Convert to Maven project
Accept all the defaults and click on Finish
I have the same problem: turns out the net-buddy library file was corrupted (error during download?).
Try deleting jars in ~/.m2/repository/net/bytebuddy and rebuild the application.
Hope this helps.
Place the hibernate jar files in the classpath. This will most likely resolve the issue.
In Java Modular System this still pops up -
You do need to in your module-info specify your module uses bytebuddy
requires net.bytebuddy;
which will force you to place it in the classpath :)
Try to add byte-buddy-X.XX.XX.jar to the Classpath of the project. If U work in Eclipse, just: right click on the project name, then select Properties -> Java Build Path -> Libraries and add to the Classpath this this file.
For me was helpful to delete byte-buddy-X.XX.XX.jar from Modulepath and then added it to the Classpath.
The bytebuddy dependency is used by Hibernate. I had a similar problem and I've solved my problem by adding the following dependency in my pom.xml.
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-dep</artifactId>
<version>1.10.9</version>
</dependency>
Go to this link for the latest version of maven dependency. This solution should work for Spring Boot based projects.
Just add the jar file: byte-buddy-1.X.XX.jar
Source: https://github.com/raphw/byte-buddy
maven: https://mvnrepository.com/artifact/net.bytebuddy/byte-buddy/1.9.7
Only JAR binry Build: http://central.maven.org/maven2/net/bytebuddy/byte-buddy/1.9.7/byte-buddy-1.9.7.jar
I tried couple of solutions for this but it is quite simple. Simply move all Jar files from ModulePath to ClassPath and then Clean and Build the project. Your project will start working as expected.
I had the same error with this library when using it with maven. The dependency was transitively coming from hibernet-core. When checked with maven dependency:tree, the dependency was on test score for some reason. I had to include the dependency in my pom with provided scope and that fixed the issue.
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.6.14</version>
<scope>provided</scope>
</dependency>

Configure TomEE with Hibernate

I am trying to configure Tomee with Hibernate.
Changes that I made so far :
Copied following jars in tomcat/lib folder :
antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-entitymanager-4.0.1.Final.jar
hibernate-annotations-3.4.0.GA.jar
hibernate-commons-annotations.jar
hibernate-core-3.3.2.GA.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-validator-6.0.9.Final.jar
hibernate-validator-annotation-processor-6.0.9.Final.jar
hibernate-validator-cdi-6.0.9.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
Removed following jars from tomcat/lib :
bval-core-0.5.jar
bval-jsr303-0.5.jar
commons-lang-2.6.jar
openjpa-2.4.0.jar
serp-1.14.1.jar
And persistence.xml looks like this :
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="JPADB" >
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>*****</jta-data-source>
<properties>
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
</properties>
</persistence-unit>
</persistence>
And when I run tomcat, I am getting the following exception :
org.apache.openejb.OpenEJBRuntimeException: java.lang.ClassNotFoundException: org.apache.openjpa.persistence.PersistenceProviderImpl: java.lang.ClassNotFoundException: org.apache.openjpa.persistence.PersistenceProviderImpl
at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:843)
at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:677)
at org.apache.openejb.assembler.classic.Assembler.buildContainerSystem(Assembler.java:568)
at org.apache.openejb.assembler.classic.Assembler.build(Assembler.java:464)
at org.apache.openejb.OpenEJB$Instance.<init>(OpenEJB.java:151)
at org.apache.openejb.OpenEJB.init(OpenEJB.java:309)
at org.apache.tomee.catalina.TomcatLoader.initialize(TomcatLoader.java:256)
at org.apache.tomee.catalina.ServerListener.install(ServerListener.java:167)
at org.apache.tomee.catalina.ServerListener.lifecycleEvent(ServerListener.java:54)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:394)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:110)
at org.apache.catalina.startup.Catalina.load(Catalina.java:642)
at org.apache.catalina.startup.Catalina.load(Catalina.java:667)
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:498)
at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:253)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:427)
Caused by: org.apache.openejb.OpenEJBException: org.apache.openejb.OpenEJBRuntimeException: java.lang.ClassNotFoundException: org.apache.openjpa.persistence.PersistenceProviderImpl: java.lang.ClassNotFoundException: org.apache.openjpa.persistence.PersistenceProviderImpl
at org.apache.openejb.assembler.classic.Assembler.loadPersistenceUnits(Assembler.java:988)
at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:741)
... 20 more
What other changes should I do to make tomee work with hibernate ?

shared c3p0 connection pool with hibernate and mysql on tomcat

Iam working on a project where i want to configure a shared connection pool with hibernate on tomcat.
The project is already implemented and i have to change it.
It was configured with hibernate and c3p0 connection pool where all jars where in the project itself.
I copied all jar files for connection pooling into the lib folder of tomcat.
c3p0-0.9.5.2.jar
c3p0-oracle-thin-extras-0.9.5.2.jar
mchange-commons-java-0.2.11.jar
mysql-connector-java-5.1.40-bin.jar
in server.xml i made a Resource
<Resource auth="Container"
description="DB Connection"
driverClass="com.mysql.jdbc.Driver"
maxPoolSize="40"
minPoolSize="10"
acquireIncrement="1"
name="jdbc/test"
user="admin"
password="root"
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
jdbcUrl="jdbc:mysql://localhost:3306/testDB" />
<ResourceLink
global="jdbc/test"
name="jdbc/test"
type="javax.sql.DataSource" />
after this i can see my connections created by the pool in phpmyadmin(mysql).
In my web.xml file of my project i created
a resource-ref.`
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/testDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
`
And my hibernate.cfg.xml file
`
</hibernate-configuration>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.datasource">java:comp/env/jdbc/testDB</property>
<mapping resource="Database.hbm.xml"/>
</hibernate-configuration>
Thats what i have done till yet. My problem is. In my project i have a sessionfactory where i set my hibernate.cfg.xml and Database.hbm.xml file.
This is now giving me a java.lang.NullPointerException.
Do i have to configure the sessionfactory also on tomcat as resource?
At Tomcat:
Jun 29, 2017 10:06:19 AM org.apache.catalina.mbeans.GlobalResourcesLifecycleListener createMBeans
SCHWERWIEGEND: Exception processing Global JNDI Resources
javax.naming.NamingException: Could not load resource factory class [Root exception is java.lang.ClassNotFoundException: myutil.hibernate.HibernateSessionFactoryTomcatFactory]
at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:82)
at javax.naming.spi.NamingManager.getObjectInstance(Unknown Source)
at org.apache.naming.NamingContext.lookup(NamingContext.java:842)
at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
at org.apache.naming.NamingContextBindingsEnumeration.nextElementInternal(NamingContextBindingsEnumeration.java:117)
at org.apache.naming.NamingContextBindingsEnumeration.next(NamingContextBindingsEnumeration.java:71)
at org.apache.naming.NamingContextBindingsEnumeration.next(NamingContextBindingsEnumeration.java:34)
at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:138)
at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:145)
at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:110)
at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.lifecycleEvent(GlobalResourcesLifecycleListener.java:82)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:402)
at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:347)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:732)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.startup.Catalina.start(Catalina.java:689)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:321)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:455)
Caused by: java.lang.ClassNotFoundException: myutil.hibernate.HibernateSessionFactoryTomcatFactory
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:80)
... 23 more
Jun 29, 2017 10:06:17 AM org.apache.tomcat.util.digester.Digester endElement
WARNUNG: No rules found matching 'Server/GlobalNamingResources/ResourceLink'.
Jun 29, 2017 10:06:17 AM org.apache.catalina.core.AprLifecycleListener init
INFORMATION: The APR based Apache Tomcat Native library which allows optimal
performance in production environments was not found on the java.library.path:
EDIT
What i have not mentioned, iam using OSGI equinox and a servlet bridge.
everything that i have configured is right, the only thing i had to change in my case was to change my launch.ini
i had:
osgi.parentClassloader=app
osgi.contextClassLoaderParent=app
changing it to
osgi.parentClassloader=app
osgi.contextClassLoaderParent=fwk <--- changed
here a link where i got my help from
also using
java:/comp/env/jdbc/testDB using a slash before comp solved my problem
in your server.xml Change to this :
<Resource auth="Container"
description="DB Connection"
driverClass="com.mysql.jdbc.Driver"
maxPoolSize="40"
minPoolSize="10"
acquireIncrement="1"
name="jdbc/testDB" <!-- change -->
user="admin"
password="root"
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
jdbcUrl="jdbc:mysql://localhost:3306/testDB" />
and :
<ResourceLink
global="jdbc/testDB" <!--change -->
name="jdbc/testDB" <!-- & change -->
type="javax.sql.DataSource" />
And your hibernate.cfg.xml file to
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.datasource">java:comp/env/jdbc/testDB</property> <!-- ? -->
<mapping resource="Database.hbm.xml"/>
</session-factory>
</hibernate-configuration>
restart catalina.
osgi.parentClassloader=app
osgi.contextClassLoaderParent=app
changing it to
osgi.parentClassloader=app
osgi.contextClassLoaderParent=fwk <--- changed
here a link where i got my help from when i had NoInitialContextException.
javax.naming.NoInitialContextException:Cannot instantiate class: org.apache.naming.java.javaURLContextFactory (root cause classnotfound for org.apache.naming.java.javaURLContextFactory)
also using java:/comp/env/jdbc/testDB instead of java:comp/env/jdbc/testDB (slash before /comp) solved a different problem i had on the linux tomcat server(JNDI lookup exception):
NameNotFoundException.

How to fix mapping not found exception for Hibernate 5?

Hello I am new in hibernate and at the moment I'm trying to do something in practice. But have some errors.
I try to use hibernate in my project. I read the tutorial and start to integrate Hibernate into my java maven project.
So first of all I added the next dependencies:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.0.6.Final</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
The next step was to add hibernate.cfg.xml file into the src/main/resources directory. This file looks like:
<?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.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/testprojectdatabase</property>
<property name="hibernate.connection.username">username_here</property>
<property name="hibernate.connection.password">password_here</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<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>
<mapping resource="milkiv/mytestproject/models/UserInfo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Then I create UserInfo.java for mapping user_info table and put it inside src/main/java/milkiv/mytestproject/models/UserInfo.java, and UserInfo.hbm.xml file which I put inside src/main/resources/milkiv/mytestproject/models/UserInfo.hbm.xml.
I have also created the class UserInfoManager which look like:
public class UserInfoManager implements ManagerAdd<UserInfo> {
private final SessionFactory factory;
public UserInfoManager() {
factory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
}
public int add(UserInfo user) {
Transaction transaction = null;
Integer userId = null;
try (Session session = factory.openSession()){
transaction = session.beginTransaction();
userId = (Integer) session.save(user);
transaction.commit();
} catch (HibernateException he) {
if (transaction != null) {
transaction.rollback();
}
}
return userId;
}
}
When I try to create test for method add(UserInfo user) it failed on UserInfoManager constructor on factory initialization because of
Mapping (RESOURCE) not found : milkiv/mytestproject/models/UserInfo.hbm.xml : origin(milkiv/mytestproject/models/UserInfo.hbm.xml)
So eventually I know where, but do not know how to fix this. I have read a lot of answers for this questions on stackoverflow but no one didn't help me to fix this error.
I will appreciate any ideas, help and explanation how to fix this problem...
full stack trace:
Mapping (RESOURCE) not found : milkiv/mytestproject/models/UserInfo.hbm.xml : origin(milkiv/mytestproject/models/UserInfo.hbm.xml)
org.hibernate.boot.MappingNotFoundException
at org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:56)
at org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:274)
at org.hibernate.boot.cfgxml.spi.MappingReference.apply(MappingReference.java:70)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:413)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at milkiv.mytestproject.managers.UserInfoManager.(UserInfoManager.java:22)
at milkiv.mytestproject.managers.UserInfoManagerTest.testAdd(UserInfoManagerTest.java:24)
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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
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 org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
UPDATE
If anyone need, solving is in the comments for #v.ladynev answer.
I check your approach and everything works fine.
When you put UserInfo.hbm.xml in the src/main/resources/milkiv/mytestproject/models/UserInfo.hbm.xml
it will appear in the
bin/milkiv/mytestproject/models/UserInfo.hbm.xml
after build (instead of bin can be your build folder).
So /milkiv/mytestproject/models/ looks like as other source packages in the build folder. Please, check it. Check that you have resource folder in the classpath.
Try to add / in the beginning
<mapping resource="/milkiv/mytestproject/models/UserInfo.hbm.xml"/>
Update
Adding / in the beginning doesn't matter, because of Hibernate remove it before loading by ClassLoader. So the most valid way — without / in the beginning.
Try to put UserInfo.hbm.xml in the root of resources
<mapping resource="UserInfo.hbm.xml"/>
Update
Try to test
public UserInfoManager() {
System.out.println(UserInfoManager.class
.getResource("/milkiv/mytestproject/models/UserInfo.hbm.xml"));
System.out.println(ClassLoader.getSystemClassLoader().getResource(
"milkiv/mytestproject/models/UserInfo.hbm.xml"));
}
Note leading / in the first case. What console output after
new UserInfoManager();
#NickolasUA you are using Hibernate 5.
So change the last line in hibernate.cfg.xml
<mapping class="milkiv.mytestproject.models.UserInfo.hbm.xml"/>

Hibernate gives error error as "Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set" when there is no internet connection

Hibernate works fine when there is internet connection. But when there is no internet connection, it gives me
Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
error. I think my application is trying to access the internet connectivity when running. The detail error that I get is given below:-
Dec 22, 2015 6:17:43 PM
org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator
initiateService
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
Initial SessionFactory creation failed.org.hibernate.HibernateException: Access to
DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104)
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:209)
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 com.pos.admin.admin_module.admin_module.hibernatefiles.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:32)
at com.pos.admin.admin_module.admin_module.hibernatefiles.hibernate.HibernateUtil.getSessionFactory(HibernateUtil.java:44)
at com.pos.admin.admin_module.main.AdminModule.start(AdminModule.java:21)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(Unknown
Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Unknown
Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(Unknown
Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(Unknown
Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown
Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown
Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown
Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(Unknown
Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ExceptionInInitializerError
at com.pos.admin.admin_module.admin_module.hibernatefiles.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:39)
at com.pos.admin.admin_module.admin_module.hibernatefiles.hibernate.HibernateUtil.getSessionFactory(HibernateUtil.java:44)
at com.pos.admin.admin_module.main.AdminModule.start(AdminModule.java:21)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(Unknown
Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Unknown
Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(Unknown
Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(Unknown
Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
... 1 more
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104)
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:209)
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 com.pos.admin.admin_module.admin_module.hibernatefiles.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:32)
... 11 more
Exception running application com.pos.admin.admin_module.main.AdminModule
My pom.xml file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.pos.admin.admin_module</groupId>
<artifactId>admin_module</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>admin_module</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- <build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build> -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.5.Final</version>
</dependency> -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.5.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<!-- <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId>
<version>4.3.5.Final</version> </dependency> -->
<!-- <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId>
<version>5.2.2.Final</version> </dependency> -->
<!-- <dependency> <groupId>javax.el</groupId> <artifactId>javax.el-api</artifactId>
<version>2.2.4</version> </dependency> <dependency> <groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId> <version>2.2.4</version> </dependency> -->
</dependencies>
</project>
My hibernate configuration (hibernate.cfg.xml) file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/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/point_of_sales</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">
</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
<!--
<property name="hbm2ddl.auto">update</property> -->
<mapping class="com.pos.admin.admin_module.hibernatefiles.dto.Category"/>
<mapping class="com.pos.admin.admin_module.hibernatefiles.dto.ProductHeader"/>
<mapping class="com.pos.admin.admin_module.hibernatefiles.dto.PurchaseDetail"/>
<mapping class="com.pos.admin.admin_module.hibernatefiles.dto.PurchaseHeader"/>
<mapping class="com.pos.admin.admin_module.hibernatefiles.dto.SalesDetail"/>
<mapping class="com.pos.admin.admin_module.hibernatefiles.dto.SalesHeader"/>
<mapping class="com.pos.admin.admin_module.hibernatefiles.dto.Settings"/>
<mapping class="com.pos.admin.admin_module.hibernatefiles.dto.Unit"/>
<mapping class="com.pos.admin.admin_module.hibernatefiles.dto.User"/>
<mapping class="com.pos.admin.admin_module.hibernatefiles.dto.Supplier"/>
<!-- <mapping class="com.pos.storekeeper.storekeeper_module.hibernateFiles.dto.Supplier"/>
<mapping class="com.pos.storekeeper.storekeeper_module.hibernateFiles.dto.PurchaseHeader"/>
<mapping class="com.pos.storekeeper.storekeeper_module.hibernateFiles.dto.ProductHeader"/>
<mapping class="com.pos.storekeeper.storekeeper_module.hibernateFiles.dto.Category"/>
<mapping class="com.pos.storekeeper.storekeeper_module.hibernateFiles.dto.Unit"/> -->
<!-- <mapping class="com.pos.storekeeper.storekeeper_module.hibernateFiles.dto.User"/> -->
<!-- <mapping class="com.pos.admin.admin_module.admin_module.main.Cart1"/> -->
</session-factory>
</hibernate-configuration>
HibernateUtil class where I create Configuration and Session objects
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate-annotation.cfg.xml
Configuration configuration = new Configuration();
try{
configuration.configure("hibernate.cfg.xml");
}catch(Exception e){
System.out.println("Exception while handling configureation file :" + e.getMessage());
System.out.println("asfsdaf " + configuration);
}
System.out.println("Hibernate Annotation Configuration loaded");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
System.out.println("Hibernate Annotation serviceRegistry created");
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
if(sessionFactory == null) sessionFactory = buildSessionFactory();
return sessionFactory;
}
public static void closeSessionFactory(){
if(!sessionFactory.isClosed()){
System.out.println("Closing SessionFactory");
sessionFactory.close();
}
}
}
I want to fix this issue because internet connectivity cannot be always available. Thank you
to use hibernate offline you will have to change your dtd in your xml mapping and configuration files.make sure hibernate jar is in your classpath
for xml mapping file
<!DOCTYPE hibernate-mapping SYSTEM
"classpath://org/hibernate/hibernate-mapping-3.0.dtd">
for configuration file :
<!DOCTYPE hibernate-configuration SYSTEM
"classpath://org/hibernate/hibernate-configuration-3.0.dtd">
http://www.journaldev.com/2959/how-to-configure-hibernate-cfg-xml-to-work-offline
Try to remove SessionFactory from this:
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
To be:
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Or add this buildSessionFactory() like this:
private static SessionFactory sessionFactory = buildSessionFactory();
Your version of Hibernate has the ability to auto-configuring the Dialect, once its been able to establish a JDBC connection. If you can't establish a JDBC connection, that might be the problem.

Categories

Resources