configure hibernate.cfg.xml for MySQL on Ubuntu 12.04 - mysql

I tried to rebuild project settings from SQL Server to Apache 2 with MySQL.
I newly at JDBC and can't figure out all aspects.
I installed according this tutorial MySQL/JDBC Driver Setting Up MySQL/JDBC Driver on Ubuntu.
And test runs perfect after added this .jar file to project.
Content of hibernate.cfg.xml:
<?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>
<!-- /MYPC:1433/Blog;instance=SQLEXPRESS local connection properties -->
<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<!--<property name="hibernate.connection.url">jdbc:jtds:sqlserver://streamer2005.softserveinc.com/_055_OMS;instance=tc;</property>-->
<property name="hibernate.connection.url">jdbc:jtds:mysql://nazar_art/_056_DB;instance=MYSQL;</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">nazarsql</property>
<!--<property name="hibernate.hbm2ddl.auto">create</property>-->
<!-- <property name="show_sql">true</property> <property name="format_sql">true</property> -->
<mapping class="com.softserveinc.edu.oms.domain.entities.Role" />
<mapping class="com.softserveinc.edu.oms.domain.entities.Region" />
<mapping class="com.softserveinc.edu.oms.domain.entities.CustomerType" />
<mapping class="com.softserveinc.edu.oms.domain.entities.User" />
<mapping class="com.softserveinc.edu.oms.domain.entities.OrderStatus" />
<mapping class="com.softserveinc.edu.oms.domain.entities.Order" />
<mapping class="com.softserveinc.edu.oms.domain.entities.Product" />
<mapping class="com.softserveinc.edu.oms.domain.entities.Dimension" />
<mapping class="com.softserveinc.edu.oms.domain.entities.OrderItem" />
<mapping class="com.softserveinc.edu.oms.domain.entities.CreditCardType" />
</session-factory>
</hibernate-configuration>
But this doesn't work correctly when I run from maven it failed.
How to solve this issue?

Are you using Sql server or MySql?.If its MySql then change the dialect type to MySQLDialect
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

Related

Hibernate can not login to MySQL - Password verification failed

I have Hibernate 5.4.17 Final and MySQL 8.0 and 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.MySQL8Dialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/pia?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">********</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping class="rs.ac.bg.etf.dao.beans.Korisnik"/>
<mapping class="rs.ac.bg.etf.dao.beans.Kategorija"/>
<mapping class="rs.ac.bg.etf.dao.beans.Obavestenje"/>
<mapping class="rs.ac.bg.etf.dao.beans.Predmet"/>
<mapping class="rs.ac.bg.etf.dao.beans.Materijal"/>
<mapping class="rs.ac.bg.etf.dao.beans.Publikacija"/>
<mapping class="rs.ac.bg.etf.dao.beans.PlanAngazovanja"/>
</session-factory>
</hibernate-configuration>
When I test the password in MySQL Workbench connection is successful.
I don't know why I get the error java.io.IOException: Keystore was tampered with, or password was incorrect

Hibernate changes database change from oracle to mysql

I am using Spring and Hibernate in my project. My database has changed from Oracle 10g to MySQL.
Can you please tell me what changes do I need to make in my Hibernate configuration?
Also are there any changes required in my Java code.
If you are using the hibernate.cfg.xml for your project to define the database property, then you need to change it to the below values for MySQL :-
<?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.connection.url">jdbc:mysql://localhost:3306/{db.name}</property>
<property name="hibernate.connection.username">{db.name}</property>
<property name="hibernate.connection.password">{db.password}</property>
</session-factory>
</hibernate-configuration>
Also you need to include the mysql-connector jar in your pom.xml for example
<!--Mysql-Connector-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
if you are not using the hibernate.cfg.xml and instead using some sessionFactory bean in your Spring ApplicationContext.xml. Then you need to change it to below :-
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="annotatedClasses">
</property>
<property name="hibernateProperties">
<props>
<!-- As of now not using hibernate.cfg.xml -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.validator.apply_to_ddl">false</prop>
<prop key="hibernate.validator.autoregister_listeners">false</prop>
<prop key="show_sql">true</prop>
<prop key="format_sql">true</prop>
</props>
</property>
</bean>
<bean id="dataSource"
class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.pass}" />
</bean>
Please follow http://www.baeldung.com/hibernate-4-spring for complete example on how to setup hibernate with spring.

How can I reference the mysql connector in nhibernate without installing the mysql connector msi?

I need some help with the configuration file. I have included MySql.Data.dll in my project, but how can I tell the configuration file that this dll contains the MySQL connector?
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="NHibernate.Test">
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="connection.driver_class">
MySql.Data <!--this attempt didn't work!! -->
</property>
<!--<property name="hbm2ddl.auto">update</property> -->
<property name="connection.connection_string">
Server=f.xx.ca;Database=xx;Uid=root;Pwd=xx;
</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
<!-- <property name="use_outer_join">true</property> -->
<property name="command_timeout">60</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache</property>
<property name="cache.use_second_level_cache">false</property>
I'm getting fed up with installing the connector with the msi every time I try to debug the project on a new computer.
Thanks!
If the MySql.Data.dll is a reference inside your Visual Studio Solution and is set to Copy local = true then you don't need to install the msi, e.g.
The connection drive class should then:-
<property name="connection.driver_class">
NHibernate.Driver.MySqlDataDriver
</property>

Hibernate: MySql vs ' ' user

I've started learning hibernate. Here is my hibernate-configuration file:
<hibernate-configuration>
<session-factory>
<!-- connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernatelearning</property>
<property name="connection.name">root</property>
<property name="connection.password"></property>
<!-- JDBC connection pool -->
<property name="connection.pool_size">1</property>
<!-- dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property>
<mapping resource="./Event.hbm.xml"/>
</session-factory>
</hibernate-configuration>
The exception is:
INFO: connection properties: {name=root, password=****}
22.03.2011 15:45:14 org.hibernate.cfg.SettingsFactory buildSettings
WARNING: Could not obtain connection to query metadata
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Access denied for user ''#'localhost' to database 'hibernatelearning'
Does anybody know how to deal with it?
The problem was in hibernate.cfg.xml. The right one:
<session-factory>
<!-- connection settings -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatelearning</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<!-- JDBC connection pool -->
<property name="connection.pool_size">1</property>
<!-- dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property>
<mapping resource="./Event.hbm.xml"/>
</session-factory>

Problem with UTF-8 in Create Schema By Hibernate

this is My hibernate.hbm.xml
and I use MySQL
<?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="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/dbName?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">******</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.connection.characterEncoding">UTF-8</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<property name="current_session_context_class">thread</property>
<!-- configuration pool via c3p0-->
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.idle_test_period">100</property> <!-- seconds -->
<property name="c3p0.max_size">100</property>
<property name="c3p0.max_statements">0</property>
<property name="c3p0.min_size">10</property>
<property name="c3p0.timeout">100</property> <!-- seconds -->
<!-- DEPRECATED very expensive property name="c3p0.validate>-->
</session-factory>
</hibernate-configuration>
when I run my program for first time it creates Table in database but my problem is the Charset still is latin1_swedish_ci (latin) and don't be utf8 what should I change in hibernate.hbm.xml settings?
I resolved this problem by setting character-set-server option in my.cnf:
[mysqld]
character-set-server=utf8
The UTF-8 setting relates to the way that Hibernate configures its runtime connections to the database. I'm guessing it has no effect on the schema creation stuff, which is really a separate part of Hibernate altogether.
I find that the auto-DDL stuff is only really useful for generating an initial stab at the schema. I always take that DDL and modify it to be exactly what I want.