在Spring Boot RESTful响应中获取OneToMany数据的问题

时间:2018-09-01 13:07:25

标签: hibernate rest spring-boot one-to-many

我有CountryLanguage实体类,它们之间存在一对多的关系。

下面是实体类:

@Entity
@Table(name = "COUNTRY")
public class Country {

    @Id
    @GeneratedValue
    @Column(name = "COUNTRY_ID")
    private Long id;

    @Column(name = "COUNTRY_NAME")
    private String name;

    @Column(name = "COUNTRY_CODE")
    private String code;

    @OneToMany(mappedBy = "country")
    List<Language> languages;
    // getters and setters
}

Language

@Entity
@Table(name = "LANGUAGE")
public class Language {
    @Id
    @GeneratedValue
    @Column(name = "LANGUAGE_ID")
    private Long id;

    @Column(name = "LANGUAGE_NAME")
    private String name;

    @ManyToOne
    @JoinColumn(name = "COUNTRY_ID")
    @JsonIgnore
    private Country country;
    //getters and setters
}

我正在使用JpaRepository进行CRUD操作。这是我的存储库界面:

@Repository
public interface ICountryRepository extends JpaRepository<Country, Long>{
    // http://localhost:8081/country/search/findByName?name=India
    // List<Country> findByName(@Param("name") String role);
}

最后是我的Rest Controller:

@RestController
@RequestMapping("/countries")
public class CountryRestController {

    private final ICountryRepository iCountryRepository;

    @Autowired
    public CountryRestController(ICountryRepository iCountryRepository) {
        this.iCountryRepository = iCountryRepository;
    }

    @GetMapping("/country/{id}")
    public ResponseEntity<Country> retrieveCountryById(@PathVariable Long id) {
    Optional<Country> country = iCountryRepository.findById(id);
    if (!country.isPresent()) {
        return ResponseEntity.noContent().build();
    }
    return ResponseEntity.ok(country.get());
}
}

我正在尝试通过ID获取国家/地区详细信息,并期望语言数据也将填充到JSON响应中。但是响应中包含空的语言。

以下是Hibernate日志:

2018-09-01 05:40:07.863 DEBUG 3612 --- [nio-8081-exec-1] org.hibernate.SQL                        : select country0_.country_id as country_1_0_0_, country0_.country_code as country_2_0_0_, country0_.country_name as country_3_0_0_ from country country0_ where country0_.country_id=?
2018-09-01 05:40:07.863 TRACE 3612 --- [nio-8081-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [1]
2018-09-01 05:40:07.865 TRACE 3612 --- [nio-8081-exec-1] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([country_2_0_0_] : [VARCHAR]) - [IN ]
2018-09-01 05:40:07.865 TRACE 3612 --- [nio-8081-exec-1] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([country_3_0_0_] : [VARCHAR]) - [India]
2018-09-01 05:40:07.865 TRACE 3612 --- [nio-8081-exec-1] org.hibernate.type.CollectionType        : Created collection wrapper: [com.learning.springboot.model.Country.languages#1]
2018-09-01 05:40:07.875 DEBUG 3612 --- [nio-8081-exec-1] org.hibernate.SQL                        : select languages0_.country_id as country_3_2_0_, languages0_.language_id as language1_2_0_, languages0_.language_id as language1_2_1_, languages0_.country_id as country_3_2_1_, languages0_.language_name as language2_2_1_ from language languages0_ where languages0_.country_id=?

以下是我得到的回复:

{
"id": 1,
"name": "India",
"code": "IN ",
"languages": [],
}

请指导我我做错了什么?我正在运行打印在日志中的查询,它们给了我预期的结果,但是响应的语言列表为空。

编辑:

以前,我在数据库中每个国家/地区使用两种语言。我删除了一种语言,并且对一种语言的反应很好。但是在使用多种语言的情况下,响应的语言将变成空白。

1 个答案:

答案 0 :(得分:0)

@OneToMany关系默认情况下是惰性的,因此可以将其设置为Eager,如下所示:

@Entity
@Table(name = "COUNTRY")
public class Country {

     // other fields

    @OneToMany(fetch = FetchType.Eager ,mappedBy = "country")
    List<Language> languages;
    // getters and setters
}