• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Simple Hibernate Primitive Array Example

 
Ranch Hand
Posts: 42
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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):
     
    Good heavens! What have you done! Here, try to fix it with this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic