Hibernate `create` not dropping table on `5.4.15.Final` version - java

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?

Related

Every time I run server my new table is being created and previous data are deleted

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>

Hibernate c3p0 configuration has no affect

I'm trying to add c3p0 connection pool to our existing hibernate configuration.
But it has no affect and hibernate still uses default pool.
Hibernate Configuration alone is working fine so i am absolutely sure no problem with it.
Wondering if someone here can point my mistake Or suggest how i can debug the problem ?
I do not use Spring framework.
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.ibm.db2.jcc.DB2Driver</property>
<property name="hibernate.connection.password">passwd</property>
<property name="hibernate.connection.url">jdbc:db2://server:port/database</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.default_schema">DB Schema</property>
<property name="hibernate.dialect">org.hibernate.dialect.DB2Dialect</property>
<property name="hibernate.format_sql">false</property>
<property name="hibernate.max_fetch_depth">4</property>
<property name="hibernate.search.autoregister_listeners">false</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.connection.isolation">1</property>
<property name="hibernate.jdbc.batch_size">50</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">3000</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">900</property>
<mapping resource="table.hbm.xml" />
</session-factory>
</hibernate-configuration>
Here are the jars i have:
hibernate3.jar
hibernate-c3p0-3.5.0-Final.jar
Statements from the logs:
hibernate.cfg.Environment - Hibernate 3.5.0-Final
INFO ManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
INFO ManagerConnectionProvider - Hibernate connection pool size: 20
Try configuring a connection provider class from c3p0.
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
Also, it's better to add the hibernate configuration doctype, which will help in finding any typos.
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
UPDATE
You need to include the mchange library in your classpath. This link might helpful to you.

After implementing C3P0 the page keeps refreshing

I am trying to implement C3P0 into my hibernate. I have as follows:
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<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://IPaddress</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.current_session_context_class">thread</property>
<!-- JDBC connection pool (use the built-in) -->
<!--<property name="connection.pool_size">20</property>-->
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">1000</property>
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.max_statements">10</property>
<property name="hibernate.c3p0.min_size">10</property>
<property name="hibernate.c3p0.timeout">864000</property>
<property name="hibernate.c3p0.idleConnectionTestPeriod">30</property>
<property name="hibernate.c3p0.initialPoolSize">10</property>
<property name="hibernate.c3p0.maxPoolSize">100</property>
<property name="hibernate.c3p0.minPoolSize">10</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Mappings -->
<mapping class="com.nebuilder.ats.pojo.TopicsDetails"/>
<mapping class="com.nebuilder.ats.pojo.GroupsDetails"/>
<mapping class="com.nebuilder.ats.pojo.ModulesDetails"/>
<mapping class="com.nebuilder.ats.pojo.TraineesDetails"/>
<mapping class="com.nebuilder.ats.pojo.ColoursDetails"/>
<mapping class="com.nebuilder.ats.pojo.CustomersDetails"/>
<mapping class="com.nebuilder.ats.dao.MusicStoreDaoImpl"/>
</session-factory>
</hibernate-configuration>
ApplicationContext.xml
<beans>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:jdbc.properties"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"
p:connectionProperties="${jdbc.connectionProperties}"/>
<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
</beans>
Both of the files are in my resources folder. They seem to be working, but the problem is that my page keeps loading or refreshing without displaying any information when I try to access the database.
I am using jars as follow - hibernate-c3p0 3.6.3.Final, hibernate-core 3.6.3.Final, c3p0 0.9.1.2
Did you try to debug your application to see where is the problem ?
Moreover, it seems to be a little bit excessive to define a timeout of 864 000 ms, isn't it ?

Prefix for hibernate connection configurations?

Does anybody know about the hibernate configurations file (hibernate.cfg.xml), what are the "connection" properties - without the "hibernate" prefix - used for?
I mean, why do those properties (such as connection.url, connection.usermame...) exist?
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">org.postgresql.Driver</property>
...
<property name="connection.url">SOME_URL</property>
<property name="hibernate.connection.url">SOME_URL</property>
<property name="connection.username">SOME_USER</property>
<property name="hibernate.connection.username">SOME_USER</property>
...
Why can I use the both connection.url and hibernate.connection.url ?
ANSWER:
Ok, I believe those properties (without the hibernate. prefix) exist just for backward compatibility with configuration files of older hibernate versions.
Thanks.
The hibernate.connection.url is use to connect the database url following is the configuration setting for hibernate . Hope It will help you.
<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/youdb</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="emp/dto/Employee.hbm.xml"/>

how to solve Null user or password not supported in THIN driver

i am getting Null user or password not supported in THIN driver when configure jpa with hibernate configuration in seam
components.xml
<persistence:hibernate-session-factory name="hibernateSessionFactory" cfg-resource-name="hibernate.cfg.xml"/>
<persistence:managed-hibernate-session name="session"
auto-create="true"
session-factory-jndi-name="java:/mobeeSessionFactory"/>
hibernate.cfg.xml
<hibernate-configuration>
<session-factory name="java:/mobeeSessionFactory">
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#localhost:1521:mobee</property>
<property name="hibernate.connection.username">mobeemigrate</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.default_entity_mode">pojo</property>
<property name="hibernate.session_factory_name">java:/mobeeSessionFactory</property>
<property name="hibernate.connection.datasource">mobeeadminDataSource</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.auto_close_session">false</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="hibernate.transaction.flush_before_completion">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- Here are the mappings -->
<mapping class="tempCustomers" package="com.manam.mobee.persist.entity.TempCustomers"/>
<mapping class="tempAccounts" package="com.manam.mobee.persist.entity.TempAccounts"/>
</session-factory>
</hibernate-configuration>
projectname-ds.xml
<local-tx-datasource>
<jndi-name>mobeeadminDataSource</jndi-name>
<use-java-context>false</use-java-context>
<connection-url>jdbc:oracle:thin:#localhost:1521:mobee</connection-url>
<driver-class>oracle.jdbc.OracleDriver</driver-class>
<user-name>mobeemigrate</user-name>
<password>mobeemigrate</password>
</local-tx-datasource>
The password is missing from hibernate.cfg.xml.
To fix include the following in your hibernate.cfg.xml file as shown below:
<property name="hibernate.connection.password">mobeemigrate</property>
hibernate.cfg.xml
<hibernate-configuration>
<session-factory name="java:/mobeeSessionFactory">
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#localhost:1521:mobee</property>
<property name="hibernate.connection.username">mobeemigrate</property>
<property name="hibernate.connection.password">mobeemigrate</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.default_entity_mode">pojo</property>
<property name="hibernate.session_factory_name">java:/mobeeSessionFactory</property>
<property name="hibernate.connection.datasource">mobeeadminDataSource</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.auto_close_session">false</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="hibernate.transaction.flush_before_completion">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- Here are the mappings -->
<mapping class="tempCustomers" package="com.manam.mobee.persist.entity.TempCustomers"/>
<mapping class="tempAccounts" package="com.manam.mobee.persist.entity.TempAccounts"/>
</session-factory>
</hibernate-configuration>
Your hibernate.cfg.xml has the following property:
<property name="hibernate.connection.username">mobeemigrate</property>
But not the corresponding password one:
<property name="hibernate.connection.password">mobeemigrate</property>

Categories

Resources