Hibernate: MySql vs ' ' user - mysql

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>

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

How to set MariaDb Driver properties using hibernate

I am using aurora mysql db. I am using mariadb connector j with hibernate.
I am using "org.mariadb.jdbc.Driver".I want to set following properties of this driver using hibernate cfg file.
set read only on connetion so that it will fetch data from read replica.
pooling mechanism of mariadb driver(minPoolSize , maxPoolSize etc.)
hibernte.cfg.xml
<hibernate-configuration>
<session-factory>
<!-- SQL Dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Database Connection Settings -->
<property name="hibernate.connection.driver_class">org.mariadb.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:aurora://xxxxx:3306/dbname</property>
<property name="hibernate.connection.username">xxxxx</property>
<property name="hibernate.connection.password">xxxxx</property>
<property name="hibernate.connection.assureReadOnly">true</property>
<property name="hibernate.connection.autoReconnect">true</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="hibernate.connection.maxPoolSize">5</property>
<property name="hibernate.connection.minPoolSize">2</property>
<property name="hibernate.connection.poolName">MyConnectionPool</property>
<property name="hibernate.connection.pool">true</property>
<property name="hibernate.connection.maxIdleTime">7000</property>
<property name="show_sql">true</property>
<!-- Specifying Session Context -->
<property name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property>
<!-- Mapping With Model Class Containing Annotations -->
<mapping class="com.xxx.aurora.beans.xxx" />
<mapping class="com.xxx.aurora.beans.User" />
</session-factory>
</hibernate-configuration>
I am able to connect with aurora mysql, but not able to set the connection on readonly. My requirement is that I want to create the connection with read-only replica for read queries using single cluster end point.
I am taking reference form mariadb-connector-j
I am also wondering that can I use the maria db connection pool with hibernate, if yes then how?
Change these Lines
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.url">jdbc:mysql:aurora://xxxxx:3306/dbname</property>
to
<property name="dialect">org.hibernate.dialect.MariaDBDialect</property>
<property name="connection.url">jdbc:mariadb://xxxxxx:3306/dbname</property>

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.

Database connection error when IP address is specified

this is my hibernate.cfg.xml
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://192.168.0.23:3306/meili_s?useUnicode=true&characterEncoding=UTF-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">50</property>
<property name="hibernate.c3p0.maxStatementsPerConnection">30</property>
<property name="hibernate.c3p0.timeout">3600</property> <!-- seconds -->
<property name="hibernate.c3p0.idle_test_period">300</property> <!-- seconds -->
<property name="hibernate.c3p0.testConnectionOnCheckin">true</property>
<property name="hibernate.c3p0.preferredTestQuery">select 1;</property>
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
The problem is that when the "hibernate.connection.url" has the IP address (of my VM) like just "localhost" all works but when it is an IP number (except for 127.0.0.1) i get this error:
Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
at org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:78)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
... 45 more
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
... 48 more
have you ever had some issue?
thanks lot in advance.

configure hibernate.cfg.xml for MySQL on Ubuntu 12.04

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>