This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Blog around the Campfire and the fly likes Simple Hibernate Primitive Array Example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Other » Blog around the Campfire
Bookmark "Simple Hibernate Primitive Array Example" Watch "Simple Hibernate Primitive Array Example" New topic
Author

Simple Hibernate Primitive Array Example

Christopher Rygaard
Ranch Hand

Joined: Feb 22, 2014
Posts: 42
    
    3
Hibernate Array Example

At first glance, this might look like another non-example, but I'm posting it to explain a point.

In my day job, I need to work with arrays of primitive data types, and I needed to find out how Hibernate would handle that. That's why I built this example. At first, it looked like Hibernate handled it just fine, without any annotation at all (you can see that in the example below).

However, after running the example, I investigated what was in the database. It looks like Hibernate is not creating a separate table to hold the values of the array. Instead, Hibernate serializes the array.

This will work fine in some cases, but it could present problems in other cases:
  • You can't query the database to get just the value of a single array element.
  • This will not scale to large arrays. The only way Hibernate can access a single element of the array is to reconstruct the entire array.

  • I'm not yet sure of the best strategy to use if the array needs to be in a separate table (hey, I said I'm still learning Hibernate). One approach might be to use collection semantics, as described here, here, here, and here.

    Listed at the bottom of this posting is a diagram of the database after I run the example.

    This is the POJO with an array:

    This is an example of how to use it:

    This is a diagram of the database (there is just one table):
     
    GeeCON Prague 2014
     
    subject: Simple Hibernate Primitive Array Example