• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Hibernate Primitive Array Example  RSS feed

Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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):
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!