I'm an Hibernate beginner user, I created a very simple application to test it !
But I get this error in the console :
Initial SessionFactory creation failed.org.hibernate.HibernateException: Dialect class not found: org.openmeetings.app.hibernate.utils.MySQL5MyISAMDialect
Exception in thread "main" java.lang.ExceptionInInitializerError
at util.HibernateUtil.buildSessionFactory(HibernateUtil.java:20)
at util.HibernateUtil.<clinit>(HibernateUtil.java:10)
at DAO.services.addProduit(services.java:9)
at test.main(test.java:11)
Caused by: org.hibernate.HibernateException: Dialect class not found: org.openmeetings.app.hibernate.utils.MySQL5MyISAMDialect
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:81)
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:42)
at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:422)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:128)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
at util.HibernateUtil.buildSessionFactory(HibernateUtil.java:15)
... 3 more
My Test class :
import DAO.services;
import DAO.Produit;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
services s = new services();
Produit p = new Produit("PC","Sony Vaio",(double )7500);
s.addProduit(p);
}
}
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">
<!-- Generated file - Do not edit! -->
<hibernate-configuration>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory>
<!-- User / Password -->
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- Database Settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- for performance reasons changed to MyISAM from org.hibernate.dialect.MySQLInnoDBDialect -->
<property name="dialect">org.openmeetings.app.hibernate.utils.MySQL5MyISAMDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/gestProd</property>
<property name="hibernate.connection.CharSet">utf8</property>
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.useUnicode">true</property>
<!-- Database Scheme Auto Update -->
<property name="hbm2ddl.auto">update</property>
<!-- properties -->
<property name="show_sql">true</property>
<property name="use_outer_join">false</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!--
<property name="connection.provider_class ">org.hibernate.connection.C3P0ConnectionProvider</property>
-->
<property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.generate_statistics">false</property>
<property name="hibernate.cache.use_structured_entries">false</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">2</property>
<property name="hibernate.c3p0.idle_test_period">100</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.timeout">100</property>
<!-- mapping files -->
<mapping resource="DAO/Categorie.hbm.xml"/>
<mapping resource="DAO/Produit.hbm.xml"/>
</session-factory>
</hibernate-configuration>
service.java :
package DAO;
import org.hibernate.Session;
import util.HibernateUtil;
public class services {
public void addProduit(Produit p){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(p);
session.getTransaction().commit();
}
}
AND finally 2 classes for my objects ( Produit and Categorie ) and their Configuration files !
What do you think may be the source of this error ?
Thank you :)
The dialect for MySql with MyISAM(if that is what you have) should be:
org.hibernate.dialect.MySQLMyISAMDialect
Related
I am using a 5.4.15.Final version of hibernate. When I am running my application in create mode it is not dropped and create the tables. Is there any way I can do it? I remember I was able to do the same in older version(don't exactly remember the version). My hibernate.cfg.xml file is below:
hibernate.cfg.xml
<?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.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/test</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">5</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.default_schema">test</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
<property name="output.record.batch.size">10</property>
<property name="javax.persistence.schema-generation.create-source">metadata</property>
<property name="javax.persistence.schema-generation.scripts.action">create</property>
<property name="javax.persistence.schema-generation.database.action">create</property>
<property name="hibernate.hbm2dll.create_namespaces">true</property>
<property name="javax.persistence.schema-generation.scripts.create-target">sql/executors_create.sql</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL81Dialect</property>
</session-factory>
</hibernate-configuration>
I am creating the sessionFactory object as below:
SessionFactory sessionFactory = new Configuration().configure().addAnnotatedClass(Test.class).buildSessionFactory();
Is there any way to solve this?
**How to Optimize Hibernate buildSessionFactory();"
The method 'cfg.buildSessionFactory();' of Hibernate consumes a lot of CPU. And on low-end systems is taking more than 20 seconds.
Is there anyway I can cache it and just load manually?
Im stuck on optimizing this. Whats the proper way to make 'cfg.buildSessionFactory();' faster?
public static void initHibernate() throws Exception {
System.out.println("Initializing Hibernate.....");
String username = Cryptographer.decrypt(MySettings.getDb_username());
String password = Cryptographer.decrypt(MySettings.getDb_password());
String url = Cryptographer.decrypt(MySettings.getDb_url());
cfg = new Configuration()
.configure("hibernate.cfg.xml")
.setProperty("hibernate.connection.url", url)
.setProperty("hibernate.connection.username", username)
.setProperty("hibernate.connection.password", password)
.setProperty(username, url);
//Error: Hibernate Could not deserialize cache file:
//File fileAux = new File("A:\\hibernate.cfg.xml");
//cfg.addCacheableFile(fileAux);
sessions = cfg.buildSessionFactory();
}
Additional Information:
compile group: 'org.hibernate', name: 'hibernate-c3p0', version: '5.4.27.Final'
Mapping xml file: (I redacted private information as REDACTED)
<?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>
<!-- UPDATE OR NONE -->
<property name ="hibernate.hbm2ddl.auto">none</property>
<!-- -->
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url"></property>
<property name="hibernate.connection.username"></property>
<property name="hibernate.connection.password"></property>
<!-- -->
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property name="hibernate.order_updates">true</property>
<!-- Just set the correct isolation to default(1). Stops logging messages -->
<property name="hibernate.connection.isolation">1</property>
<!-- Might Improve Performance -->
<property name="hibernate.jdbc.batch_versioned_data">true</property>
<property name="hibernate.connection.autoReconnect">true</property>
<property name="hibernate.connection.autoReconnectForPools">true</property>
<!-- fix time zone -->
<property name="hibernate.jdbc.time_zone">UTC</property>
<!-- hibernate -->
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
<property name="hibernate.connection.pool_size">3</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.format_sql">false</property>
<property name="hibernate.jdbc.batch_size">360</property>
<property name="hibernate.max_fetch_depth">30</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- C3PO -->
<property name="hibernate.c3p0.max_size">3</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.timeout">240</property>
<property name="hibernate.c3p0.max_statements">180</property>
<property name="hibernate.c3p0.idle_test_period">30</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.testConnectionOnCheckout">false</property>
<property name="hibernate.c3p0.testConnectionOnCheckin">true</property>
<property name="hibernate.c3p0.preferredTestQuery">SELECT 1</property>
<!-- Mapping -->
<mapping class="br.REDACTED.models.REDACTED"/>
<mapping class="br.REDACTED.models.REDACTED"/>
<mapping class="br.REDACTED.models.REDACTED"/>
<mapping class="br.REDACTED.models.REDACTED"/>
<mapping class="br.REDACTED.models.REDACTED"/>
<mapping class="br.REDACTED.models.REDACTED"/>
<mapping class="br.REDACTED.models.REDACTED"/>
<mapping class="br.REDACTED.models.REDACTED"/>
<mapping class="br.REDACTED.models.REDACTED"/>
<mapping class="br.REDACTED.models.REDACTED"/>
<mapping class="br.REDACTED.models.REDACTED"/>
<mapping class="br.REDACTED.models.REDACTED"/>
<mapping class="br.REDACTED.models.REDACTED"/>
<mapping class="br.REDACTED.models.REDACTED"/>
</session-factory>
</hibernate-configuration>
References
https://forum.hibernate.org/viewtopic.php?p=2242766
https://developer.jboss.org/thread/197521
https://github.com/spring-projects/spring-framework/issues/18305
https://docs.jboss.org/hibernate/orm/5.2/topical/html_single/bytecode/BytecodeEnhancement.html
Whenever I run my server my database table are re-created and all my previous data are being deleted. I was thinking this might be the problem in hibernate configuration file but don't know the actual reason hope you awesome guys will figure me out .
I also tried changing <property name="hibernate.hbm2ddl.auto">update</property> to auto , validate but didnot help
<?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.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/restro
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
root
</property>
<!-- <property name="connection.release_mode">auto</property> -->
<!-- Drop and re-create the database schema on startup -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Names the annotated entity class -->
<mapping class="com.restroo.model.MenuItems"/>
<mapping class="com.restroo.model.AdminUser"/>
</session-factory>
</hibernate-configuration>
I am hoping someone can help me. My database connection keeps getting closed and will not reopen so I am having to restart my application every few hours.
I have seen other answers here and have tried implementing them but they dont seem to make any difference. My JDBC url has autoReconnect=true and I am using hibernate with a c3p0 connection pool.
Any help would be great!
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.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/devenv38?autoReconnect=true</property>
<property name="hibernate.connection.username">devenv38</property>
<property name="hibernate.connection.password">devenv38</property>
<property name="hibernate.connection.autoReconnect">true</property>
<property name="hibernate.connection.autoReconnectForPools">true</property>
<property name="connection.is-connection-validation-required">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Use the C3P0 connection pool provider -->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="c3p0.acquire_increment">5</property>
<property name="c3p0.max_size">500</property>
<property name="c3p0.max_statements">0</property>
<property name="c3p0.idleConnectionTestPeriod">120</property>
<property name="c3p0.initialPoolSize">25</property>
<property name="c3p0.min_size">25</property>
<property name="c3p0.numHelperThreads">15</property>
<property name="c3p0.timeout">0</property> <!-- seconds -->
<property name="hibernate.show_sql">false</property>
<property name="hibernate.format_sql">false</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- resources -->
<mapping class="entities.Story" />
<mapping class="entities.User" />
</session-factory>
Database Connection Class
public class DatabaseConnection
{
static SessionFactory sf;
private DatabaseConnection()
{
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(
configuration.getProperties()).buildServiceRegistry();
sf = configuration.buildSessionFactory(sr);
}
public static Session getSession()
{
if (sf == null)
{
new DatabaseConnection();
}
return sf.openSession();
}
}
Example usage
Session session = DatabaseConnection.getSession();
Transaction txn = session.beginTransaction();
User user = new User();
user.setUsername("JUNIT USER 1");
user.setEmail("test#test.com");
user.setPassword("Test Password");
session.save(user);
txn.commit();
I am new to Hibernate .... i tried this code but i got Could not parse configuration: NewFile.cfg.xml (name of .cfg.xml file)
<?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>
<!-- connection to oracle -->
<propertyname="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver </property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#localhost:1521:XE
</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.connection.password">system</property>
<!-- autocommit -->
<property name="hibernate.connection.autocommit">false</property>
<!-- to display sql query generated while running program -->
<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 file -->
<mapping resource="Mapping.hbm.xml"/>
</session-factory>
The end tag for <hibernate-configuration> is missing:
</hibernate-configuration>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver </property>
Your opening tag not match the ending tag.