1

I currently have a simple Spring Boot app set up with two entities - AppUser and Settings. One AppUser has many Settings in a List like so -

AppUser -

@Entity
@Table(name = "app_user")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class AppUser implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    @NotNull
    @Column(name = "first_name", nullable = false)
    private String firstName;

    @NotNull
    @Column(name = "last_name", nullable = false)
    private String lastName;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "appUser")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private List<Setting> settings = new ArrayList<>();
    // getters and setters ...

Setting -

@Entity
@Table(name = "setting")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Setting implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    @JsonIgnore
    private Long id;

    @Column(name = "test_setting_1")
    private Boolean testSetting1;

    @Column(name = "test_setting_2")
    private Boolean testSetting2;

    @ManyToOne
    @JsonIgnore
    private AppUser appUser;
    // getters and setters ...

I'm currently using the repository to return a List of settings by using

List<Setting> findByAppUserId(Long id);

Which is being called by the controller -

@GetMapping("/settings")
public List<Setting> getAllSettings() {
    return settingRepository.findByAppUserId(1001L);
}

When I return that via a controller though, I get an array of objects like this -

[
  {
    "testSetting1": true
  },
  {
    "testSetting2": true
  }
]

What I'd like to return to the front end is a single object with all the settings in it, something like this -

{
  "testSetting1": true,
  "testSetting2": true
}

I'm curious what's the best way to do this in Spring?

ssdev
  • 99
  • 6
  • Can you add the controller method too please? The returned result isn't valid because if you're returning a `List`, both "testSetting1" and "testSetting2" properties should be displayed in the JSON object, even if they're not set. They should be initialized with "null" in this case. – user1234SI. Mar 20 '20 at 20:36
  • @SebastianI. Sorry, I don't follow. The returned result that I showed above is what I'm currently getting. It's valid, it's just not in the format that i would like. Nothing should need to be set to null. I added in the controller but, all it's doing is calling the repository function I listed. – ssdev Mar 20 '20 at 20:53
  • Maybe the cache strategy is getting you in trouble. First, try to remove the `@Cache` annotation and see if the actual result is the same with the expected result. And then you can read [this](https://stackoverflow.com/questions/1837651/hibernate-cache-strategy) answer and see why this cache strategy could lead to inconsistent data ( @Pascal Thivent answer) because the result you're expecting should be displayed. – user1234SI. Mar 20 '20 at 21:00
  • It's normal to have a JSON array since you request a List, I don't see where is the problem. Also, since you have `@JsonInclude(JsonInclude.Include.NON_NULL)` check if you don't have null values. – akuma8 Mar 20 '20 at 21:28
  • @SebastianI. Removing Cache strategy does not work. akuma8 - the problem is that I need all settings in a single json object for the front end. The jackson json property is set specifically to avoid null values – ssdev Mar 20 '20 at 21:35
  • @ssdev , @akuma8 wanted to say that maybe "testSetting2" from the first element and "testSetting1" from the second element of the list are not set and `@JsonInclude(JsonInclude.Include.NON_NULL)` will eventually stop displaying them. – user1234SI. Mar 20 '20 at 21:40
  • If you need a single object don’t request a List. – akuma8 Mar 20 '20 at 22:05
  • @akuma8 thanks for the suggestion. Do you have an example? The problem is that there are many settings to one user, so I need to return multiple settings, hence a list. Can you show code for what you think would work? – ssdev Mar 20 '20 at 22:09

0 Answers0