Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • 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
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

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):
     
    reply
      Bookmark Topic Watch Topic
    • New Topic