【发布时间】:2019-02-07 06:00:38
【问题描述】:
我有 Country 和 Language 具有一对多关系的实体类。
以下是实体类:
@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 响应中。但响应包含空语言。
以下是休眠日志:
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": [],
}
请指导我做错了什么?我正在运行打印在日志中的查询,它们给了我预期的结果,但响应中的语言列表为空。
编辑:
早些时候,我在数据库中每个国家/地区都有两种语言。我删除了一种语言,并且对一种语言的响应很好。但在多种语言的情况下,响应的语言是空的。
【问题讨论】:
标签: hibernate rest spring-boot one-to-many