我有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界面:
@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": [],
}
请指导我,我做错了什么?我正在运行在日志中打印的查询,他们给我预期的结果,但响应有空语言列表。
编辑:
之前我在数据库中每个国家/地区使用两种语言。我删除了一种语言,对一种语言的反应很好。但是在多种语言的情况下,响应中的语言变得空洞。
@OneToMany关系默认为Lazy,因此您可以将其设置为Eager,如下所示:
@Entity
@Table(name = "COUNTRY")
public class Country {
// other fields
@OneToMany(fetch = FetchType.Eager ,mappedBy = "country")
List<Language> languages;
// getters and setters
}