- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一个国家和州表,我已将其与 Spring Data JPA 集成。我创建了一个函数 public Page<CountryDetails> getAllCountryDetails
在我的 CountryServiceImpl 中获取所有国家和相应的州详细信息。该服务运行良好,并为我提供以下输出:
{
"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
}
我的完整代码如下:
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);
}
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));
}
}
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
}
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
}
实际上我希望国家/地区服务返回的信息最少,如下所示
{
"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
}
因此,为了实现这一点,我使用了如下所示的投影
国家投影.java
public interface CountryProjection {
public String getCountryName();
public String getCountryCode();
public List<StateProjection> getStates();
}
StateProjection.java
public interface StateProjection {
public String getStateCode();
}
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);
}
但现在服务正在返回任何状态详细信息,如下所示
{
"content": [
{
"countryName": "USA",
"countryCode": "USA"
}
],
"last": false,
"totalPages": 28,
"totalElements": 326,
"size": 12,
"number": 0,
"sort": null,
"numberOfElements": 12,
"first": true
}
我们怎样才能得到最小的状态细节,如下所示
{
"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
}
谁能帮我解决这个问题
最佳答案
尝试将 JsonIgnore 与您在返回 JSON 中不需要的字段一起使用
@JsonIgnore
private String stateDetails;
关于java - Spring Projections 不返回状态详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50630056/
FogBugz 有没有办法创建一个过滤器,它是: [All] [open] [cases] assigned to [Developer A] with the exception of 1 or m
我是 java hibernate 的新手,我无法理解 Projections.property 和 Projections.groupProperty 之间的区别,两者都给出相同的结果。请解释其中的
让我们再试一次。我发布这个是为了回答 2 个问题 MS Project 2007 是否需要 SharePoint(我希望没有)? 做 你喜欢 MS Project 开发团队 - 它是有用的还是 疼痛?
我们的项目结构如下: sharedlib (lib-project, containing classes, that are useful in many apps) -> main-project
我正在尝试获取有关如何将 MS Project 2010 连接到 MS Project Server 2010 的教程或分步说明。 我已经在我的服务器上安装了 Server 2008 R2(64 位)
Projections.distinct(Projections.count("objectId")) 和 Projections.countDistinct("objectId") 谁能告诉我它们之
可以在项目 “project-a” 中创建一个主题“my-topic-project-a”,这样它就可以公开可见(已完成通过将角色“pub/sub subscriber” 设置为“allUsers”)
我创建了一个简单的 Kafka 消费者,它返回 Flux对象(收到的消息),我正在尝试使用 StepVerifier 对其进行测试. 在我的测试中,我做了这样的事情: Flux flux = cons
我有一个项目,最初是在 Eclipse 中,然后我设法将它转换为 gradle,并且在 Android Studio 中一切正常。 但后来我没有在这个项目上工作一段时间,几个月后当我回到项目时,我很惊
我正在研究 CQRS/ES 架构。我们将多个异步投影并行运行到读取存储中,因为某些投影可能比其他投影慢得多,并且我们希望与写入端保持更多同步以实现更快的投影。 我试图了解有关如何生成读取模型以及这可能
第一次尝试使用 Java 持久性注释创建多对多关系。 场景:Java 类Project 包含子项目,它只是Projects 的List。没有反向(没有 super 项目)成员。所以我认为多对多关系对于
我有现有的 C 代码和现有的 Makefile,我想将其包装到 Eclipse C 项目中(使用 Eclipse 3.4 Ganymede)。代码组织如下: 主目录:/Project/Software
我有一个 Eclipse 项目,不久前我设法在 Android Studio 中工作。它使用 TouchDB 库/项目,我现在想升级到他们最新的产品 couchbase-lite-android,看起
我将项目定义为包含主干、分支、标签子目录的 SVN 目录。 在确定何时将项目拆分为两个或将多个项目合并为一个时,您使用什么标准? - 每个“项目”一个应用程序,具有用于公共(public)源和资源的共
尝试在 Visual Studio 2008 中构建解决方案时遇到此错误。这是什么意思? 最佳答案 这可能意味着文件 bin\project.dll被另一个进程(可能是另一个 Visual Studi
此宏将隐藏/关闭 VBE 主窗口: Sub VBEMainWindowHide() 'close VBE window: Application.VBE.Window.Visible
我正在寻求开发户外应用程序,但不确定 tango 平板电脑是否可以在户外使用。那里的其他深度设备往往在室外效果不佳,因为它们依赖于从设备转换的红外光,然后在它从场景中的物体反射回来后进行观察。我一直在
在标准 .csproj您可以进入属性并设置默认命名空间。如何在 .xproj 中实现这一点项目使用 project.json ? 最佳答案 使用 ASP.NET Core 1.0.1,您可以在 pro
当 Redmine 上注册的项目超过 5 个时,主页“最新项目”框中列出的项目按创建日期降序排序(最近创建的优先),将旧项目(可能更新频率更高)排除在列表之外. 有没有办法按事件从最高到最低列出前 5
我开始学习android开发,但我不知道如何将库添加到项目中。我使用安卓工作室。我创建了新项目,但项目结构中没有项目设置。 最佳答案 在“项目”窗口中右键单击您的包名称,然后选择“打开模块设置”。这应
我是一名优秀的程序员,十分优秀!