Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

java - How to add custom field in model with Spring boot projection?

Now I have two classes

class A{
    @Column(name="num1")
    private BigDecimal num1;
    
    private B b;
    
    //getter and setter
}

And

class B{
    @Column(name="num2")
    private BigDecimal num2;
    
    //getter and setter
}

I have tried to add a new field called total in class A and add the getter in AView.

class A{
    @Column(name="num1")
    private BigDecimal num1;
    
    // a custom field that does not exist in db
    private BigDecimal total;
    
    //suppose we have joined two entity using @join annotation
    private B b;
    
    public getTotal(){
      return this.num1.add(b.num2);
     }
    
    // other getter and setter

}


public interface AView{

    // add num1 and num2
    BigDecimal total();
}

But it showed error message.

I have found a solution but it does not work. https://www.baeldung.com/spring-data-jpa-projections#2-open-projections Are there any other solutions? Thank you!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...