简体   繁体   English

为Hibernate实体的特定字段设置查询

[英]Setting up query for specific field of Hibernate entity

I use spring and hibernate. 我用spring和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. 我需要通过SQL查询而不是java代码生成specField的值。 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. 我需要specField将由一些查询而不是java代码。 Is it possible? 可能吗?

Thanks for help. 感谢帮助。

UPDATE Thanks to @premkumar, for advicing to use @Formula So now I have 更新感谢@premkumar,建议使用@Formula所以现在我有

@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 但是app无法在org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory上使用NullPointerException启动bean初始化

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 将公式置于“私有字符串specField”之上 - 应用程序已启动,但是hibernate失败则无法在数据库中找到spec_field

  2. Put @Formula and @Transient on getter - app started, no errors, but specField is always null. 把@Formula和@Transient放在getter上 - app启动,没有错误,但是specField总是为null。 It looks like hibernate totally ignored it 看起来hibernate完全忽略了它

Why not: 为什么不:

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

You can leverage the JPA Callback methods: 您可以利用JPA Callback方法:

  1. Create a EntityListener: 创建一个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: 使用@EntityListener为您的类添加注释:

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

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. 注意: - 据我所知,JPA没有其他选择。 Beware this will mixup hibernate and jpa annotations. 请注意这将混淆hibernate和jpa注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM