Posts

Showing posts with the label hibernate

Hibernate/JPA Identity Generators

Introduction As usually it has been a long time since I have last posted in my blog, and even longer (about half a year) since the last time I wrote about Hibernate but finally I have fond the tome for that. This post is about Hibernate standard compatible (TABLE, SEQUENCE, IDENTITY, and AUTO) identity generators: it explains what the identity generators are and illustrated the different considerations need to be taken when choosing identity generation strategy. Environment Hibernate - 3.5.6-Final PostgreSQL - 8.4

Hibernate 3.5/JPA 2.0 - New Query Expressions

Hi all, As usual it took me a long time to write to my blog again but finally here it is - in this post I continue writing about JPA 2.0 (my previous post is here ), more than that this is the first entry I'm publishing in my new (the old one is here ) blog - Congratulations to me :-). Anyhow in this post I'm trying to go over some of the new functions introduced by JPA 2.0 to the JPA query language. As always my platform is Hibernate but I am using only JPA standard annotation and query syntax. The post includes the following: The INDEX function (and the @OrderColumn annotation) The TYPE expression, and The four types of case expressions My environment Hibernate 3.5.2-Final PostgreSQL 8.3

Hibernate 3.5.0-CR-2/JPA 2.0 - Getting Started

Version 2.0 of the Java Persistency API (JPA), a.k.a JSR-317 , was released a while ago (10-December-2009) and Hibernate's next version (3.5.0) will implement this version of the specification. In this, and the next, blog entries I'm planning to explorer JPA 2.0 new features and Hibernate implementation of those feature. Since this is the first post in the series I'll focus on environment setup and include only few new mapping options: The orphanRemoval option The ElementCollection annotaiotn, and The CollectionTable annotation On my next posts I'll cover some more options. My Environment Hibernate 3.5.0-CR-2 (Please notice that Hibernate 3.5 is still a candidate release so things written in this blog entry might change in the future) Database PostgreSQL 8.3

Hibernate Derived Properties - Performance and Portability

Hi all, This time about derived properties, maybe this is not a commonly used feature, and maybe even a little bit hidden one (I don't think I have ever been asked about it in any of the Hibernate courses I had lectured in and this usually a sign that people are not familiar with that feature) but once you're familiar with that it is a powerful feature - however, as always, there are considerations regarding of how and when to use it. What Is a Derived Property? A derived, or calculated, property is a read only property which its value is calculated at fetch time using SQL expressions. For example a Product class might have a price and a calculated final price which is the price including VAT. The first (not so good) solution might be something like that: @Entity @Table(name="PRODUCTS") public class Product { @Column(name="PRICE") private float price; public float getFinalPrice() { return VAT*price; } } The getFinalPrice() method...

Hibernate - Tuning queries Using Paging, Batch Size, and Fetch Joins

Hi,  Another entry about queries - this time a tuning test case and the relations between simple queries, join fetch queries, paging query results, and batch size. Paging the Query Results Since I have never said anything about paging in this blog I start with a short introduction about paging in EJB3: To support paging the EJB3 Query interface defines the following two methods: setMaxResults - sets the number of maximum rows to retrieve from the database setFirstResult - sets the first row to retrieve

Hibernate - the @Any Annotation

Hi,  The 3.3.1.GA release of Hibernate annotations (released on March) includes a new annotation: the @Any (see  http://opensource.atlassian.com/projects/hibernate/browse/ANN-28 ). This annotation (accomplished with @AnyMetaDef) provides the Hibernate annotation and EntityManager support for the "any" type. The Any Property Type Sometimes we need to map an association property to different types of entities that  don't have  a common ancestor entity - so a plain polymorphic association doesn't do the work. For example let's assume three different applications which manage a media library - the first application manages books borrowing, the second one DVDs, and the third VHSs. The applications have nothing in common. Now we want to develop a new application that manages all three media types and reuses the exiting Book, DVD, and VHS entities. Since Book, DVD, and VHS classes came from different applications they don't have any ancestor entity - the common...

EJB3QL - Queries - Part II

Hi all, since I was so busy it has been a long time, too long, since my last post. Last month I visited our headquarters at San-Diego, and I hoped that on the flight I will write a post, or two, or three (well it takes more than 20 hours to get from Tel-Aviv to San-Diego...). But it was too dense, uncomfortable, and noisy to write anything. Anyhow, I want to write more ( here  is the first part) about EJB3 queries.

Hibernate's hbm2ddl Tool

Hi again, The Hibernate hbm2ddl is a tool allows us to create, update, and validate a database schema using Hibernate mappings configuration. The .hbm files are Hibernate mapping files, but since the schema export/validation is done using the the internal configuration Hibernate creates for the entities mapping, we can use still use hbm2ddl when working with JPA. As usual in here I write about the JPA environment, just remember that the hbm2ddl can also be invoked from command line or using Ant task.

Hibernate Exception - Simultaneously Fetch Multiple Bags

One of my customers has just upgraded to JBoss 4.0.4-GA, the process also required us to upgrade Hibernate products to the following versions: Hibernate core – 3.2.0CR2 Hibernate Entity Manager – 3.2.0CR1 Hibernate annotations- 3.2.0CR1 We fixed some minor changes and improvements and then we bumped into the following exception javax.persistence.PersistenceException: org.hibernate.HibernateException: cannot simultaneously fetch multiple bags at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:217) at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:114) ........ The Exception is thrown by org.hibernate.loader.BasicLoader and it means that when loading an entity Hibernate might has to simultaneously fetch two or more bags.

Using Hibernate Annotations to Map a Collection of Elements

Using Hibernate Annotations to Map a collection of Elements An element is a fully dependent object, its life cycle is fully dependent on its owner (the collection owner). In Hibernate a collection of elements has to be annotated using the  @CollectionOfElements  annotation, currently the  @OneToMany  annotation performs the same task but Hibernate documentation (3.1beta8) deprecates the use of  @OneToMany  for a collection of elements. The documentation explicitly states that it will not be supported in future Hibernate annotations versions. An element can be of any class, a java basic type– like String, or a user class – like Comment. In this blog entry I illustrate the uses a user defined type.