0

I am using SpringDataJpa. Let's say I have an entity called USER (with properties first, last and level). Which annotation should I use on the level property, so it's only populated when querying, but not ignored during saving the object to database? I have tried @Transient, but then the field is ignored when querying...

Mathias Dpunkt
  • 11,594
  • 4
  • 45
  • 70
topcan5
  • 1,511
  • 8
  • 30
  • 54

1 Answers1

1

I would try the following.

First make sure that you use JPA Field Access - so your JPA provider is not relying on getters and setters. If your @Id annotation is set on the field and not the getter you should be fine. See the accepted answer of this question for more details on the JPA access type:

What is the purpose of AccessType.FIELD, AccessType.PROPERTY and @Access

Additionally you do not provide a setter for your level property. So the field can just be read by the caller.

Further more you can express that the field cannot be written using the @Column annotation on the level property.

@Column(name = "LEVEL", updatable = false, insertable = false)
Community
  • 1
  • 1
Mathias Dpunkt
  • 11,594
  • 4
  • 45
  • 70