I have a simple maven project setup with a java class that creates a SessionFactory from a hibernate.cfg.xml - when I run this application from a main method it executes fine:
Test.java
public static void main(String[] args){
System.out.println("Starting App");
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
//do some stuff
//success
}
HibernateUtil.java
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
System.out.println("Opening Session Factory...");
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
sessionFactory = configuration.buildSessionFactory(builder.build());
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
However, when I run this same app on my Tomcat Application Server it calls my RestController class which creates the SessionFactory the same way:
Controller.java
#RestController
public class MyController {
MyClass class = null;
#RequestMapping(value = "/{searchQuery}", produces = "application/json")
public String search(#PathVariable String searchQuery) throws IOException {
System.out.println("Starting Session...");
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
//do some stuff
}
And the following exception is returned:
Starting Session...
Opening Session Factory... //Inside HibernateUtil.java
Mar 09, 2015 11:54:47 AM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.1
Mar 09, 2015 11:54:47 AM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Mar 09, 2015 11:54:47 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
Mar 09, 2015 11:54:47 AM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Mar 09, 2015 11:54:48 AM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Mar 09, 2015 11:54:48 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Initial SessionFactory creation failed.org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.test.dto.MyClassDTO"/>
Mar 09, 2015 11:54:48 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [MyClass] in context with path [/test] threw exception [Handler processing failed; nested exception is java.lang.ExceptionInInitializerError] with root cause
org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.test.dto.MyClassDTO"/>
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1597)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1552)
I believe this exception is misleading, as I have no need to instantiate an AnnotationConfiguration; Configuration works just fine. What could be the reason this exception is thrown while run on my Application Container?
EDIT: hibernate.cfg.xml
<!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.SQLServerDialect</property>
<!-- Database Connection String -->
<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost:55555/test</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">pass</property>
<!-- C3p0 Settings -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">100</property>
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.max_statements">10</property>
<property name="hibernate.c3p0.min_size">10</property>
<property name="hibernate.c3p0.timeout">100</property>
<!-- Names the annotated entity class -->
<mapping class="com.test.dto.MyClassDTO"/>
</session-factory>
</hibernate-configuration>
Related
I have a problem with my hibernate.cfg.xml in IntelliJ IDE.
Here is my hibernate config file:
<?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>
<!-- Database connection settings -->
<property name="connection.driver:class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost/HIndex</property>
<property name="hibernate.connection.username">index_user</property>
<property name="hibernate.connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL Dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL82Dialect</property>
<!-- Assume test is the database name -->
<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="HIndexSaar.HIndex.Person"/>
<mapping class="HIndexSaar.HIndex.University"/>
<mapping class="HIndexSaar.HIndex.Publication"/>
</session-factory>
</hibernate-configuration>
And this is my application class:
package HIndexSaar.HIndex;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateManager {
private static SessionFactory factory;
public HibernateManager(){
//
//* Setup the configuration.
//
Configuration config = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Person.class) .addAnnotatedClass(University.class).addAnnotatedClass(Publication.class);
factory = config.buildSessionFactory();
}
I get an error by running the following code:
package HIndexSaar.HIndex;
public class AppHibernate {
public static void main(String[] args){
HibernateManager mng = new HibernateManager();
[...]
}
}
So something seems to be broken in building the SessionFactory in the HibernateManager. I get the error
Could not locate cfg.xml resource
and the following stacktrace:
Feb 27, 2016 10:26:03 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.7.Final}
Feb 27, 2016 10:26:03 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Feb 27, 2016 10:26:03 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:53)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
at org.hibernate.cfg.Configuration.configure(Configuration.java:259)
at org.hibernate.cfg.Configuration.configure(Configuration.java:245)
at HIndexSaar.HIndex.HibernateManager.<init>(HibernateManager.java:18)
at HIndexSaar.HIndex.AppHibernate.main(AppHibernate.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
I already have placed the hibernate.cfg.xml file under /src/main/java/resources but obviously something goes still wrong.
Does somebody have an idea what could be the mistake I made? Surprisingly, the same code runs in Eclipse without error, so what could be the problem of IntelliJ? I use the same SDK/JDK, namely jdk 1.7.
If your are using intellij then try to create new module with hibernate framework support then paste this same code and execute it.
This might be path difference in clipse and intellij
I have hibernate project structure like this
My hibernate.cfg.xml is
<?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.url">jdbc:sqlserver://localhost:1433;databaseName=WSMS</property>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<mapping class="test.IssuesEntity"/>
<!-- DB schema will be updated if needed -->
<!-- <property name="hbm2ddl.auto">update</property> -->
</session-factory>
</hibernate-configuration>
My main class is
public class Main {
public static void main(String[] args) throws Exception {
/// System.out.print("jajaja");
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file
//creating seession factory object
SessionFactory factory=cfg.buildSessionFactory();
//creating session object
Session session=factory.openSession();
//creating transaction object
Transaction t=session.beginTransaction();
IssuesEntity e1=new IssuesEntity();
// e1.setId(115);
// e1.setFirstName("sonoo");
// e1.setLastName("jaiswal");
e1.setIssueName("akash");
session.persist(e1);//persisting the object
t.commit();//transaction is commited
session.close();
System.out.println("successfully saved");
}
}
But whenever i run project it shows following error
Feb 26, 2016 10:02:59 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Feb 26, 2016 10:03:00 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.2.Final}
Feb 26, 2016 10:03:00 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Feb 26, 2016 10:03:00 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
I have followed intellij instruction but i can not find any solution. Please help
It is not an error. It is just an information that you don't have hibernate.properties file in the class path. You have properties in the hibernate.cfg.xml, it is ok too. But you need to specify hibernate.dialect, hibernate.connection.username and hibernate.connection.password.
And if you don't have tables in the database you need this too
<property name="hbm2ddl.auto">update</property>
I am trying to create a simple hibernate project, but I am getting error while reading hibernate.cfg.xml file, dont know what the issue is, below is the code and error trace. Please help.
Here is my hibernate.cfg.xml file:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/EPS</property>
<property name="hibernate.connection.username">haris</property>
<property name="hibernate.connection.password">ihatecookies</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="hibernate.connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="hibernate.show_sql">true</property>
<!-- Drop or re-create database -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping class="com.secure.eps.model.User"/>
</session-factory>
</hibernate-configuration>
Here is my main class:
package com.secure.eps.controller;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import com.secure.eps.model.User;
public class HibernateUtil {
public static void main(String args[]){
User user = new User();
user.setUsername("RoojiPooji");
user.setPassword("ihatecookies");
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.configure().buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
session.close();
}
}
Here is error stack:
Sep 24, 2014 9:37:41 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Sep 24, 2014 9:37:41 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.6.Final}
Sep 24, 2014 9:37:41 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.service.allow_crawling=false, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.max_fetch_depth=5, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE, hibernate.bytecode.use_reflection_optimizer=false, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=5}
Sep 24, 2014 9:37:41 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Sep 24, 2014 9:37:41 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
Sep 24, 2014 9:37:41 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2163)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2075)
at com.secure.eps.controller.HibernateUtil.main(HibernateUtil.java:19)
Caused by: org.dom4j.DocumentException: null Nested exception: null
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2155)
... 2 more
I believe it is an issue with this line in your xml configuration.
<!-- Mapping files -->
<mapping class="com.secure.eps.model.User"/>
Refer to hibernate docs: http://www.tutorialspoint.com/hibernate/hibernate_mapping_files.htm.
I would like to know how to create a JNDI entry for a MySQL database using Eclipse Kepler and Tomcat 7 (Is there a plugin for Eclipse?).
When I created the Dynamic Web Project using Eclipse, I did NOT checked the "Generate web.xml deployment descriptor", so, I do not have a web.xml file in my project.
The JDNI resource definition is declared in Tomcat's <Context> element in context.xml 1, not your new application's web.xml. So whether it's generated or not, it doesn't actually matter.
<Context>
<Resource name="jdbc/YourDB" auth="Container"
type="javax.sql.DataSource"
username="username" password="password"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://dbserver:dbport/actualDbname"/>
</Context>
If you had web.xml in your application, you will need an entry like this to refer to the datasource mentioned above:
<resource-ref>
<res-ref-name>jdbc/YourDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Since you opted not to have web.xml, you can use annotations:
#Resource(lookup = "java:comp/env/jdbc/YourDB")
private DataSource dataSource;
1 There are multiple ways to define the Context, refer to Tomcat's definition on Defining a Context.
When I posted the question, I forgot to say that I'm using Hibernate and to include the files.
The hibernate.cfg.xml is:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE
hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//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.datasource">java:comp/env/jdbc/ExemplosWeb</property>
<property name="hibernate.connection.username">TheUsername</property>
<property name="hibernate.connection.password">ThePassword</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- One to One Unidirecional -->
<mapping class="relacionamentoHibernate.bean.PaiOneToOneUnidirecional"/>
<mapping class="relacionamentoHibernate.bean.FilhoOneToOneUnidirecional"/>
</session-factory>
There is an Hibernate util class:
package hibernateUtil;
import javax.annotation.Resource;
import org.hibernate.*;
import org.hibernate.cfg.*;
#Resource(lookup="java:comp/env/jdbc/exemplosweb")
#SuppressWarnings("deprecation")
public class HibernateUtil
{
private static final SessionFactory sessionFactory;
static
{
try
{
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch (HibernateException e)
{
// Make sure you log the exception, as it might be swallowed
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
catch (NoClassDefFoundError e)
{
e.printStackTrace();
throw new NoClassDefFoundError("static HibernateUtil");
}
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
}
and I got this exception:
Fev 26, 2014 7:02:46 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Fev 26, 2014 7:02:46 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.4.Final}
Fev 26, 2014 7:02:46 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Fev 26, 2014 7:02:46 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Fev 26, 2014 7:02:46 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Fev 26, 2014 7:02:46 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Fev 26, 2014 7:02:46 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!
Fev 26, 2014 7:02:46 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
org.hibernate.service.jndi.JndiException: Unable to lookup JNDI name [java:comp/env/jdbc/ExemplosWeb]
at org.hibernate.service.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:68)
at org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:116)
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)
we are integrating the java blazeds hibernate with flex project..First we tested the pure java hibernate in eclipse and it is executing fine. But when we put the same in tomcat for flex integration with blazeds it is showing the following error.this is the only error.
Jan 24, 2013 11:31:31 AM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.6
Jan 24, 2013 11:31:31 AM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Jan 24, 2013 11:31:31 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
Jan 24, 2013 11:31:31 AM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Jan 24, 2013 11:31:31 AM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Jan 24, 2013 11:31:31 AM org.hibernate.cfg.Configuration getConfigurationInputSt
ream
INFO: Configuration resource: /hibernate.cfg.xml
this is the hibernate.cfg.xml(To identify whether hibernate.cfg.xml is not found and i tested by deleting DOCTYPE of .cfg.xml then tomcat output displayed saying the root element is not found...which means it is able to find the hibernate.cfg.xml( i think)
<?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.url">jdbc:mysql://localhost/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<property name="current_session_context_class">thread</property>
<mapping class="com.model.User" />
<mapping class="com.model.UserDetails" />
<mapping class="com.model.LoanDetails" />
<mapping class="com.model.BorrowerDetails" />
</session-factory>
</hibernate-configuration>
this is hiberutil.java
public class HibernateUtil
{
private static SessionFactory sessionFactory=configureSessionFactory();
private static ServiceRegistry serviceRegistry;
private static SessionFactory configureSessionFactory() throws HibernateException {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
}
why the error is showing? the control is coming from flex side and executing the starting point of the java method...but when it comes to hibernate stuff execution, it is showing this error in tomcat...
But when i execute java+hibernate as a pure java application it is executing fine..
can anybody help me?
Thanks
EDIT 1 After the modification suggested by #Andremoniy and putting the .cfg.xml in src folder and here also in eclipse it is working but not in tomcat with flex
following error comes
org.hibernate.MappingException: An AnnotationConfiguration instance is required
to use <mapping class="com.model.User"/>
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.jav
a:1606)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.jav
a:1561)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1540)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1514)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1434)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1420)
at com.sample.HibernateUtil.configureSessionFactory(HibernateUtil
.java:26)
at com.sample.HibernateUtil.<clinit>(HibernateUtil.java:20)
at com.sample.App.checkUser(App.java:34)
I guess the problem is with the code for configuration you mentioned for .hbm.xml may not be working in Tomcat.
I think it needs AnnotationConfiguration object. I guess you used this code due to the Annotationconfiguration object creation is not working.
Better create a maven hibernate project with pom and export the war file to Tomcat (with the changes of Annotationconfiguration). Also use log4j jar for showing the details of Tomcat execution with the debug outputs, so that Tomcat output will print all of execution flow.
It seems to me, that you can solve this problem, by passing hibernate.cfg.xml directly into Configuration.
try (InputStream in = HibernateUtil.class.getResourceAsStream("/hibernate.cfg.xml")) {
Configuration configuration = new Configuration().addInputStream(in).configure();
...
} ...
The only thing, you have to properly point the path or class, for getResourceAsStream. Or you may be want to create InputStream using some relative path.
I have faced the same problem but I found the solution here..
http://www.mkyong.com/hibernate/hibernate-error-an-annotationconfiguration-instance-is-required-to-use/
ie. Change the line:
new Configuration().configure().buildSessionFactory();
to:
new AnnotationConfiguration().configure().buildSessionFactory();
if it is appeared that you use Intellij Idea you need to put hibernate.cfg.xml file into resources folder
and if you will have after next error:
org.hibernate.service.jndi.JndiException: Error parsing JNDI name []
delete in hibernate.cfg.xml file name of tag see picture: