Problem retrieving utf-8 string from mysql using hibernate3 - java

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.

Related

java.lang.UnsupportedOperationException : The application must supply JDBC connections

<?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 ="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name ="conection.url">jdbc:mysql://localhost:3306/employeesdb</property>
<property name ="connection.username">root</property>
<property name ="connection.password">password#123</property>
<property name ="hbm2ddl.auto">update</property>
<property name ="show_sql">true</property>
<property name="current_session_context_class">thread</property>
<mapping class = "com.start.Student"></mapping>
</session-factory>
</hibernate-configuration>
Normally I should be able to connect to DB but I am getting an error.
Error: - "Exception in thread "main" java.lang.UnsupportedOperationException: The application must supply JDBC connections"
It means I am not able to connect with DB.
I have checked that the database username and password are correct.
ERROR
Main Program
You have typo in conection.url. Should be connection.url.

Bind datasource to persistence on Intellij fails on resolve column

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!

Could not parse configuration: hibernate.cfg.xml: The element type "session-factory" must be terminated by the matching end-tag "</session-factory>"

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>

org.hibernate.exception.JDBCConnectionException: could not fetch initial value for increment generator

In my application, I am using two hibernate.cfg.xml files to communicate the same mysql database. The hibernate cfg files are hibernate.cfg.xml and dynamic-hibernate.cfg.xml and I am deployed my application in JBoss server.
At some time I got the exception as "org.hibernate.exception.JDBCConnectionException: could not fetch initial value for increment generator" while inserting the data by using Dynamic-hibernate session. But i am not facing the problem in another Hibernate-cfg.xml session. I have also extended MYsql waittimeout. Still i'm facing the same problem
Please Help me to solve this issue.
My Dynamic-hibernate-cfg.xml :
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/mydatabase</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.default_entity_mode">dynamic-map</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping file="./WEB-INF/xml/DynamicMapping.hbm.xml"/>
</session-factory>
My Another Hibernate-cfg.xml
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/mydatabase</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.connection.isolation">2</property>
<mapping resource="HibernateMapping.hbm.xml" />
</session-factory>
My Exception is :
org.hibernate.exception.JDBCConnectionException: could not fetch initial value for increment generator
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:72)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.IncrementGenerator.getNext(IncrementGenerator.java:107)
at org.hibernate.id.IncrementGenerator.generate(IncrementGenerator.java:44)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:91)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:557)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:545)
Caused by: java.sql.SQLException: No operations allowed after connection closed.
Connection was closed due to the following exception:
** BEGIN NESTED EXCEPTION **
java.sql.SQLException
MESSAGE: Communication link failure: java.io.EOFException, underlying cause: null
** BEGIN NESTED EXCEPTION **
java.io.EOFException
STACKTRACE:
java.io.EOFException
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1394)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:1538)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1929)
If you read the exception care fully, it is saying connection is already closed. Check in your code where the connection is closed. And also check the file is reading properly or not.

XML parsing Error while running Hibernate

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 --

Categories

Resources