Connection with MySQL database. Xml error Intellij - java

I'm trying to connect with my local MySQL database. When im trying to connect via session factory it throws following errror:
INFO: HHH000206: hibernate.properties not found
java.lang.ExceptionInInitializerError
at Main.<clinit>(Main.java:22)
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 Main.<clinit>(Main.java:18)
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)
... 6 more
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
at javax.xml.bind.ContextFinder.safeLoadClass(ContextFinder.java:594)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:239)
... 10 more
Exception in thread "main"
Process finished with exit code 1
My main class is generated with persistence mapping
hibernate.cfg.xml 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>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<mapping class="entities.AddressEntity"/>
<mapping class="entities.AnimalEntity"/>
<mapping class="entities.OwnerEntity"/>
</session-factory>
</hibernate-configuration>
I generated classes and .xml file via persistence mapping option in intelliJ. Thanks in advance for a quick response.

Related

Issue connecting to mysql database using hibernate.xml

I am unable to connect to a mysql database using xml. I have no problem using an hibernate.properties file, but when i substitute it with an xml it doesn't work. The xml in main/resources is
<?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/ifinances</property>
<property name="connection.username">infinite</property>
<property name="connection.password">skills</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<mapping class="com.infiniteskills.data.entities.User" />
</session-factory>
</hibernate-configuration>
I have a utility SessionFactory class that returns this
private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration();
return configuration
.buildSessionFactory(new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build());
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Error in building the factory");
}
}
After i retrieve it and i call the method .openSession() i have the following error
WARN - HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
WARN - HHH000342: Could not obtain connection to query metadata : The application must supply JDBC connections
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:271)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
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:691)
at com.infiniteskills.data.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
at com.infiniteskills.data.HibernateUtil.<clinit>(HibernateUtil.java:11)
at com.infiniteskills.data.Application.main(Application.java:18)
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:100)
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259)
... 15 more
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.infiniteskills.data.Application.main(Application.java:18)
Caused by: java.lang.RuntimeException: Error in building the factory
at com.infiniteskills.data.HibernateUtil.buildSessionFactory(HibernateUtil.java:22)
at com.infiniteskills.data.HibernateUtil.<clinit>(HibernateUtil.java:11)
... 1 more
Add in your library path MySQL Connector JDBC driver for MySQL (JAR)
Link [ https://dev.mysql.com/downloads/connector/j ]
You are not .configuring() the Configuration object with the hibernate.cfg.xml
so it's not picking up those properties.
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml"); //Resource path or File
Your hibernate config file is using wrong parameter names. Correct param names are as below:
hibernate.connection.driver_class
hibernate.connection.url
hibernate.connection.username
hibernate.connection.password
hibernate.dialect
hibernate.show_sql
Correct your param names and try again.
Refer this link for detailed list of param names: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html

org.hibernate.exception.GenericJDBCException: Cannot open connection - Oracle / Tomcat / localhost

I am writing a simple Hibernate program with Oracle as a database.
I am facing this error and I am not able to get the solution despite many trials.
I have added all hibernate jars & Oracle(ojdbc14.jar)
I have a schema as "test" in Oracle database
The username & password for Oracle is system
I have created the following files:
Student.java - POJO class with fields as rollno, name, address & setters/getters
Student.hbm.xml - Mapping file
Student.cfg.xml - Hibernate Configuration file
Test.java - Driver class
Student.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">oracle.jdbc.driver.OracleDriver</property>
<property name = "connection.url">jdbc:oracle:thin:#localhost:1521:xe</property>
<property name = "username">system</property>
<property name = "password">system</property>
<property name = "show_sql">true</property>
<property name = "dialect">org.hibernate.dialect.OracleDialect</property>
<property name = "hbm2ddl.auto">update</property>
<mapping resource = "com/alighthub/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Below is the error:
Exception in thread "main" org.hibernate.exception.GenericJDBCException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:420)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
at com.alighthub.Test.main(Test.java:16)
Caused by: java.sql.SQLException: invalid arguments in call
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:236)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:414)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
... 5 more
Please suggest.
Property for username & password is incorrect in Student.cfg.xml, updated as "connection.username" & "connection.password" as below :-
<property name = "connection.username">system</property>
<property name = "connection.password">system</property>

Hibernate database driver class not found

I am not very experienced with Hibernate yet and hava a problem when trying to create test data for my local database with Hibernate 4.3 and PostgreSQL.
I had another project where I did this exactly the same way and there it worked, so I did exactly the same setup but with another database, but now in my current project I get the following exception:
exception.DBException: Could not configure Hibernate!
at dao.BenutzerDAO.<init>(BenutzerDAO.java:48)
at export.ExportDBSchema.main(ExportDBSchema.java:16)
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.postgresql.Driver]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:245)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.loadDriverIfPossible(DriverManagerConnectionProviderImpl.java:200)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildCreator(DriverManagerConnectionProviderImpl.java:156)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:95)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
at dao.BenutzerDAO.<init>(BenutzerDAO.java:45)
... 1 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.postgresql.Driver
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:230)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:340)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:242)
... 15 more
I searched for possible solutions, but none of them worked for me:
-)Specify Classpath to jar in Manifest.mf -> Did not work
-)Place the postgresql-9.4.1208.jre6.jar in lib folder under WEB-INF -> Did not work
-)Specify hibernate.cfg.xml file in Configuration().configure(); -> Did not work
I use Glassfish 4.1 and the org.postgres.Driver.class is existing, so why is it not found?
My hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/Testdb</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<property name="hibernate.connection.password">password</property>
<mapping class="entity.Benutzer"/>
</session-factory>
</hibernate-configuration>
Method in BenutzerDAO.java where the exception occurs:
try {
if (sessionFactory == null) {
Configuration conf = new Configuration().configure();
StandardServiceRegistryBuilder builder
= new StandardServiceRegistryBuilder();
builder.applySettings(conf.getProperties());
sessionFactory = conf.buildSessionFactory(builder.build());
}
} catch (Throwable ex) {
throw new DBException("Could not configure Hibernate!", ex);
}
I would be very thankful for every answer.
try
{
Configuration cfg =new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sf=cfg.buildSessionFactory();
Session sess=sf.openSession();
----------------------------------
your code
------------------------------
Transaction tx=sess.beginTransaction();
sess.save(e);
tx.commit();
sess.flush();
sess.close();
sf.close();
}
catch(Exception e)
{
System.out.println("e="+e.getMessage());
}

hibernate 5 configuration for oracle11g in eclipse

I am trying to configure hibernate 5 to access oracle 11 g in eclipse.
hibernate.cfg.xml is as follows:
In following xml i have specified database credentials(url, username and password),dilect, mapping class, show_sql . I want to ask if I missed any important property.
<?xml version='1.0' encoding='utf-8'?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<!-- <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:#127.0.0.1:1521:orcl</property> -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:#127.0.0.1:1521:XE</property>
<property name="connection.username">SYSTEM</property>
<property name="connection.password">motog</property>
<mapping class="com.demo.one.Hello"/>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.OracleDialect
</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<property name="cache.use_query_cache">true</property>
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_structured_entries">true</property>
<property name="cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>
<property name="net.sf.ehcache.configurationResourceName">/hibernate-config/ehcache.xml</property>
<!-- Echo all executed SQL to stdout -->
<property name="hibernate.show_sql">true</property>
<mapping resource="hibernate-config/domain/Event.hbm.xml"/>
<mapping resource="hibernate-config/domain/Person.hbm.xml"/>
<mapping resource="hibernate-config/domain/PhoneNumber.hbm.xml"/>
<mapping resource="hibernate-config/domain/Account.hbm.xml"/>
<mapping resource="hibernate-config/domain/HolidayCalendar.hbm.xml"/>
<mapping resource="hibernate-config/domain/Item.hbm.xml"/>
</session-factory>
</hibernate-configuration>
[image specifies project structure and imported libraries][1]
Defined Hello class as Table name by using #Entity and userId as primary key by using #Id
Hello.java:
package com.demo.one;
import javax.persistence.Entity;
import javax.persistence.Id;
#Entity
public class Hello {
#Id
private int userId;
private String userName;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
} // end of Hello.java
Following class tries to save instance of Hello in database.
OracleTest.java:
package com.demo.one;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class OracleTest {
public static void main(String[] args) {
Hello z = new Hello();
z.setUserId(2);
z.setUserName("ashish");
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(z);
session.getTransaction().commit();
}
} // End of OracleTest.java
I am trying to save Hello class's object in oracle 11g XE database(resides on my local machine) , OracleTest.java is written for that.
I am getting following errors.
Jan 17, 2016 6:08:01 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.7.Final}
Jan 17, 2016 6:08:01 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jan 17, 2016 6:08:01 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number -1 and column -1 in RESOURCE hibernate.cfg.xml. Message: cvc-complex-type.2.4.a: Invalid content was found starting with element 'property'. One of '{"http://www.hibernate.org/xsd/orm/cfg":mapping, "http://www.hibernate.org/xsd/orm/cfg":class-cache, "http://www.hibernate.org/xsd/orm/cfg":collection-cache, "http://www.hibernate.org/xsd/orm/cfg":event, "http://www.hibernate.org/xsd/orm/cfg":listener}' is expected.
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:259)
at org.hibernate.cfg.Configuration.configure(Configuration.java:245)
at com.demo.one.OracleTest.main(OracleTest.java:13)
Caused by: javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; cvc-complex-type.2.4.a: Invalid content was found starting with element 'property'. One of '{"http://www.hibernate.org/xsd/orm/cfg":mapping, "http://www.hibernate.org/xsd/orm/cfg":class-cache, "http://www.hibernate.org/xsd/orm/cfg":collection-cache, "http://www.hibernate.org/xsd/orm/cfg":event, "http://www.hibernate.org/xsd/orm/cfg":listener}' is expected.]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:126)
... 6 more
Caused by: org.xml.sax.SAXParseException; cvc-complex-type.2.4.a: Invalid content was found starting with element 'property'. One of '{"http://www.hibernate.org/xsd/orm/cfg":mapping, "http://www.hibernate.org/xsd/orm/cfg":class-cache, "http://www.hibernate.org/xsd/orm/cfg":collection-cache, "http://www.hibernate.org/xsd/orm/cfg":event, "http://www.hibernate.org/xsd/orm/cfg":listener}' is expected.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.handleStartElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.bridge(Unknown Source)
... 9 more
please guide me, its my first encounter with hibernate.
PS: please check attached image. link is at the bottom.
regards
ashish
[1]: http://i.stack.imgur.com/DBYxu.png
The error message tells you that it's not expecting a <property> element somewhere. Instead, it expects a <mapping> element, or a <class-cache> element, or...
And indeed, the DTD has the following definition:
<!ELEMENT session-factory (property*, mapping*, (class-cache|collection-cache)*, event*, listener*)>
This means that, under <session-factory>, you're supposed to have 0 or more <property> elements, then 0 or more <mapping> elements.
But your xml file has
<property name="connection.password">motog</property>
<mapping class="com.demo.one.Hello"/>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
So you have a <mapping>element in the middle of <property> elements. That is thus invalid.

Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml

I am getting the following exception:
Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
at com.ClientForSave.main(ClientForSave.java:26)
Caused by: org.dom4j.DocumentException: Connection timed out: connect Nested exception: Connection timed out: connect
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
... 2 more
Entire stack trace
Here is my config file:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#localhost:1521:devt2x</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.password">tiger</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
I was to able to solve my problem.The Problem was the dtd and the required jars did not match.
I went to the url in dtd and downloaded the jars and its working now
dtd in my hibernate.cfg.xml file
I resolved this problem by deleting the spaces infront of dtd stating.There should not be a space infront of dtd.
The nested exception says that the parser has attempted to make a network connection to somewhere, and that failed. What it was trying to connect to, and why, and why the connect failed cannot be established definitively. However, I'm guessing that:
it was attempting to fetch a DTD or XML schema, and
it failed because:
you used the wrong URL (in your config file)
the server hosting the DTD or schema was down, or
some firewall is blocking out-going connections.
(1)Some times your Antivirus block to connect to URL to download DTD. So first disable your Antivirus and then run app .
(2)It may be dialect error, you have to use dialect for same database .
(3)and also check for dtd

Categories