3

Is there a way to set some (default) value if the entity not found (only if the entity not found - EntityNotFound exception)? If there is a null value in DB, the field must be null. For example, I have an entity First with relationship to an entity Second:

class First {
   ...
   @ManyToOne @JoinColumn(name="second", nullable=true)
   @NotFound(action = NotFoundAction.IGNORE)
   Second second;
   ...
}

if column "second" in DB (Table "First") is null, then first.second must be null. If column "second" in DB (Table "First") is 5 (second id = 5) and there isn't exist row in table "Second" with id == 5 then firts.second should be some default value (entity), for example entity Second with id = 1 or new Second(params);

AVANG
  • 71
  • 2
  • 9

2 Answers2

1

I hope you access your fields by pair of get/set methods. Just make null-checking logic inside getter:

public Second getRelated(){
    if( second == null )
        return defaultValue;
}

Please also see this answer https://stackoverflow.com/a/757330/149818

Community
  • 1
  • 1
Dewfy
  • 23,277
  • 13
  • 73
  • 121
  • 1
    Yes, I use get/set methods. But I need defaultValue only if EntityNotFound exception occurs. If the field "second" is null and there is no exception then the filed «second» should be null (not defaultValue) – AVANG Sep 07 '15 at 12:43
  • When you use relations you get empty array instead of EntityNotFound - JPA doesn't raise exception to resolve related. So use empty size checking inside get method – Dewfy Sep 07 '15 at 12:56
  • I get EntityNotFound when some table contains a link to other table which does not contain the required data – AVANG Sep 07 '15 at 14:40
  • Please see these 2 answers http://stackoverflow.com/questions/15511899/cannot-make-manytoone-relationship-nullable – Dewfy Sep 07 '15 at 15:20
1
class First {
   ...
   @ManyToOne @JoinColumn(name="second", nullable=true)
   @NotFound(action = NotFoundAction.IGNORE)
   Second second;

   @Column(name = "second", insertable = false, updatable = false)
   private Long secondId;

   public Second getSecond(){
      if (second == null && secondId != null) {
        return defaultSecond;
      }
      return second;
   }
}

If you want to change the second field itself to the default value, then you could add the @PostLoad callback:

public Second getSecond(){
   return second;
}

@PostLoad
private void postLoad() {
   if (second == null && secondId != null) {
      this.second = defaultSecond;
   }
}
Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110
  • That's almost what I need. Just how to initialize field "second" by another Second object (width different id)? – AVANG Sep 07 '15 at 14:04
  • Please, see my edited answer. You may find `@PostLoad` callback useful for this. – Dragan Bozanovic Sep 07 '15 at 14:17
  • This approach solves the question! I can set a new value using @PostLoad method and AutowireHelper.autowire (this, someService); For example: – AVANG Sep 07 '15 at 16:01
  • This approach solves the question! I can set a new value using @PostLoad method and AutowireHelper.autowire (this, this.secondService); to autowire the service field in the class. But then hibernate will update this field in the database. I used annotation @org.hibernate.annotations.Entity(dynamicUpdate = true) but hibernate thinks that the field has changed. Is there a way to set value to the field that the hibernate not updated it in a database? – AVANG Sep 07 '15 at 16:09
  • In that case, just don't set the field value at all and use the getter to get the value as explained in the first part of the answer. – Dragan Bozanovic Sep 07 '15 at 16:22
  • I need to save old value of the field in DB. I use default value to show user, that he hasn't access to the "old value (old "second" entity )". – AVANG Sep 07 '15 at 16:22
  • Ok. I think that's all I need. Thanks! – AVANG Sep 07 '15 at 16:24