Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts

Monday, October 12, 2015

Tapestry5, JPA 2 and Hibernate

The documentation for Integrating JPA found on the Tapestry5 website was lacking.
  1. It doesn't tell you that you have add the Tapestry-jpa dependency in you project POM
  2. It uses EclipseLink
  3. Explicitly tell you to use JPA 2.0 NOT JPA 2.1;
So, I had a few problems to fix after reading and following the docs. With much tinkering with my Tapestry5 (5.4-beta-35) project, I figured out:
  1. To add the Tapestry-jpa dependency
    <dependency> <groupId>org.apache.tapestry</groupId> <artifactId>tapestry-jpa</artifactId> <version>5.4-beta-35</version> </dependency>
        

  2. Hibernate 4.2 instead of EclipseLink
    <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.2.20.Final</version> </dependency>
  3. Edit the persistence.xml header so it reads version=2.0 instead of 2.1. This is because my Netbeans IDE can generate the persistence.xml file but defaults to 2.1 version instead of 2.0.
And if you get a javax.persistence.PersistenceException: Unable to build EntityManagerFactory error during the build, you probably didn't add a hibernate.dialect property in your persistence.xml configs.

Wednesday, July 18, 2012

Tapestry-hibernate and that ValueEncoder exception

I love tapestry5 but sometimes it just does something bad. In this instance, its a ValueEncoder error you get when your are using the default transitive dependencies from Maven. If just followed the "Using Tapestry with Hibernate" you might end up with a HTML 500 error with tapestry5 telling you about a ValueEncoder exception.

A first I thought it was my stuff (Netbeans + MSSQL + Glassfish) but I eventually figured out it wasn't by process of elimination.

The way to fix this is to change something in your POM file. Look for the hibernate dependency entry. You will notice that its using older version of hibernate.

<dependency>  
    <groupId>org.hibernate</groupId>  
    <artifactId>hibernate</artifactId>  
    <version>3.2.5.ga</version>  
</dependency>  

I just changed this to to use the newer version of the hibernate. Also changed it from hibernate to hibernate-core. So the dependency would read:
<dependency>  
    <groupId>org.hibernate</groupId>  
    <artifactId>hibernate-core</artifactId>  
    <version>4.1.4.Final</version>  
</dependency>  

It would be also a good idea to update the rest dependencies.

EDIT: Apparently the Tapestry-hibernate module doesn't like 4.1.4.Final version of Hibernate. The Hibernate Session will fail when you use it. The fix is just to revert Hibernate to anything in the 3.xx branch like 3.4.xx.

Sunday, November 7, 2010

SpringHibernateVaading

A few weeks (maybe a couple months ago - SO SUE ME!), I figured out how to get a Spring WebApplicationContext into a Vaadin application, so I tried to figure out how to get Hibernate into it with a working DAO to boot. It took a few hours but I think I have this puppy wired. Get it?

Here's a screenshot:
singgihpraditya blog help me get the bits working together. Spring Framework breaks your application into small bits and wire them together using XML config files. So, the magic is really in the config files.

Now since I am using Netbeans as my development IDE, I didn't have to deploy a mySQL database for this. Netbeans has a since Derby "Sample" database - being a sample it already has data.

Don't forget to import the derbyclient.jar. It took me a few to figure that one out. The first time around I thought that my database properties file was source of the error along with the Spring config file.

Here is another screenshot with the web application running on Tomcat.


Aryan, Bhadz, Cholo, Carl, Kim don't hate!

Sunday, October 18, 2009

Homer Simpson on Hibernate: DAO! (Part 1)

Homer's got nothing to do with this - besides he's a nuclear technician not a web developer.

This where we add our database part of our addressbook web application. This part is actually pretty long so I am going to split it into two maybe three parts.

Let's get started...

We will be using a design pattern called Data Access Objects. We will inject this as a service, exposing it to the entire web application. We will also use Hibernate for this. This will further simplify our DAO approach. If you don't know or forgot what is a design pattern or even what is a DAO, educate yourself.

We already have all the pieces - we got the database, our webapp and our IDE - all  we need is to use these pieces and work our DAO.

We begin by creating our hibernate.cfg.xml file. This file will connect us to our database where we created earlier at the beginning of this tutorial series. Where we put the file is defined by the file layout of our Tapestry5 web application. Now...

  1. Go to the Other Sources node of our project browser and locate the default package,
  2. Right-click select New and then Other. 
  3. Look for the Hibernate folder and then select Hibernate Configuration Wizard.
  4. Just accept the defaults of the first two pages until you reach the third page of the wizard where you select the data source. Select New Database Connection and supply the details more or less like the screenshot.
You should be now look at a hibernate.cfg.xml file. The next part is where we add some optional bits to our hibernate.cfg.xml file to help us debug our code later. Trust me, better to do it now than later.We could remove this bits when we deploy or we could simply leave it. Almost all of the debugging code outputs into the console anyway.
  1. Open the hibernate.cfg.xml file if its not open(duh!) and locate Optional Properties 
  2. Open that up and we will add two new properties
  3. The first property is the hibernate.show_sql (should be the first item in the combo box) and set it to true.
  4. The second one is to set hibernate.format_sql to true. This basically just pretty prints our sql statements into the console.
We got our database connection, we should start making our entities. Entities? Well, entities are object equivalents of database tables. Right now, we should be able to create two entities which are address and login which are our two tables in our database.
  1. Go to the Source Package create a new file.
  2. Now, this part will not make sense to you but just work with me. Go to Persistence and select Entity Classes from Database.
  3. Select the correct connection and select both tables.
  4. In the Entity Classes part modify the package name into edu.addressbook.entities. Don't about the project not having any persistence unit and make sure that the Generate named query annotations is check.
  5. In the Mapping Options there is one thing you need to decide on though, its the collection type. The collection type basically defines how you are going to handle rows of records containing data retrieved from the database. Now don't you wish you should have paid more attention to Sir Jay's class on data structures and algorithms class during college. Anyhow, I am selecting List for this tutorial but it doesn't really matter what you choose as long you know how to handle that data structure.
You should be looking at something that looks like the screenshot #2 after finishing the wizard. If you look at the code inside the entities you should notice is basically a POJO with bits of annotations - lots of get and set methods.

With that, we start creating our abstract interfaces for our DAO. Abstract interfaces in DAO are simply Java interfaces and we need two - one for the address entity and another one for the login entity.

Remember, Java interfaces contain "contract" methods that we implement somewhere else by implementing the interface. Here is the AddressDAO interface. There is If you notice the methods are CREATE, RETRIEVE, UPDATE and DELETE hence CRUD applications. You can also change this to synonyms like create = add; retrieveAll = findAll, etc. You get the idea.

 public interface AddressDAO {  
   public void create(Address newAddress);  
   public List<Address> retrieveAll();  
   public void update(Address address);  
   public void delete(Address address);  
 }  

The LoginDAO interface has exactly the same contents. Just change the Address entities into Login entities. And don't forget to import the List interface from the java.util package.

Now stay tune for part 2....