0

We are using OpenJPA 2.3.0. We have DAO entities with Date type properties and with relations to other DAO entities. We retrieve the data from the database, map it to similar DTO objects and pass those across the wire. We also receive the same DTO objects over the wire, map those back to the DAO and persist the data to the DB.

We experienced the problem with Date fields where when the client NULLs the Date field and we persist the change, OpenJPA does not update the field to NULL because it can't be sure that the value was loaded in the first place.

We tried including this persistence.xml setting:

<property name="openjpa.DetachState" value="fetch-groups"/>

Which fixed the Date saving problem, but then caused a problem on other entities. This problem was with the DAO entities that referenced other DAO entities (with a ManyToOne or OneToOne reference etc.). As these referenced entities were not loaded initially, when we persisted the parent entity the child reference would be NULL and therefore the OpenJPA would delete the child reference (cascading persistence was turned on for saving).

To get around this second problem we turned on eager loading for those child entities, but that caused a lot of extra data to be loaded when it wasn't always necessary. We need to turn this off as it causes too high a load on the DB.

I'm curious to know if anyone else has found a more elegant solution to saving NULL values for Date properties on DAO entities that are serialized.

Class for reference:

@Entity
public class Parent implements Serializable {

@OneToMany(cascade = CascadeType.ALL, mappedBy = "child", fetch = FetchType.EAGER)
private List<ChildType> children;

@Temporal(TemporalType.DATE)
private Date endDate;
...
Sean
  • 1,416
  • 19
  • 51
  • Do you have a "not null" constraint on your db column that holds the date ? – sashok_bg Nov 17 '15 at 13:42
  • There's some discussion around which propose alternatives: 1. http://openjpa.208410.n2.nabble.com/Null-field-not-persisted-for-Dates-td5993519.html 2. http://stackoverflow.com/questions/2453671/nullable-date-column-merge-problem 3. https://issues.apache.org/jira/browse/OPENJPA-2559 – Alan Hay Nov 17 '15 at 13:50
  • @AlanHay Those links pretty much drove us down the path that we have followed above. The problem I have is each one is a trade-off, the main one being the eagar loading of a lot of extra data. – Sean Nov 17 '15 at 14:36

1 Answers1

0

Did you try?

jdbc:mysql://HOST:3306/DBNAME?zeroDateTimeBehavior=convertToNull"

Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42
  • That's a nice idea, but the way I understand that it converts dates coming from the DB with value '0000-00-00' to NULLs in Java, but how do I set the Date property on my DAO to a zero date before persisting? – Sean Nov 17 '15 at 15:08
  • I understand your comment, in that it sounds like you want those 0s saved, which you don't even mention in your original post Sean. So why would you down vote this answer when it is a great valid answer to help save nulls in dates. This is an actual issue that has been discussed many time where this answer got over 150 up votes and selected as the actual answer. Saving 0s as dates doesn't make that much sense as such a date doesn't exist. Whereas null makes more sense. Thanks – bytor99999 Apr 04 '16 at 18:11