Hi i created a Hibernate powered hsql embedded swing app..When i run eclipse juno version it run perfectly..After that i export as runnable jar file as run it.But does not retrieve,add data database...!!!
Project directory like
Project
..src
.....dao
.....daoimpl
.....sevice
.....serviceimpl
.....domain
.....main
.....hibernate.cfg.xml
..db/hsql/library
.............library.script
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>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="hibernate.connection.url">jdbc:hsqldb:db/hsql/library;shutdown=true</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">sa</property>
<property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- JDBC connection pool (use the built-in one) -->
<property name="connection.pool_size">1</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property
name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- disable batching so HSQLDB will propagate errors correctly. -->
<property name="jdbc.batch_size">0</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- List all the mapping documents we're using -->
<mapping class="com.habitz.librarymanagement.domain.Admin" />
<mapping class="com.habitz.librarymanagement.domain.Book" />
<mapping class="com.habitz.librarymanagement.domain.Category" />
<mapping class="com.habitz.librarymanagement.domain.Group" />
<mapping class="com.habitz.librarymanagement.domain.Issue" />
<mapping class="com.habitz.librarymanagement.domain.Member" />
</session-factory>
</hibernate-configuration>
Someone know please help...!!
This property creates a database relative to where the jar was executed from.
<property name="hibernate.connection.url">jdbc:hsqldb:db/hsql/library;shutdown=true</property>
Relative URL's do not work. Use an absolute URL. For example, you can use the user's directory for the database files, or a directory that your app is installed in.
Related
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 writing my first example for Inserting Record using Hibernate Framework .
To start with I am trying to following instructions given at URL
You can see my code at GIT Hibernate Basic
When calling below piece of code
public Long saveEmployee(Employee emp){
Session session = new Configuration().configure().buildSessionFactory().openSession();
session.beginTransaction();
Long id = (Long) session.save(emp);
session.getTransaction().commit();
session.close();
return id;
}
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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root123</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</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">false</property>
<!-- validate | update | create | create-drop -->
<!-- validate: validate the schema, makes no changes to the database.
update: update the schema.
create: creates the schema, destroying previous data.
create-drop: drop the schema at the end of the session. -->
<property name="hbm2ddl.auto">validate</property>
<!-- added as suggested in below answer-->
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
It throws org.hibernate.TransactionException: Unable to locate JTA UserTransaction
Details :
I am not deploying the application from any Application server I am running it as plan Java application.
I am using Hibernate 4.1
Is it neccessary to deploy it in Application Server ?
Whether we have to explicitly specify the JTAUsertransaction in Hibernate 4.1 version?
=============== Update ==============
With
hibernate.transaction.factory_class = org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
Solved this error and I can execute the program .
But I still wanted to know the reason why its not working when using org.hibernate.transaction.JTATransactionFactory
When used
hibernate.transaction.factory_class = org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
Solved this error and I can execute the program.
I have deliberately posted it as an answer so it would be helpful for someone.
You would be missing the JTA configuration in your hibernate config file
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
I have a problem in my maven project
click the links below to see it
I added this to my hibernate.cfg but didn't work
<mapping package="com.redpass.entities"/>
<mapping class="com.redpass.entities.MyReference"/>
console error
Project Explorer
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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/avocatbd</property>
<property name="connection.username">root</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- <mapping package="com.redpass.entities"/>
<mapping class="com.redpass.entities.MyReference"/> -->
</session-factory>
</hibernate-configuration>
I should add this:
configuration.configure("hibernate.cfg.xml");
return configuration
.buildSessionFactory(new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.build());
to my HibernateUtil Class to tell that we want to configure using the hibernate.cfg.xml file and map the classes like this:
<mapping class="com.redpass.entities.Partie"/>
<mapping class="com.redpass.entities.Societe"/>
After deploying a web application with maven to heroku cloud, I get log error:
app[web.1]: org.hibernate.HibernateException: hibernate.cfg.xml not found.
The web application running locally correctly, but on the cloud not.
This is my 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>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/todoitemsdb
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping class="il.ac.hit.model.User" resource="todoitems.xml"/>
<mapping class="il.ac.hit.model.ToDoItem" resource="users.xml"/>
</session-factory>
</hibernate-configuration>
What can I do?
Make sure that the hibernate.cfg.xml is bundled into your jar like this:
your-app.jar\hibernate.cfg.xml
That's where Hibernate tries to locate the configuration file.
I had my hibernate.cfg.xml under resources under a folder config
Changing the below line
cfg.configure("./config/hibernate.cfg.xml");
to
cfg.configure("config/hibernate.cfg.xml");
worked.
I'm trying to set up Hibernate, and since 4.0.0 just came out I naturally decided to go with that. It seems that no matter what way I try to create a SessionFactory, it always leads to the same error:
Initial SessionFactory creation failed.java.util.ServiceConfigurationError: org.hibernate.integrator.spi.Integrator: Provider org.hibernate.envers.event.EnversIntegrator could not be instantiated: java.lang.ClassCastException: Cannot cast org.hibernate.envers.event.EnversIntegrator to org.hibernate.integrator.spi.Integrator
It seems like there is something wrong with my Hibernate configuration but I can't figure out what. Here's my 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>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://192.168.0.17:5432/mydb</property>
<property name="connection.username">myusrname</property>
<property name="connection.password">mypasswd</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- c3p0 configuration -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>
Does anyone spot anything out of the ordinary, or do you have other ideas?
I found no way to get Hibernate 4.0.0 CR4 working. Switching to 3.6.7 resolved the problem and works just fine. I conclude that there must be some bug in the release, and will file a bug report.