I am newbie to hibernate.I have tried my first hibernate program without success.I have got following error while running my hibernate program.
Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: Hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1586)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1520)
at com.test.Test.main(Test.java:16)
Caused by: org.dom4j.DocumentException: Error on line 11 of document : The content of elements must consist of well-formed character data or markup. Nested exception: The content of elements must consist of well-formed character data or markup.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1576)
... 2 more
This is hibernate configuration file.
<?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>
<!-- connecting database -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.username">test</property>
<property name="hibernate.connection.password">test123</property>
<! -- autocommit false -->
<property name="hibernate.connection.autocommit">false</property>
<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 resource="mapping.hbm.xml">
</mapping>
</session-factory>
</hibernate-configuration>
Simple, your comment is not a comment:
<! -- autocommit false -->
Should be
<!-- autocommit false -->
You have an extra space...
You should be able to see that in source code highlighting - as demonstrated by the excellent SO source highlighting.
You have a comment bad delimited, search for this:
<! -- autocommit false -->
fix it by deleting the space beteen ! and --
Related
I'm starting learing how to use IntelliJ, and I'm trying to link my persistence to my datasource.
I've read here on SO how to do that, and now I've binded my persistence to my datasource successfully: I can run queries on database, and i can browse my persistence.
This is the db (MSSQL):
This is the persistence:
And this is the assignment:
The problem is that I'm still getting error on my class, looks like is not able to link to the datasource, and is giving me the error "Cannot resolve column" (not only on ID column, but on all columns):
The application is running fine, so the problem is only related to some wrong intellij configuration, but I can't find where the problem is.
This is my hibernate.cfg.xml (there are more tables, I've left only the one in the example):
<?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 name="Artemide">
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<!-- Configurazione maven-->
<property name="hibernate.connection.password">${hibernate.password}</property>
<property name="hibernate.connection.username">${hibernate.username}</property>
<property name="hibernate.connection.url">${hibernate.connection.url}</property>
<property name="hibernate.default_catalog">${hibernate.default_catalog}</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
<property name="hibernate.search.autoregister_listeners">false</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.format_sql">true</property>
<mapping class="xx.xx.xx.database.pojo.Categoria"/>
</session-factory>
</hibernate-configuration>
Thanks!
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>
I am stuck with hibernate configuration file exception, this is my hibernate.cfg.xml file:
<?xml version='1.0' encoding='utf-8'?>
<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">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
<property name="connection.username">postgresql</property>
<property name="connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgresPlusDialect</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.person.PersonModel"/>
</session-factory>
</hibernate-configuration>
I am using hibernate-core-4.1.12.Final.jar, I am getting the following exception:
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Exception in thread "main" org.hibernate.MappingException: invalid configuration
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2022)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1939)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1918)
at org.person.PersonPersistance.main(PersonPersistance.java:14)
Caused by: org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 25; Document is invalid: no grammar found.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
By seeing the respective exception I came to know that I have some mistake in my configuration file. I want to know what mistake I have done in my configuration file.
When I wrote same program in Eclipse Luno, it has got execute. Try this and see.
Add this before <hibernate-configuration> tag:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
With Hibernate 4.x you should use the same DTD as 3.x:
<?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>
I'm trying to set up Hibernate, and since 4.0.0 just came out I naturally decided to go with that. It seems that no matter what way I try to create a SessionFactory, it always leads to the same error:
Initial SessionFactory creation failed.java.util.ServiceConfigurationError: org.hibernate.integrator.spi.Integrator: Provider org.hibernate.envers.event.EnversIntegrator could not be instantiated: java.lang.ClassCastException: Cannot cast org.hibernate.envers.event.EnversIntegrator to org.hibernate.integrator.spi.Integrator
It seems like there is something wrong with my Hibernate configuration but I can't figure out what. Here's 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>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://192.168.0.17:5432/mydb</property>
<property name="connection.username">myusrname</property>
<property name="connection.password">mypasswd</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- c3p0 configuration -->
<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>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</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">false</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>
Does anyone spot anything out of the ordinary, or do you have other ideas?
I found no way to get Hibernate 4.0.0 CR4 working. Switching to 3.6.7 resolved the problem and works just fine. I conclude that there must be some bug in the release, and will file a bug report.
When I try to load an object( a row ) from mysql database, the string properties are not loaded properly , and as a result when I print them, nothing is displayed.
here is my hibernate config file :
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://hibernate.sourceforge.net/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/demo_hib_1
</property>
<property name="connection.username">root</property>
<property name="connection.password"> </property>
<property name="pool_size">5</property>
<property name="show_sql">true</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- Mapping files -->
<mapping resource="com/navid/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
I tryed adding encoding to connection url :
jdbc:mysql://localhost:3306/demo_hib_1&characterEncoding=UTF-8
and got hibernate exception :
Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2246)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
at com.navid.Main.main(Main.java:31)
Caused by: org.dom4j.DocumentException: Error on line 10 of document : The reference to entity "characterEncoding" must end with the ';' delimiter. Nested exception: The reference to entity "characterEncoding" must end with the ';' delimiter.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2238)
... 3 more
The hibernate config file is an XML file, so raw & symbols aren't allowed
Two options that spring to mind (but untested!), the first would be to use the XML & escape sequence:
<property name="connection.url">
jdbc:mysql://localhost:3306/demo_hib_1?useUnicode=true&characterEncoding=UTF-8
</property>
Or using name+value syntax, which wouldn't need the & escaping:
<property name="connection.url" value="jdbc:mysql://localhost:3306/demo_hib_1?useUnicode=true&characterEncoding=UTF-8" />
Note that I've added a 2nd option too, I think you need both
Did you try adding
<property name="connection.characterEncoding">UTF-8</property>
to session factory configuration.
From Hibernate documentation:
Arbitrary connection properties can be given by prepending "hibernate.connection" to the connection property name. For example, you can specify a charSet connection property using hibernate.connection.charSet.