S
S
Stepan2021-09-28 15:19:48
Spring
Stepan, 2021-09-28 15:19:48

Is it possible to add a private method to an entity?

Is it possible to add a private method to the entity that calculates the value for the field? In this case, the calculateExpirationDate method, which is called from the constructor:

Like this
@Entity
@Table(name = "registration_tokens")
public class RegistrationToken {
    private static final int TTL_MINUTES = 60 * 24; //24 часа

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "token")
    private String token;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", nullable = false)
    private User user;

    @Column(name = "expiration_date")
    private Date expirationDate;

    public RegistrationToken() {
    }

    public RegistrationToken(User user) {
        this.token = UUID.randomUUID().toString();
        this.user = user;
        this.expirationDate = calculateExpirationDate();
    }

    public RegistrationToken(String token) {
        this.token = token;
    }

    public boolean isExpired() {
        return this.expirationDate.before(new Date());
    }

    public boolean isNotExpired() {
        return this.expirationDate.after(new Date());
    }

    private Date calculateExpirationDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MINUTE, TTL_MINUTES);
        return new Date(calendar.getTime().getTime());
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getToken() {
        return token;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTokenString() {
        return token.toString();
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Date getExpirationDate() {
        return expirationDate;
    }

    public void setExpirationDate(Date expirationDate) {
        this.expirationDate = expirationDate;
    }
}

Or is it more correct to set expiretionDate through setExpirationDate (that is, remove calculateExpirationDate and call the method in the constructor)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2021-09-28
@steff

Or is it more correct to set expiretionDate through setExpirationDate

According to all OOP textbooks, data processing should be where the data lies The
only question is that anemic entities with setters and getters usually reign in Java / Php applications
Answer: this is a good practice, quite semantic and good from all sides (Information Expert from Grasp, the law of demeter, encapsulation), setters and processing the state of an object from the outside ignore all this
. hides your logic and semantics of what is happening)
Read:
https://martinfowler.com/bliki/TellDontAsk.html
https://habr.com/ru/post/500416/ (my article)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question