Skip to content

Category Archives: Hibernate/JPA

Understanding OpenJPA InvalidStateException: Detected reentrant flush

Ran into this issue today: <openjpa-1.2.2-r422266:898935 fatal user error> org.apache.openjpa.persistence.InvalidStateException: Detected reentrant flush. Make sure your flush-time instance callback methods or event listeners do not invoke any operations that require the in-progress flush to complete. at org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:1923) at org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:1698) at org.apache.openjpa.kernel.QueryImpl.isInMemory(QueryImpl.java:956) at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:838) at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:779) at org.apache.openjpa.kernel.DelegatingQuery.execute(DelegatingQuery.java:525) This is a tricky exception since the verbose [...]

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) [...]

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 [...]

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; [...]

My Latest Issue with Groovy, Spring, and Hibernate

I started this post about a year ago; I will admit my use of groovy has been a little tumultuous, and that’s prevented me from writing much about it. I’ve started a little webapp project – just something simple at first, to get back into doing the type of work that I enjoy doing (as [...]

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 [...]