How to use Vaadin and Hibernate Envers - java

I am having trouble figuring out how to use Hibernate Envers and Vaadin. I have a vaadin web application which has tables of information which users can edit. I am using a postgresql to store the data, and have used Hibernate to create the Bean classes (I have multiple Bean classes and multiple SQL tables) and linking to the DB. I am trying to figure out how to use Envers to keep track of user modifications to the table (if they added something, deleted, or edited and the username of the user and the time at which it happened). Here is my hibernate.cfg.xml 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 name="">
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/appdb</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping
resource="com/webapp/ui/database/Person.hbm.xml" />
<mapping
resource="com/webapp/ui/database/Occupation.hbm.xml" />
<mapping
resource="com/webapp/ui/database/Address.hbm.xml" />
<mapping
resource="com/webapp/ui/database/Car.hbm.xml" />
<mapping
resource="com/webapp/ui/database/Education.hbm.xml" />
<listener class="org.hibernate.envers.event.AuditEventListener" type="post-insert"/>
<listener class="org.hibernate.envers.event.AuditEventListener" type="post-update"/>
<listener class="org.hibernate.envers.event.AuditEventListener" type="post-delete"/>
<listener class="org.hibernate.envers.event.AuditEventListener" type="pre-collection-update"/>
<listener class="org.hibernate.envers.event.AuditEventListener" type="pre-collection-remove"/>
<listener class="org.hibernate.envers.event.AuditEventListener" type="post-collection-recreate"/>
</session-factory>
</hibernate-configuration>
I also added the #Audited to the Bean classes. After doing this it does not seem to work, the audit tables are not being created.

Related

How to add JNDI Based on my Config file

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

Hibernate Configration file error

I am new to hibernate framework. i made a sample project just to test hibernate. i set up the project. but when i run it i got this error
org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1405)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1427)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:972)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:69)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:966)
The Hibernate.cfg.xml file that i put in the main package of the project.
<?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.DerbyDialect
</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="connection.pool_size">2</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!--<property name="htm2ddl.auto">create</property>-->
<property name="hibernate.connection.driver_class">
org.apache.derby.jdbc.ClientDriver
</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">
jdbc:derby://localhost:1527/NetworkDB
</property>
<property name="hibernate.connection.username">
</property>
<property name="hibernate.connection.password">
</property>
<!-- List of XML mapping files -->
<!-- <mapping resource="Employee.hbm.xml"/>-->
</session-factory>
</hibernate-configuration>
I am using netbeans 7.0
As per DTD, <hibernate-configuration> should have a single <session-factory> but you have declared it twice.
Regarding second error:
org.hibernate.HibernateException: /hibernate.cfg.xml not found
Hibernate looks for the configuration file at the root of the classpath, so check if you have placed this file at root of your classpath.
So once you build your project the hibernate.cfg.xml file should be in classes directory.
Add the following in first line of your xml:
<?xml version="1.0" encoding="UTF-8"?>

How to make different Hibernate configuration for tests?

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

Hibernate auto create database

I have a Java EE Hibernate project, and I'm using MySQL as a database.
I want that when I first time run the project, it will create the database automatically.
This is my hibernate.cnf.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>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/InternetProject</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.pool_size">10</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping class="entities.Business" />
<mapping class="entities.Coupon" />
<mapping class="entities.User" />
<mapping class="entities.LastLogin" />
</session-factory>
</hibernate-configuration>
When I first time run this project on another computer, how can I make the database InternetProject to be created?
According to the config file, it might already do it and I'm not aware to it.
Thanks in advance.
<property name="hibernate.hbm2ddl.auto">create</property>
will create tables. But it will not create database. Change the connection url to generate the database.
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/InternetProject?createDatabaseIfNotExist=true</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.pool_size">10</property>
Update : character encoding when creating database
<property name="connection.url">
jdbc:mysql://localhost/InternetProject?createDatabaseIfNotExist=true&useUnicode=yes&characterEncoding=UTF-8
</property>
<property name="hibernate.hbm2ddl.auto">create</property>
will do
Hibernate will not create the database for you, only the tables. To create the database you need to tell MySQL to create it if it does'nt already exist by adding a parameter to the URL. E.g.:
jdbc:mysql://db:3306/mydatabase?createDatabaseIfNotExist=true
There are multiple option for auto property.
create - It creates new tables corresponding mapping or annotation. It drops existing tables and data.
update - It keeps existing data and tables. It updates schema. here we have to take care contrants.
create-drop - It is same like create but once session gets closed it drops everything.
validate - it validates or matches schema with map or annotation. It's valid for Production environment.
<!-- create create-drop validate update -->
<property name="hbm2ddl.auto">update</property>
I hope it helps.

Hibernate Mapping Files Not Found(MappingNotFoundException)

I followed this tutorial: http://manikandanmv.wordpress.com/2011/04/13/hibernate-basics-simple-example/
This is my file structure
http://i.stack.imgur.com/sX3nn.jpg
When I run Java Applet, I get this error
Initial SessionFactory creation failed.org.hibernate.MappingNotFoundException: resource: src/com/bookstore/bookapp.hbm.xml not found
However, I have that file there as you can see it. When I put the file under
workspace-windows\226project1\src\com\bookstore
I'm still facing this error even then, Can someone help?
This is my hibernate.cfg.xml
<!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.url">jdbc:postgresql://localhost:5432/postgres</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.username">postgres</property>
<property name="connection.password">rocker123</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="src/com/bookstore/bookapp.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Move your mapping files into src/main/resources. Only .java files belong in src/main/java.
There is an example project on github showing how to correctly reference XML mapping files from a hibernate.cfg.xml. Please refer to https://github.com/zzantozz/testbed/tree/master/hibernate-with-xml-mappings

Categories