0

I have a REST API with Spring and @RestController and an entity with a JPA repository. Is there a way to transport data (fields) via REST but not persist the corresponding data to the database?

I tried to use @Transient for the specific field, which works for persistence but not for transport. The data will not be marshalled/unmarshalled and thus omitted during transport.

Any ideas?

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
fireball29
  • 11
  • 6
  • I'm not sure I understand you intention. `@Transient` says to JPA not to persist fields being annotated. But at the same time you will be able to pass its' values from frontend to controllers – Dmitry Senkovich Aug 07 '17 at 14:28
  • I'd like to transport temporary (and dependent) data (which can be regenerated) from client to server without additional objects or entities, but not persist them to the database. – fireball29 Aug 07 '17 at 14:36
  • well, why do you think the `@Transient` data will be lost while mashalling/unmarshalling? or maybe I don't understand you that clear – Dmitry Senkovich Aug 07 '17 at 14:40
  • Because I tried it and the data is null or let's say not set. :-) – fireball29 Aug 07 '17 at 14:50
  • hm, but I believe it's the problem somewhere else. could you paste your controller, frontend js code related if exists, mvc configuration? – Dmitry Senkovich Aug 07 '17 at 14:53
  • What does your class look like where you use `@Transient`? – Brian Aug 07 '17 at 19:32
  • Problem solved! After some very hard debugging sessions I figured out, that there are different voters for visibility to include the fields to JSON, and if there are normal getter/setter present, the field will be included. And the problem was, I fogot to generate the getter/setter and accidently used another method to set the fields. Bad idead! ;-) – fireball29 Aug 09 '17 at 15:09

1 Answers1

0

I'd suggest separate representations. A XxxResource representation used at the Controller level for (de-)serializing via the REST API. And a second XxxData or XxxEntity representation for JPA/persistence.

Good luck!

Jamie Bisotti
  • 2,605
  • 19
  • 23
  • Hmm. Let's do an example. We have persons and and list of clothes. I'd like to transport the information about clothes List to the server, but the person should be persisted "naked". Oh my dear, it's only an example :-) Do you suggest to use two different Person entities? But how to transform without to much boilerplate code? – fireball29 Aug 07 '17 at 14:47
  • I assume your REST API accepts a JSON/XML document representing the Person and their Clothes. There would be a `PersonResource` class, containing a `List` of `ClothesResource` class. The incoming document would be deserialized into this Resource representation. If you just want to persist the "person", sans "clothes", then there would be a `PersonEntity` that did not know anything about clothes. You'd convert the `PersonResource` to the `PersonEntity` and persist it. To reduce boilerplate, look at Project Lombok. – Jamie Bisotti Aug 07 '17 at 14:55
  • Ok, so you suggest to use two different "entities" for person without inheritance or something. That should work of course, but I'd like to avoid duplicate data and thus code. BTW, Project Lombok is doing the magic for copying the data? – fireball29 Aug 07 '17 at 15:10
  • Not two diferent entities, I'd say what Jamie is suggesting is to have a DTO layer, that is a valid pattern, you have your objects for transfer data, and then, at the controller layer, before going to service, you convert them to business / entities objects. http://www.baeldung.com/entity-to-and-from-dto-for-a-java-spring-application , https://stackoverflow.com/questions/36174516/rest-api-dtos-or-not , https://medium.com/@schneidenbach/restful-api-best-practices-and-common-pitfalls-7a83ba3763b5 – Javier Sánchez Aug 07 '17 at 15:48
  • I know the concept of DTOs and used it a lot, but didn't really like the duplication. But I will have a look at the suggested mapping frameworks. Thank you very much! – fireball29 Aug 08 '17 at 06:09