Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7161613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:35:47+00:00 2026-05-28T13:35:47+00:00

I have an entity with default values and a calculated field as follow: public

  • 0

I have an entity with default values and a calculated field as follow:

public class Target{
    @Transient
    public Long       total;

    @Min(0)
    @Column(columnDefinition="default 0")
    public Long       val1 = 0L;
    @Min(0)
    @Column(columnDefinition="default 0")
    public Long       val2 = 0L;

    public Target() {
        this.total = Long.valueOf(0L);
        this.val1 = Long.valueOf(0L);
        this.val2 = Long.valueOf(0L);
    }

    public Long calcTotal() {
        return val1 + val2 ;
    }

    public void setVal1(Long val) {
        this.val1 = checkNotNull(val);
        total = calcTotal();
    }

    public void setVal2(Long val) {
        this.val2 = checkNotNull(val);
        total = calcTotal();
    }
}

However whenever the entity is loaded by JPA, the setters are called and a NullPointerException is thrown in calc.
Is there anyway to default the values before JPA calls the setters?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:35:48+00:00Added an answer on May 28, 2026 at 1:35 pm

    First of all, given your mapping, the JPA engine should not call the setters at all, because you chose field access by placing the annotations on the field.

    Second, there is no total field in the code.

    Third, this field should not exist at all, since it can be computed from two other fields. Just let other classes call calcTotal() to access its value. And rename this method getTotal().

    Oh, and the fields should be private, not public.

    If you really want to store the result for reuse, then compute it lazily, and reset it to null when one of the operands is modified:

    public Long getTotal() {
        if (total == null) {
            total = val1 + val2;
        }
        return total;
    }
    
    public void setVal1(Long val1) {
        this.val1 = val1;
        this.total = null;
    }
    
    public void setVal2(Long val2) {
        this.val2 = val2;
        this.total = null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an entity class that has a property with an underlying db column
I have an entity similar to the below: public class Entity { public List<DateItem>
Say you have an domain entity with business logic for initializing its default values.
I have entity model like this (using EclipseLink and JPA 2.0): @Entity class A
In my business model I have Entity class (IPoint interface ) that has a
I have an entity loaded by Hibernate (via EntityManager ): User u = em.load(User.class,
I have two entity classes annotated in the following way @Entity class A {
I have a Priority class that has a datetime column. This column has a
I have Subscribers table in the DB which cantains DateCreate field that has default
I have a model class with a persisted DateTime field that is only interacted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.