JPA merge operation is used to reconnect and synchronize detached entities with active PersistenceContext so that changes performed on them could be reflected onto the database. Due to its complex nature, there might be times when multiple representations of an entity appears in the merge process. Hibernate throws IllegalStateException in that case, because allowing each […]
Tag: hibernate
Hibernate Events and Custom EventListener Registration
Since Hibernate 3, each and every operation through Session API fires one or more persistence events during the course of its execution. For example, when you invoke session.get(Foo.class,1L), in order to fetch Foo entity with PK 1, Hibernate fires events with pre-load, load and post-load event types. There is a seperate EventListener interface for each […]
How to Customize Dirty Check Mechanism of Hibernate During Flush
Hibernate needs to identify which entities in the Session has been changed, in other words become dirty in the meantime, so that it can issue an update sql statement to reflect those changes into the database. The default mechanism to identify dirty entities is to compare each attribute one by one with the snapshot kept […]
Is It Possible To Change Entity Fetch Strategy During Hibernate Merge?
When I talk with my colleague about my experience with Hibernate merge behavior on lazy associations with cascade type merge, he suggested me to check if Hibernate allows us to change fetch logic it applies on detached entities during merge operation. That way, it would be possible to suppress eager initialization of those associations during merge. Hibernate […]
More about Eager Initialization of Lazy Associations During Hibernate Merge
After reading my blog post about eager initialization of lazy 1:1 or M:1 associations because of the cascade merge attribute, someone asked about if it applies for 1:M and M:N associations as well? The answer is, yes it applies. Let’s create another small entity in order to illustrate that case as well. @Entity @Table(name=”T_BAZ”) public […]
Hibernate Merge may Cause EAGER Initialization of LAZY OneToOne Associations
Let’s have following two simple entities, having 1:1 lazy association between each other. @Entity @Table(name=”T_FOO”) public class Foo { @Id @GeneratedValue private Long id; @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE) @JoinColumn(name = “bar_id”) private Bar bar; public Long getId() { return id; } public Bar getBar() { return bar; } public void setBar(Bar bar) { […]
When It is Useful to Make Use of JPA @MapKey?
As you probably know, JPA provides way to map collection associations using java.util.Map. However, usage scenarios for such mappings are very limited; but when it comes, they become highly invaluable to easily extract necessary information from your domain model. They are especially useful in order to categorize entities in your associated collection based on some […]
How to populate your DB with sample data during Hibernate bootstrap?
One of the undocumented features of Hibernate is its execution of SQL scripts given within a special file during bootstrap process. It is a very useful feature in order to populate your DB with sample data during testing or development mode. If you create a file named import.sql under project’s root classpath, and put […]
Lookup Tables with Hibernate and JSF
Every project has several tables which contains data mainly in a form of code,name pair. Those tables define state, type or other similar values as codes, which will be referenced from other tables in database. Further, their name values are used in GUI to make users know what those code values really mean in real […]
Extending XDoclet Hibernate Module
As you may know, bag collection type of Hibernate contains its elements unordered and unindexed as well as multiple copies of one element instance in it. Java Collection Framework does not have a bag concept already, so we utilize available List type to implement bag semantics in Java, although List keeps its elements ordered. From […]