我有一个像
这样的查询小组Select r.release.protected_, r.release.releaseType, r.version FROM
ReleaseCategory r where r.release.lifecycleStatus = :lifecycleStatus and
r.release.releaseType is not null
group by r.release.protected_, r.release.releaseType, r.version`
获得例外是
引起:org.postgresql.util.PSQLException:错误:列 " releasetyp3_.id"必须出现在GROUP BY子句中或用于 聚合函数位置:123
Hibernate输出查询是
select release1_.PROTECTED as col_0_0_,
release1_.RELEASE_TYPE_ID as col_1_0_,
releasecat0_.VERSION_ID as col_2_0_,
releasetyp3_.ID as ID1_11_0_,
version4_.ID as ID1_16_1_,
releasetyp3_.DESCRIPTION as DESCRIPT2_11_0_,
releasetyp3_.NAME as NAME3_11_0_,
version4_.EDITION_ID as EDITION_3_16_1_,
version4_.PLATFORM_ID as PLATFORM4_16_1_,
version4_.VERSION_NAME as VERSION_2_16_1_
from schema1.RELEASE_CATEGORY releasecat0_ cross join schema1.RELEASE release1_
inner join schema1.RELEASE_TYPE releasetyp3_ on release1_.RELEASE_TYPE_ID=releasetyp3_.ID
inner join schema1.VERSION version4_ on releasecat0_.VERSION_ID=version4_.ID
where releasecat0_.RELEASE_ID=release1_.id
and release1_.LIFECYCLE_STATUS=?
and (release1_.RELEASE_TYPE_ID is not null)
group by release1_.PROTECTED , release1_.RELEASE_TYPE_ID , releasecat0_.VERSION_ID
查询形成:
StringBuffer sqlString = new StringBuffer();
sqlString.append("Select r.release.protected_, r.release.releaseType, r.version ");
sqlString.append(" FROM ReleaseCategory r where r.release.lifecycleStatus = :lifecycleStatus ");
sqlString.append(" and r.release.releaseType is not null ");
sqlString.append(" group by r.release.protected_, r.release.releaseType, r.version ");
TypedQuery<Object[]> query = entityManager.createQuery( sqlString.toString(),Object[].class );
em.flush();
List<Object[]> results = null;
query.setParameter("lifecyleStatus", "active");
results = query.getResultList();
我正在使用Postgresql 9.5.x.
请帮我解决这个问题。
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name="RELEASE_CATEGORY")
public class ReleaseCategory implements Serializable {
/**
*
*/
private static final long serialVersionUID = 8745765537011255150L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)`enter code here`
@Column(unique=true, nullable=false)
private long id;
@JoinColumn(name="RELEASE_ID", nullable=false)
private Release release;
@ManyToOne
@JoinColumn(name="PLATFORM_ID", nullable=false)
private Platform platform;
@ManyToOne
@JoinColumn(name="EDITION_ID", nullable=false)
private Edition edition;
@ManyToOne
@JoinColumn(name="VERSION_ID", nullable=false)
private Version version;
public ReleaseCategory() {
}
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public Release getRelease() {
return this.Release;
}
public void setRelease(Release release) {
this.release = release;
}
public Platform getPlatform() {
return platform;
}
public void setPlatform(Platform platform) {
this.platform = platform;
}
public Edition getEdition() {
return edition;
}
public void setEdition(Edition edition) {
this.edition = edition;
}
public Version getVersion() {
return version;
}
public void setVersion(Version version) {
this.version = version;
}
}
import java.io.Serializable;
import javax.persistence.*;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(name="RELEASE")
public class Release implements Serializable {
/**
*
*/
private static final long serialVersionUID = -3030747033983038600L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(unique=true, nullable=false)
private long id;
@Column(name="LIFECYCLE_STATUS", nullable=false, length=255)
private String lifecycleStatus;
@Column(name="OPERATING_SYSTEM", nullable=false, length=255)
private String operatingSystem;
@Column(name="PROTECTED", nullable=false, length=5)
private String protected_;
@Column(name="RELEASE_DATE", nullable=false)
private Timestamp releaseDate;
@Column(name="RELEASE_NAME", unique=true, nullable=false, length=255)
private String releaseName;
@Column(name="SHORT_DESCRIPTION", nullable=false, length=255)
private String shortDescription;
//bi-directional many-to-one association to pFile
@OneToMany(mappedBy="release", fetch=FetchType.EAGER, cascade={CascadeType.PERSIST,
CascadeType.REMOVE,
CascadeType.REFRESH,
CascadeType.MERGE,
CascadeType.DETACH})
private List<PFile> pFiles = new ArrayList<PFile>(20);
//bi-directional many-to-one association to ReleaseType
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="RELEASE_TYPE_ID")
private ReleaseType releaseType;
public Release() {
}
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public String getLifecycleStatus() {
return this.lifecycleStatus;
}
public void setLifecycleStatus(String lifecycleStatus) {
this.lifecycleStatus = lifecycleStatus;
}
public String getOperatingSystem() {
return this.operatingSystem;
}
public void setOperatingSystem(String operatingSystem) {
this.operatingSystem = operatingSystem;
}
public String getProtected_() {
return this.protected_;
}
public void setProtected_(String protected_) {
this.protected_ = protected_;
}
public Timestamp getReleaseDate() {
return this.releaseDate;
}
public void setReleaseDate(Timestamp releaseDate) {
this.releaseDate = releaseDate;
}
public String getReleaseName() {
return this.releaseName;
}
public void setReleaseName(String releaseName) {
this.releaseName = releaseName;
}
public String getShortDescription() {
return this.shortDescription;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
public List<PFile> getPFiles() {
return this.pFiles;
}
public void setPFiles(List<PFile> pFiles) {
this.pFiles = pFiles;
}
public ReleaseType getReleaseType() {
return releaseType;
}
public void setReleaseType(ReleaseType releaseType) {
this.releaseType = releaseType;
}
}
答案 0 :(得分:0)
您通过Hibernate日志收到的错误消息已经足够清楚,问题在于您通过Java构建的查询。您是否尝试在pgAdminIII
上执行相同的查询?如果您运行独立查询,则会收到相同的错误。
您遇到的问题是由于您在GROUP BY
子句中使用的列在SELECT
列列表中不可用。如果您将查询更改为此类问题,那么您应该能够毫无问题地运行查询本身!
SELECT r.release.protected_,
r.release.releaseType,
r.version
FROM ReleaseCategory r
WHERE r.release.lifecycleStatus = :lifecycleStatus
AND r.release.releaseType IS NOT NULL
GROUP BY r.release.protected_, r.release.releaseType, r.version
您似乎没有使用问题中提到的此查询,但您在GROUP BY
子句中添加了两个SELECT
列列表中不存在的列。
您在问题中提供的JAVA
代码段中需要进行更改。
希望这能回答你的问题!
答案 1 :(得分:0)
问题已解决。我更改了像
这样的查询sqlString.append(&#34; FROM ReleaseCategory r where r.elease.lifecycleStatus =:lifecycleStatus&#34;); sqlString.append(&#34;和 r.release.releaseType不为null&#34;); sqlString.append(&#34; group by r.release.protected_,r.release.releaseType.id,r.version.id,r.id &#34);
并且工作正常。
谢谢大家......
答案 2 :(得分:0)
您必须加入releaseType:
SELECT
release.protected_, releaseType, r.version
FROM
ReleaseCategory r
JOIN
r.release release
LEFT JOIN
release.releaseType releaseType
WHERE
release.lifecycleStatus = :lifecycleStatus
AND
releaseType IS NOT NULL
GROUP BY
release.protected_, releaseType, r.version