Is some way to drop and create table on start of hibernate session? But only one from few mapped tables.
My hibernate configuration:
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/smart_home</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">false</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="pl.smart.home.database.User" />
<mapping class="pl.smart.home.database.UnrecognisedDevice" />
<mapping class="pl.smart.home.database.Sensor" />
<mapping class="pl.smart.home.database.TypeDevice" />
<mapping class="pl.smart.home.database.Event" />
<mapping class="pl.smart.home.database.RecognisedDevice" />
<mapping class="pl.smart.home.database.Status" />
</session-factory>
I want drop and create "event" table.
You can use create-drop
<property name="hbm2ddl.auto">create-drop</property>
Drop the schema and recreate it on SessionFactory startup. Additionally, drop the schema on SessionFactory shutdown.
Related
I have been trying since hours to set up JNDI in my project
My Hibernate file is like this :
<?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.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">mypass</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#big-data-3.logti.etsmtl.ca:1521:LOG660</property>
<property name="hibernate.connection.username">myTeam</property>
<property name="hibernate.default_schema">teamAlpha</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.search.autoregister_listeners">false</property>
<mapping resource="Scenariste.hbm.xml" />
<mapping resource="Utilisateur.hbm.xml" />
<mapping resource="Annonce.hbm.xml" />
<mapping resource="Genre.hbm.xml" />
<mapping resource="Location.hbm.xml" />
<mapping resource="Pays.hbm.xml" />
<mapping resource="Personnelfilm.hbm.xml" />
<mapping resource="Clientenretard.hbm.xml" />
<mapping resource="Film.hbm.xml" />
<mapping resource="Forfait.hbm.xml" />
<mapping resource="Employe.hbm.xml" />
<mapping resource="Client.hbm.xml" />
<mapping resource="Personneltournage.hbm.xml" />
</session-factory>
I'm aware to add JNDI it has to be something similar to this
hibernate.connection.datasource = java:/comp/env/jdbc/test
hibernate.transaction.factory_class = \
org.hibernate.transaction.JTATransactionFactory
hibernate.transaction.manager_lookup_class = \
org.hibernate.transaction.JBossTransactionManagerLookup
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
But how to fill the datasource and factory class and lookup class based on my info? my database name is teamAlpha
I'm using JBOSS pluggin for eclipse ,
Thank you
I am writing service in hibernate with c3p0 concept.And this service deployed to Raspberry pi.The raspbery pi having 512 mb RAM.I am hit the service continuously, when the processlist goes above 100 connections the mysql hanging up.And my service are not working. After restarting mysql only all processlist killed them self.Then services are working properly till the processlist reach above 100 connections.
My c3p0 configurations are(hibernate.cfg file),
<?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>
<!-- Database connection settings -->
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">mani</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="current_session_context_class">thread</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.c3p0.min_size">10</property>
<property name="hibernate.c3p0.max_size">100</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.maxIdleTime">1800</property>
<property name="hibernate.c3p0.maxIdleTimeExcessConnections">300</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">30</property>
<property name="hibernate.c3p0.validate">true</property>
<property name="hibernate.c3p0.preferredTestQuery">SELECT 1</property>
<property name="hibernate.c3p0.testConnectionOnCheckin">true</property>
<property name="hibernate.c3p0.testConnectionOnCheckout">false</property>
<!-- Mention here all the pojo classes along with their package name -->
<mapping class="com.nutans.objects.Dialog" />
<mapping class="com.nutans.objects.Office" />
<mapping class="com.nutans.objects.Welcome" />
<mapping class="com.nutans.objects.Sports"/>
</session-factory>
</hibernate-configuration>
Help me out!
I have JSF Application with Hibernate. I use Tomcat connection pool in hibernate.cfg.xml
<?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>
<property name="dialect">org.hibernate.dialect.PostgreSQL9Dialect</property>
<!-- using Tomcat connections -->
<property name="connection.datasource">java:/comp/env/jdbc/netstat</property>
<!-- Parameters for Hibernate connection provider
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/netstat</property>
<property name="connection.username">netstat</property>
<property name="connection.password">netstat</property>
-->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- configuration pool via c3p0 -->
<!--
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">100</property>
<property name="c3p0.max_statements">200</property>
<property name="c3p0.timeout">600</property>
-->
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping classes -->
<mapping class="ru.gooamoko.model.Group" />
<mapping class="ru.gooamoko.model.Host" />
<mapping class="ru.gooamoko.model.Traffic" />
<mapping class="ru.gooamoko.model.DailyTraffic" />
</session-factory>
</hibernate-configuration>
Project can be build with maven with all tests skipped and runs normal. But for tests souch Hibernate configuration is not applicable.
Is any way for this case to specify different configuration file for JUnit tests?
Best regards.
put the test file in src/test/resources and that will take precedence in classpath while executing tests
I am trying to run my first Hibernate Program.
I can't figure out the problem, because the matching end-tag is actually there?
Thanks for your help in advance!
Error parsing XML: /hibernate.cfg.xml(11) The element type "session-factory" must be terminated by the matching end-tag
hibernate.cfg.xml:
<?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>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class" /> com.mysql.jdbc.Driver </property>
<property name="hibernate.connection.url"/>jdbc:mysql://localhost:3306/westbahn</property>
<property name="hibernate.connection.username" />root</property>
<property name="hibernate.connection.password" />secretpassword</property>
<property name="hibernate.dialect" />org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="connection.pool_size">1</property>
<property name="hibernate.dialect"> org.hibernate.dialect.MySQLInnoDBDialect" </property>
<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>
Hibernate configuration file should look like this
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:#localhost:1521:xe</property>
<property name="connection.user">xxx</property>
<property name="connection.password">xxxx</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="xxxx.hbm.xml"/>
</session-factory>
</hibernate-configuration>
This could help you out!!
Your 5 first property elements are wrong: you use self-closing elements although they have a body. For example:
<property name="hibernate.connection.driver_class" /> com.mysql.jdbc.Driver </property>
should be
<property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property>
no slash here --^
hibernate is not able to create a table why...?
<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/e_shop</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.show_sql">true</property>
AnnotationConfiguration cfs=new AnnotationConfiguration();
cfs.configure("daoHibernate/hibernate.cfg.xml");
new SchemaExport(cfs).create(true,true);
my all table are getting alter and table getting drop then again created why how to start a new session please give a reply
<property name="hibernate.hbm2ddl.auto">create</property>
because of this, each time it will drop and create tables.
<property name="hibernate.hbm2ddl.auto">update</property>
use update instead of create.