me1111
me1111

Reputation: 1157

Setting up query for specific field of Hibernate entity

I use spring and hibernate. The following situation occured and I don't know if it's really possible to implement. Will appreciate any help.

For example, there is a hibernate entity

    @Entity
public class TestEntity {
    private String field1;
    private String field2;
    private String specField;

    @Column(name = "field1")
    public String getField1() {
        return field1;
    }

    @Column(name = "field2")
    public String getField2() {
        return field2;
    }

    public String getSpecField() {
        return (field1 != null ? field1 : field2);
    }
}

And I need the value for specField to be generated by SQL query and not by java code. Something like this

    @Entity
public class TestEntity {
    private String field1;
    private String field2;
    private String specField;

    @Column(name = "field1")
    public String getField1() {
        return field1;
    }

    @Column(name = "field2")
    public String getField2() {
        return field2;
    }

    @Query(value= "COALESCE(field1, field2)")
    public String getSpecField() {
        return specField;
    }
}

I was told that there should be ability to do so. But didn't find anything that approves this.

it's not actually important what exactly query does. I need that specField will be taken by some query and not by java code. Is it possible?

Thanks for help.

UPDATE Thanks to @premkumar, for advicing to use @Formula So now I have

@Entity
public class TestEntity {
    private String field1;
    private String field2;
    private String specField;

    @Column(name = "field1")
    public String getField1() {
        return field1;
    }

    @Column(name = "field2")
    public String getField2() {
        return field2;
    }

    @Formula("COALESCE(field2, field2)")
    public String getSpecField() {
        return (field1 != null ? field1 : field2);
    }
}

But app fails to start on bean initialization with NullPointerException at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory

I tried also the following:

  1. Put formula above "private String specField" - app started, but hibernate failed on could not find spec_field in database

  2. Put @Formula and @Transient on getter - app started, no errors, but specField is always null. It looks like hibernate totally ignored it

Upvotes: 1

Views: 3207

Answers (3)

prem kumar
prem kumar

Reputation: 5877

If you are using hibernate, try the following:

@Formula("COALESCE(field1, field2)")
public String getSpecField() {
    return specField;
}

Note:- As far as I know there is no alternative in JPA. Beware this will mixup hibernate and jpa annotations.

Upvotes: 1

Rana_S
Rana_S

Reputation: 1550

You can leverage the JPA Callback methods:

  1. Create a EntityListener:

    public class MyEntityListener {
    
        @PrePersist
        @PreUpdate
        public void prePersist(Object object) {
            if(object instanceOf TestEntity ) {
                object.specField  = object.field1 != null ? object.field1 : object.field2
            }
        }
    }
    
  2. Annotate your class with @EntityListener:

    @Entity
    @EntityListeners(MyEntityListener .class)
    public class TestEntity {
        private String field1;
        private String field2;
        private String specField;
    
        //default cons - getters and setters
    }
    

Upvotes: 1

Lucas Oliveira
Lucas Oliveira

Reputation: 3477

Why not:

public String getSpecField() {
   return MoreObjects.firstNonNull(field1,field2);
}

Upvotes: 1

Related Questions