I am new to Hibernate .... i tried this code but i got Could not parse configuration: NewFile.cfg.xml (name of .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>
<!-- connection to oracle -->
<propertyname="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver </property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#localhost:1521:XE
</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.connection.password">system</property>
<!-- autocommit -->
<property name="hibernate.connection.autocommit">false</property>
<!-- to display sql query generated while running program -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<!-- for jdbc transaction -->
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.
JDBCTransactionFactory</property>
<!-- mapping file -->
<mapping resource="Mapping.hbm.xml"/>
</session-factory>
The end tag for <hibernate-configuration> is missing:
</hibernate-configuration>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver </property>
Your opening tag not match the ending tag.
Related
Whenever I run my server my database table are re-created and all my previous data are being deleted. I was thinking this might be the problem in hibernate configuration file but don't know the actual reason hope you awesome guys will figure me out .
I also tried changing <property name="hibernate.hbm2ddl.auto">update</property> to auto , validate but didnot help
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/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.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/restro
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
root
</property>
<!-- <property name="connection.release_mode">auto</property> -->
<!-- Drop and re-create the database schema on startup -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Names the annotated entity class -->
<mapping class="com.restroo.model.MenuItems"/>
<mapping class="com.restroo.model.AdminUser"/>
</session-factory>
</hibernate-configuration>
SOLVED
This question is about java hibernate.
When i run the Main class i get this error
Initial SessionFactory creation failed: org.hibernate.MappingException:
invalid configuration
Exception in thread "main" java.lang.NullPointerException
at principal.ClienteDAO.guardaCliente(ClienteDAO.java:38)
at principal.Main.main(Main.java:31)
C:\Users\Nico\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)
Have no idea why im getting that exception
my hibernate.cfg.xml is
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--TODA LA INFORMACION FUE SACADA DE: http://www.javatutoriales.com/2009/05/hibernate-parte-1-persistiendo-objetos.html-->
<!-- parametros para la conexion a la base de datos -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/basededatosprueba</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- Configuracion del pool interno -->
<property name="connection.pool_size">1</property>
<!-- Dialecto de la base de datos -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Otras propiedades importantes -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create-drop</property>
<!-- Archivos de mapeo -->
<mapping resource="mapeos/Cliente.hbm.xml"/>
</session-factory>
</hibernate-configuration>
EDIT 2:
So, i dont know why, but i change the hibernate.cfg.xml and now it works, here it is
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/basededatosprueba</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="mapeos/Cliente.hbm.xml"/>
</session-factory>
</hibernate-configuration>
solved? we'll see
I think you might need to close your hibernate-configuration tag in your xml.
Here is my code in file named 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>
<!-- Database connection settings -->
<property name="connection.url">jdbc:mysql://localhost:3306/HeartJar</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>
<mapping resource="com/heartjar/bean/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
There was a waining saying Stream not availableat line 2
Although warnings don't have bad influences to my project.But Understanding how to solve the problem is useful for me,a beginner in JavaWeb.Thank you for your answer
I am a beginner and trying to run first simple code.
Please help me resolve the following issue.
Error on line 11 of document : The element type "session-factory" must be terminated by the matching end-tag "</session-factory>".
Nested exception: The element type "session-factory" must be terminated by the matching end-tag "</session-factory>".
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
config 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="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
<property name="connection.username">postgres</property>
<property name="connection.password"/>shaher</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="org.shaher.hiber.dto.UserDetails"/>
</session-factory>
</hibernate-configuration>
You need to close the tag <session-factory>
<session-factory>
...
</session-factory>
you have problem with your config file see this
<property name="connection.password"/>shaher</property>
please update it to below
<property name="connection.password">shaher</property>
This is my configuration:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-5.0.dtd">
<hibernate-configuration
xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>
<mapping class="Mapping.AccountMap"
</session-factory>
</hibernate-configuration>
I read that i should change the "Doctype" and i did from other answers i have found, yet this error occurs:
Unable to perform unmarshalling at line number 8 and column 63 in RESOURCE hibernate.cfg.xml. Message: cvc-elt.1: Cannot find the declaration of element 'hibernate-configuration'.
Your dtd url is false ... seems like hibernate-configuration-5.0.dtd doesn't exist ... use this :
http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd
change the whole header :
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-5.0.dtd">
<hibernate-configuration
xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
TO
<?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>
Try to add this header block. It works for me and also check parse error in your 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">
Here i am posting some sample xml file may be it will help you.
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- Names the annotated entity class -->
<mapping class="org.java.dto.UserDetails"/>
</session-factory>
</hibernate-configuration>