java, android, ios and software development blog

Menu

Tag Archives: hibernate

Let’s say you are working on an application where users are constantly adding/changing new content. You will most likely have to keep a record what’s going on. For example you want to make a wiki-like software, it is crucial to keep a log who added/updated/deleted wiki entries.

If you need that functionality, and you are using Hibernate, then you are one happy coder! Enter ENVERS! As the good people at jboss site say: Easy Entity Auditing.

Here is what you need to do make your entities auditable:

Download latest hibernate with envers bundled (I’m using 3.6.0.Final) and add it to your classpath

Add envers event listeners to your session factory (I’m using spring in my project, so here is a part of my spring.xml):

Annotate your class with @Audited. If you don’t want to keep track of all properties, just annotate the fields you need with the same @Audited.

And that is it :)

After completing all the three steps you will see new table called revinfo (that keeps all the revisions information, you can change it’s name, prefix/suffix, check out the docs) and <entity>_aud (log for the specified entity).

I had a problem that <entity>_aud didn’t have all the required fields, even with hibernate.hbm2ddl.auto set to update. Adam Warski (great guy that create Envers, thanks man!) gave somebody a great tip in jboss forum, that helped me. I’ve just added @Audited for my getters and everything worked smoothly.

One thing I needed more is to keep track WHO made the changes. Here is a nice tutorial how to do that. To keep it short:

At work we are using hibernate with annotations. So in some places we use the @Embeddable & @Embedded annotation. What do they do? They are both standard annotations, @Embeddable means “this class will not have it’s own table, and it will be embedded in some other’s class table”, and @Embedded means “embed this field to my table”.

That’s all good, but I’ve returned to my old project (klopaj.com), and what do we have there? hbm.xml files. I confess, I’ve first started using hibernate annotations, and then hbm files, so I’m not a hbm guru (nor annotations, but I know a bit more :)). So, to do the same thing with hbm.xml:

Simple as that, now my Poi table has two additional columns “latitude” and “longitude”, but my Poi class has a reference to GeoPoint instance. :)
But still, I like the annotation solutions prettier, but this one is working as planned.

I’ve very pleased to announce something that I want to do in a very long time. I’ve heard a million times “this framework is much better than <insert name> framework”, “it’s cool, but to difficult to setup” ,”too many XMLs” etc.

So what will I do? Simple, try to test all the frameworks as much as I can on a very simple application. The mystery application name is “Toink”, so there’s the name “Toink Session”! And yeah, the name doesn’t mean anything, it’s windows default sound when something goes wrong :)

Every framework will use the same backend (core) that has been implemented in Hibernate + Guice combination. And I’ll with test to see how simple it is to setup some framework, use a “legacy” code, and try to use the old POJO objects. Maybe I will do some performance tests as well, because all the frameworks will you the same core.

All the code, examples, bugs and wiki will be @ code.google.com/p/toink. I still don’t know how often will I post here, but as soon as some framework is done, I’ll post it here.

I guess everybody read about Open Session In View. So, as the jboss guys said there, we need something like SessionRequest filter. No problem with that. I’ve used almost the same code in my application, but I had issues with it.

What happend? Well, sometimes after saving something wicket would block, wait a minute and throw a “After 1 minute the Pagemap null is still locked by:” exception. I’ve read alot about that, but couldn’t find any solution!

Luckly, I found one mailing list, where somebody said that SessionRequest should be defined before WicketFilter (I’ve lost the link, but when I find it, I’ll post it). Here is how my web.xml looks like now and it worked!