0

I have the following issue with the database in my project, and I have no idea what is causing it:

There is the following class hierarchy:

Class A - has an embedded instance of class B Class B - has 2 embedded instances of class C (I renamed the fields of one of them). Class C - has only simple fields.

I know that when you fetch an object from the db using the persistence manager, some fields can be fetched lazily and so I must access them with getters. I already do that and it works for all other cases. In this case however, when I fetch class A, class B is fetched correctly but class C - both of his instances in class B - are null, and they remain null even if I try to access them while the persistence manager is still open. Also if I add simple fields to B they also seem to turn into nulls. This leads me to believe that those instances are simply not saved.

What's wrong here? I am really lost.

Eddie Dovzhik
  • 297
  • 1
  • 6
  • 14

2 Answers2

0

is it possible that you stored null values? most ORM-tools have no way to differentiate between an embedded-object being null and an embedded-object being non null with only null values as fields. hibernate for example will interpret the embedded object to be null. you can store a dummy field for example to work around this issue or maybe your persistence-provider has some annotation to let you override the default behaviour

cproinger
  • 2,258
  • 1
  • 17
  • 33
0

In @Embedded case, all the data is stored in the same level, hence the column names are derived by field, for example, if your B class has a prop b, it would be named in A like A_B_b (think of company_manager_firstName, it wont matter if your manager is Manager1 or Manager2)

Since you have two embedded instance of same class, the persistence manager is not able to figure out the difference between two C instances. The problem would be same if you use two instances of B in A as well.

The solution is to use @AttributeOverrides annotation on one of the C instance, that will tell JPA how to differentiate between Cs.

-Hope this helps.

skywalker
  • 415
  • 3
  • 9
  • I tried to add dummy fields to class B, and it seems that they are also turned into 0. And I already did @AttributeOverrides – Eddie Dovzhik Apr 28 '13 at 18:42
  • can you please confirm if you are doing attribute override correctly, second answer [here](http://stackoverflow.com/questions/4432748/what-does-attributeoverride-mean) might help, you will have to override it for each field in one of the C properties in B. – skywalker Apr 28 '13 at 18:50