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
Related
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.
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>
I'm new to glass-fish & persistence and while trying to run a query on MySQL database in my machine i get the following error:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: No database selected
Error Code: 1046
before running the server i made sure that the server configuration are done using this guide : mysql site manual for using connector with glassfish, and configured my persistence.xml like this:
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="StudentRecipieWebsite" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>jdbc/mySql</non-jta-data-source>
<class>il.musehunter.studentRecipes.dbModel.Image</class>
<class>il.musehunter.studentRecipes.dbModel.Ingrediant</class>
<class>il.musehunter.studentRecipes.dbModel.Recipe</class>
<class>il.musehunter.studentRecipes.dbModel.User</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/recipes_data"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="moshe1475"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="eclipselink.jdbc.batch-writing" value="JDBC"/>
</properties>
</persistence-unit>
I'm really new to this and does not have a clue to what i should do next so any help would be very appreciated.
edit:
adding part of the domain.xml from my glassfish server :
<applications>
<application context-root="/StudentRecipieWebsite" location="${com.sun.aas.instanceRootURI}/applications/StudentRecipieWebsite/" name="StudentRecipieWebsite" object-type="user">
<property name="appLocation" value="${com.sun.aas.instanceRootURI}/applications/__internal/StudentRecipieWebsite/StudentRecipieWebsite.war"></property>
<property name="org.glassfish.ejb.container.application_unique_id" value="89812698949156864"></property>
<property name="org.glassfish.persistence.app_name_property" value="StudentRecipieWebsite"></property>
<property name="defaultAppName" value="StudentRecipieWebsite"></property>
<module name="StudentRecipieWebsite">
<engine sniffer="ejb"></engine>
<engine sniffer="security"></engine>
<engine sniffer="jpa"></engine>
<engine sniffer="web"></engine>
</module>
</application>
and:
<resources>
<jdbc-resource pool-name="__TimerPool" jndi-name="jdbc/__TimerPool" object-type="system-admin"></jdbc-resource>
<jdbc-resource pool-name="DerbyPool" jndi-name="jdbc/__default"></jdbc-resource>
<jdbc-connection-pool datasource-classname="org.apache.derby.jdbc.EmbeddedXADataSource" res-type="javax.sql.XADataSource" name="__TimerPool">
<property name="databaseName" value="${com.sun.aas.instanceRoot}/lib/databases/ejbtimer"></property>
<property name="connectionAttributes" value=";create=true"></property>
</jdbc-connection-pool>
<jdbc-connection-pool is-isolation-level-guaranteed="false" datasource-classname="org.apache.derby.jdbc.ClientDataSource" res-type="javax.sql.DataSource" name="DerbyPool">
<property name="PortNumber" value="1527"></property>
<property name="Password" value="APP"></property>
<property name="User" value="APP"></property>
<property name="serverName" value="localhost"></property>
<property name="DatabaseName" value="sun-appserv-samples"></property>
<property name="connectionAttributes" value=";create=true"></property>
</jdbc-connection-pool>
<jdbc-connection-pool datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" res-type="javax.sql.DataSource" name="MySQLConnPool">
<property name="portNumber" value="3306"></property>
<property name="databaseName" value="recipes_data"></property>
<property name="serverName" value="localhost"></property>
<property name="user" value="root"></property>
<property name="password" value="moshe1475"></property>
<property name="URL" value="jdbc:mysql://:3306/"></property>
</jdbc-connection-pool>
<jdbc-resource pool-name="MySQLConnPool" description="" jndi-name="jdbc/mySql"></jdbc-resource></resources>
My first guess is that the connection URL in the data source definition is not quite right because "No database selected Error Code: 1046" is a MySQL error.
You do not need to configure the data source in persistence.xml if you have already defined a data source in your application server (which is the common way). Can you post the data source definition from your $GLASSFISH_HOME/glassfish/domains/$GLASSFISH_DOMAIN/config/domain.xml?
This is an example persistence.xml I took from an project of mine (more or less from the Netbeans template) that might help you:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="MyPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/sample</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
Good luck!
Answering to the comment:
You need to add the database name to the URL:
<property name="URL" value="jdbc:mysql://nameOrIPOfYourServerOrLocalhost:3306/nameOfYourDatabase"></property>
That should resolve the problem.
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>
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.