简体   繁体   English

在JSON中序列化时,JHipster实体的用户属性设置为null

[英]JHipster entity have user property set to null when serialized in JSON

I'm not a huge expert of Spring Boot, and a beginner with JHipster 我不是Spring Boot的专家,也不是JHipster的初学者

My entity is : 我的实体是:

@Entity
@Table(name = "meal")
public class Meal implements Serializable {

    private static final long serialVersionUID = 1L;

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


    @ManyToOne(fetch = FetchType.EAGER) // added this to JHipster generation
    @JsonIgnoreProperties("meals")
    private User user;

    // ...
}


I don't think the fetch eager is needed, as it's eager by default I guess. 我认为不需要获取急切请求,因为我想默认情况下它很急切。 I also tried with or without @JsonIgnoreProperties 我也尝试过有无@JsonIgnoreProperties

I didn't touch the GET /api/meals resource 我没有GET /api/meals资源

    @GetMapping("/meals")
    public ResponseEntity<List<Meal>> getAllMeals(Pageable pageable, @RequestParam MultiValueMap<String, String> queryParams, UriComponentsBuilder uriBuilder) {
        Page<Meal> page = mealService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(uriBuilder.queryParams(queryParams), page);
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

nor service: 也不提供服务:

    @Transactional(readOnly = true)
    public Page<Meal> findAll(Pageable pageable) {
        log.debug("Request to get all Meals");
        return mealRepository.findAll(pageable);
    }

And I get this JSON: 我得到这个JSON:

[ {
  "id" : 1,
  "instant" : "2019-06-20T03:59:53Z",
  "user" : null
}, {
  "id" : 2,
  "instant" : "2019-06-20T04:18:47Z",
  "user" : null
}]

Actually the database file meals.csv loaded by liquibase had no user column. 实际上,由meals.csv加载的数据库文件meals.csv没有user列。

I've update the meals.csv file 我已经更新了meals.csv文件

id;instant;text;user_id
1;2019-06-20T03:59:53;My own value;4
2;2019-06-20T04:18:47;And another meal;4

Updated manually the changeset by finding loadData (might not be the best practice !) 通过找到loadData手动更新变更集(可能不是最佳实践!)

<changeSet id="20190620074431-1-data" author="jhipster" context="faker">
        <loadData
                  file="config/liquibase/data/meal.csv"
                  separator=";"
                  tableName="meal">
            <column name="id" type="numeric"/>
            <column name="instant" type="datetime"/>
            <column name="text" type="string"/>
            <column name="user_id" type="numeric"/> <!-- <<< Added this -->
        </loadData>
</changeSet>

then ran ./mvnw clean and relaunched the app. 然后运行./mvnw clean并重新启动该应用程序。

This worked for me and seems logical but I wonder why foreign keys are not randomly generated. 这对我有用,似乎合乎逻辑,但是我想知道为什么不是随机生成外键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM