If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!
Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts

Sunday, September 26, 2010

Hibernate and Spring - Part 6

Everybody who uses hibernate in a project is bound to run into the famous LazyInitializationException problem. By default, collections are loaded lazy. That's in most cases also what you want. E.g.: If you query Facebook for users with firstname 'John', you don't want to fetch all available data about those users in 1 go. You only want to get their complete names. If you click on the name, you want more detailed information.

You should definitely read Hibernate Fetching Strategies

Below default behaviour (fetch=FetchType.LAZY) which can be omitted.


One way to make sure you never run into lazyinitalization exceptions is to fetch eagerly. But be aware for the penalty on performance and memory consumption. You might be fetching big parts of your database if you use this strategy.


One other way to solve this issue is to add following snippet to the web.xml

Hibernate and Spring - Part 5

So let's take a look at one of our domain object's DAO interface and implementation:
  • JobDao
  • JobDaoImpl






As you can see the only thing we still had to implement was the method getEntityClass(). I also added a method to find a job based on its workflowname and itemname and used my convenience methods.

It calls the findListByProperties on the DaoImpl which dynamically creates a HQL string and calls the findByNamedParam(String query, String[] parameters, Object[] values) on our hibernate template.

The querystring we end up with looks like

Friday, September 24, 2010

Hibernate and Spring - Part 4

Ok.. So now that we do have our Domain Objects annotated and ready to be used, we need to write our Service or DAO layer which provides following methods for our domain objects:
  • find/query
  • delete
  • insertOrUpdate
So we already have defined our SessionFactory and HibernateTemplate. Time to make good use of it. First we define a generic interface and next a generic implementation.

One remark: Many methods are just one-1-one mapped to the typical hibernate template methods but I added some extra convenience methods like
  • findUniqueByProperty(String propertyName, Object value)
  • findUniqueByProperties(String[] propertyNames, Object[]values)
  • findListByProperty(String propertyName, Object value)
  • findListByProperties(String[] propertyNames, Object[]values)





Hibernate and Spring - Part 3

Now let's take a look at 2 of my DomainObject's: Job and JobEvent.
The relationships between both domainobjects are simple:
  • A job has 1 to many JobEvents
  • A JobEvent belongs to 1 Job.





Hibernate and Spring - Part 2

I think it's always good practice to extend some generic superclass for your Domain Objects. Even if they don't inherit anything at first, you might decide otherwise later on and it becomes very easy to add common methods and variables. For my use case I decided that every Domain Object had to be uniquely identifiable.

Hibernate and Spring - Part 1

I have an application which locally runs against a mysql database but for QA and PROD we're using Oracle 10G. It's quite easy to setup a generic application context by injecting the correct property values. I'm using Cocoon-Spring-Configurator do load the correct properties once the application Context is initialized.

Below my configuration:



Some example property values for Mysql:


Some example property values for Oracle:

Tuesday, September 21, 2010

JSF form reset problem

Suppose you have a search form with 2 required fields. If you want to reset the form and the values on the controller (@scope=session), you will find that JSF will not let you submit the form as the validation phase will already raise an exception. And let's also assume you want to do some business logic validation before proceeding with the actual action invocation. The code below shows you a quick way to get the job done. The solution is to NOT put the @required=true in your jsf page but accomplish the same behaviour through your own validation method.