Skip to content

Category Archives: Hibernate

Migrating from Hibernate 3.3.2, JPA 1 to Hibernate 3.5 CR2 and JPA 2

Starting with version 3.5 (currently CR2), Hibernate Annotations 3.x and Hibernate EntityManager projects have been merged into the Hibernate Core codebase as invidual modules. As a surprising addition, Hibernate Envers subproject is added as well (that’s a great thing as you get Auditing, out of box hereafter). So if you’re stuck with Hibernate 3.3.x and [...]

Mapping a clob type in hibernate and oracle 9i/10g

The easiest way to map a CLOB type of Oracle to a pojo in Hibernate, is to setup a user defined type in Hibernate, which converts Oracle’s ClobType to a String and vice versa. So you’re pojo’s property is going to be a “String” type. There are only two steps to setting this up: 1) [...]

Headless Javascript Unit Testing

I gave a presentation at work a couple of months ago covering JavaScript scoping considerations for a team of Java developers new to JavaScript. During the presentation, I used Rhino to run the examples. I have seen other projects that use Rhino for unit testing, but they seemed a little heavy weight for what I [...]

Understanding org.hibernate.TransientObjectException: object references an unsaved transient instance – save the transient instance before flushing:

Debugging Hibernate exceptions for users not so familiar with Hibernate can be frustrating. The following exception is a common source of error:- Lets try to understand the error with a use case first. The most common scenario when this exception occurs is while trying to save a collection object (but calling save/persist on parent) Consider [...]

Automated Unit Testing with DBUnit, Hsqldb in Spring & Hibernate projects

If your project has a lot of unit testing in Java and you use continuous builds (CruiseControl) to automate, it is often difficult to automate tests which connect to Database (Dao and integration tests). For most companies, having a dedicated Oracle schema for testing is expensive. Also, sometimes, developers need their own instances of schemas [...]

Extending Hibernate Validator for BigDecimal Range validation

Hibernate Validator is a very precise framework written in the hibernate umbrella of frameworks. Using the inbuilt validators should solve most of your column constraints. However, if you have Decimal types (Float, Double of BigDecimal), there is no inbuilt validator to validate both the precision and scale. Also, I’ve stopped using Doubles and Floats in [...]

GORM Relationships in the Grails Console

I’ve just started scratching the surface with Grails. A good starting point seems to be understanding GORM or Grails Object Relational Mapping, which is based on Hibernate3. In looking at the Grails tutorials online, you might be inclined to start using Grails via scaffolding and generating controllers and views. And this is fine, but another [...]

Understanding Composite-id with JPA Identifiers @Id Vs @EmbeddedId Vs @IdClass

Note – If you’re looking for Hibernate Composite-Id example using xml configuration, read our earlier post here. Entities must define an id field. Typically a generated id is fine for most cases, but sometimes we need to add multiple fields corresponding to the database primary key. (legacy database?). The id can either be simple or [...]

Generating a primary key in @ManyToMany with hbm-ddl

Say, you have two entities which have a many to many relationship (Foo and Bar). Using JPA/Hibernate, they are mapped like this, with the owning entity being Foo. public class Foo { …….. @ManyToMany @JoinTable(name="foos_bars", joinColumns = { @JoinColumn(name = "foo_id") }, inverseJoinColumns = { @JoinColumn(name = "bar_id") }) public List<bar> getBars() { return bars; [...]

Hibernate HSQLDB Sequences for Unit Testing

Sequences in HSQLDB are a little odd. The syntax isn’t unusual: CREATE SEQUENCE <sequencename> [AS {INTEGER | BIGINT}] [START WITH <startvalue>] [INCREMENT BY <incrementvalue>]; But what happens is a little unexpected. HSQLDB generates a SQL select statement to retrieve the nextval for the sequence. For example, something like this: CREATE SEQUENCE MY_TABLE_SEQ AS INTEGER START [...]