Ale*_*Man 18 java mysql hibernate spring-data-jpa spring-projections
我有一个Country和State表,我已经将其与Spring Data JPA集成在一起.我public Page<CountryDetails> getAllCountryDetails
在CountryServiceImpl中创建了一个函数,用于获取所有Country和相应的State详细信息.该服务工作正常,给我以下输出:
{
"content": [
{
"id": 123,
"countryName": "USA",
"countryCode": "USA",
"countryDetails": "XXXXXXXX",
"countryZone": "XXXXXXX",
"states": [
{
"id": 23,
"stateName": "Washington DC",
"countryCode": "USA",
"stateCode": "WAS",
"stateDetails": "XXXXX",
"stateZone": "YYYYYY"
},
{
"id": 24,
"stateName": "Some Other States",
"countryCode": "USA",
"stateCode": "SOS",
"stateDetails": "XXXXX",
"stateZone": "YYYYYY"
}
]
}
],
"last": false,
"totalPages": 28,
"totalElements": 326,
"size": 12,
"number": 0,
"sort": null,
"numberOfElements": 12,
"first": true
}
Run Code Online (Sandbox Code Playgroud)
我的完整代码如下:
CountryRepository.java
@Repository
public interface CountryRepository extends JpaRepository<CountryDetails, Integer> {
@Query(value = "SELECT country FROM Country country GROUP BY country.countryId ORDER BY ?#{#pageable}",
countQuery = "SELECT COUNT(*) FROM Country country GROUP BY country.countryId ORDER BY ?#{#pageable}")
public Page<CountryDetails> findAll(Pageable pageRequest);
}
Run Code Online (Sandbox Code Playgroud)
CountryServiceImpl.java
@Service
public class CountryServiceImpl implements CountryService {
@Autowired
private CountryRepository countryRepository;
@Override
public Page<CountryDetails> getAllCountryDetails(final int page, final int size) {
return countryRepository.findAll(new PageRequest(page, size));
}
}
Run Code Online (Sandbox Code Playgroud)
CountryDetails.java
@Entity
@Table(name = "country", uniqueConstraints = @UniqueConstraint(columnNames = "id"))
public class CountryDetails {
@Id
@GeneratedValue
@Column(name = "id", unique = true, nullable = false)
private Integer id;
private String countryName;
private String countryCode;
private String countryDetails;
private String countryZone;
@JsonManagedReference
@OneToMany(fetch = FetchType.LAZY, mappedBy = "countryDetails")
private List<State> states;
// getters / setters omitted
}
Run Code Online (Sandbox Code Playgroud)
State.java
@Entity
@Table(name = "state", uniqueConstraints = @UniqueConstraint(columnNames = "id"))
public class State {
@Id
@GeneratedValue
@Column(name = "id", unique = true, nullable = false)
private Integer id;
private String stateName;
private String countryCode;
private String stateCode;
private String stateDetails;
private String stateZone;
@JsonBackReference
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "countryCode", nullable = false, insertable = false, updatable = false, foreignKey = @javax.persistence.ForeignKey(name="none",value = ConstraintMode.NO_CONSTRAINT))
private CountryDetails countryDetails;
// getters / setters omitted
}
Run Code Online (Sandbox Code Playgroud)
实际上我希望国家服务以最少的信息返回,如下所示
{
"content": [
{
"countryName": "USA",
"countryCode": "USA",
"states": [
{
"stateCode": "WAS"
},
{
"stateCode": "SOS"
}
]
}
],
"last": false,
"totalPages": 28,
"totalElements": 326,
"size": 12,
"number": 0,
"sort": null,
"numberOfElements": 12,
"first": true
}
Run Code Online (Sandbox Code Playgroud)
因此,为了实现这一点,我使用了如下所示的投影
CountryProjection .java
public interface CountryProjection {
public String getCountryName();
public String getCountryCode();
public List<StateProjection> getStates();
}
Run Code Online (Sandbox Code Playgroud)
StateProjection .java
public interface StateProjection {
public String getStateCode();
}
Run Code Online (Sandbox Code Playgroud)
CountryServiceImpl.java
@Repository
public interface CountryRepository extends JpaRepository<CountryDetails, Integer> {
@Query(value = "SELECT country.countryName AS countryName, country.countryCode AS countryCode FROM Country country GROUP BY country.countryId ORDER BY ?#{#pageable}",
countQuery = "SELECT COUNT(*) FROM Country country GROUP BY country.countryId ORDER BY ?#{#pageable}")
public Page<CountryProjection> findAll(Pageable pageRequest);
}
Run Code Online (Sandbox Code Playgroud)
但现在该服务正在返回任何状态细节,如下所示
{
"content": [
{
"countryName": "USA",
"countryCode": "USA"
}
],
"last": false,
"totalPages": 28,
"totalElements": 326,
"size": 12,
"number": 0,
"sort": null,
"numberOfElements": 12,
"first": true
}
Run Code Online (Sandbox Code Playgroud)
我们如何获得最小的状态细节,如下所示
{
"content": [
{
"countryName": "USA",
"countryCode": "USA",
"states": [
{
"stateCode": "WAS"
},
{
"stateCode": "SOS"
}
]
}
],
"last": false,
"totalPages": 28,
"totalElements": 326,
"size": 12,
"number": 0,
"sort": null,
"numberOfElements": 12,
"first": true
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我这个
小智 1
尝试将 JsonIgnore 与您不希望出现在返回 JSON 中的字段一起使用
@JsonIgnore
private String stateDetails;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1008 次 |
最近记录: |