O
O
Oleg Mikhailov2018-07-25 16:52:44
Java
Oleg Mikhailov, 2018-07-25 16:52:44

Is it possible to add custom field to @Entity class?

In spring data jpa, is it possible to add a field to the Entity class that is not in the database? If so, how to do it correctly, because in this form I get an error (the balanceChange field is not in the database):

@Entity
@Table(name = "profile", schema = "webmaster")
public class Webmaster extends Profile {

  @Id
  @GeneratedValue
  @Column(name = "id")
  protected Integer id;

  @Column(name = "balance")
  Integer balance;

  @Column(name = "hash")
  String hash;

  @Column(name = "currency")
  String currency;

  @Column(name = "status")
  String isActive;

  @Column(name = "title")
  String title;

  @Column(name = "purse")
  String purse;

  private double balanceChange;

  public Webmaster() {
  }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Cheremisin, 2018-07-25
@Olegatorapp

Yes, you can, but of course it will not be saved to the database! Give it the transient
keyword
transient private double balanceChange;

A
Alexey P, 2018-07-25
@lynxp9

If you have Hibernate use this annotation https://docs.jboss.org/hibernate/stable/annotation... . But the field value will not be saved to the database. Such fields are used when you use entities without a DTO layer and want to transfer data calculated on the server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question