JPA查询汇总函数与两个表
我有两个实体:州长和州与许多人协会。我需要将所有州的所有州都带到/是州长的位置并显示他们的计数。我正在使用PostgreSQL。看起来像这样:
SELECT official_state_name, COUNT (governor.id_governor) from state
RIGHT JOIN governor ON state.id_state = governor.id_state
GROUP BY official_state_name
但是,我不知道在春季数据JPA中应该如何完成。我知道我需要创建其他类,例如ContorsScount并声明这两个字段。但是这些是两个不同表的字段,也是一个。因此,这是我在JPA中的查询:
@Transactional
@Query(value="SELECT official_state_name, COUNT (governor.id_governor) from state\n" +
"RIGHT JOIN governor ON state.id_state = governor.id_state\n" +
"GROUP BY official_state_name", nativeQuery = true)
List <State> findAllStatesAndGovernors();
这是两个POJO类:
@Entity
@Table(name = "state")
public class State implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_state")
private Integer idState;
@Column(name = "official_state_name")
private String officialStateName;
@Column(name = "official_language")
private String officialLanguage;
@Column(name = "state_currency")
private String stateCurrency;
//setters and getters
@Entity
@Table(name = "governor")
public class Governor implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_governor")
private Integer idGovernor;
@Column(name = "pib")
private String fullName;
@Column(name = "age")
private Integer age;
@Column(name = "position")
private String position;
@Column(name = "date_of_intercession")
private java.sql.Date dateOfIntercession;
@Column(name = "date_of_resignation")
private java.sql.Date dateOfResignation;
@Column(name = "per_capita_income")
private Double perCapitaIncome;
@Column(name = "population_below_poverty")
private Integer populationBelowPoverty;
@ManyToOne
@JoinColumn(name = "id_state", nullable = false)
private State state;
//setters and getters
那么我该如何进行此操作?任何帮助都将受到赞赏。
I have two entities: Governor and State with ManyToOne association. I need to get all states where there are/were governors and display their count. I'm using PostgreSQL. It looks like this:
SELECT official_state_name, COUNT (governor.id_governor) from state
RIGHT JOIN governor ON state.id_state = governor.id_state
GROUP BY official_state_name
However, I have no clue how it should be done in Spring Data JPA. I know that I need to create additional class like GovernorsCount and declare those two fields. But these are the fields from two different tables, nor one. So this is my query in JPA:
@Transactional
@Query(value="SELECT official_state_name, COUNT (governor.id_governor) from state\n" +
"RIGHT JOIN governor ON state.id_state = governor.id_state\n" +
"GROUP BY official_state_name", nativeQuery = true)
List <State> findAllStatesAndGovernors();
These are two POJO classes:
@Entity
@Table(name = "state")
public class State implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_state")
private Integer idState;
@Column(name = "official_state_name")
private String officialStateName;
@Column(name = "official_language")
private String officialLanguage;
@Column(name = "state_currency")
private String stateCurrency;
//setters and getters
@Entity
@Table(name = "governor")
public class Governor implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_governor")
private Integer idGovernor;
@Column(name = "pib")
private String fullName;
@Column(name = "age")
private Integer age;
@Column(name = "position")
private String position;
@Column(name = "date_of_intercession")
private java.sql.Date dateOfIntercession;
@Column(name = "date_of_resignation")
private java.sql.Date dateOfResignation;
@Column(name = "per_capita_income")
private Double perCapitaIncome;
@Column(name = "population_below_poverty")
private Integer populationBelowPoverty;
@ManyToOne
@JoinColumn(name = "id_state", nullable = false)
private State state;
//setters and getters
So how do I actually do this operation? Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以返回
list&lt; object []&gt;
。您也可以使用jpql查询来进行:
如果您只需要状态名称,则可以将“选择子句”更改为
select state.officialstateName,count(consorr)
。You can return a
List<Object[]>
.You can also do it using JPQL queries:
You can change the select clause to
SELECT state.officialStateName, COUNT(governor)
, if you only need the name of the state.首先,必须以以下方式定义实体
是双向onetomany映射。
为了获得计数,您可以使用JPA使用以下查询。
First, the entities have to be defined in the following way
Above is a bi-directional OneToMany mapping.
For getting the count you can use below query using JPA.