在这里尝试从Java服务层进行简单的选择是我的模型:
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Table(name="PROF_INFO")
public class ProfileInfo {
@EmbeddedId
private ProfileInfoPK profileInfoPK;
@Column(name="RATING", nullable = true )
private Integer rating;
@Column(name="VIEW_DATE", nullable = false )
private java.util.Date viewDate;
@Column(name="CONTACT_DATE", nullable = true )
private java.util.Date contactDate;
@Column(name="REPORTED_DATE", nullable = true )
private java.util.Date reportedDate;
@Column(name="REPORTED_TYPE", nullable = true )
private Integer reportedType;
@Column(name="REPORTED_COMMENT", nullable = true )
private String reportedComment;
public Integer getRating(){
return this.rating;
}
public void setRating(Integer rating){
this.rating = rating;
}
public java.util.Date getViewDate(){
return this.viewDate;
}
public void setViewDate(java.util.Date viewDate){
this.viewDate = viewDate;
}
public java.util.Date getContactDate(){
return this.contactDate;
}
public void setContactDate(java.util.Date contactDate){
this.contactDate = contactDate;
}
public java.util.Date getReportedDate(){
return this.reportedDate;
}
public void setReportedDate(java.util.Date reportedDate){
this.reportedDate = reportedDate;
}
public Integer getReportedType(){
return this.reportedType;
}
public void setReportedType(Integer reportedType){
this.reportedType = reportedType;
}
public String getReportedComment(){
return this.reportedComment;
}
public void setReportedComment(String reportedComment){
this.reportedComment = reportedComment;
}
@Embeddable
public static class ProfileInfoPK implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Column(name="PST_USER_ID", nullable = false )
private Integer pstUserId;
@Column(name="FILE_NAME", nullable = false )
private String fileName;
public Integer getPstUserId() {
return pstUserId;
}
public void setPstUserId(Integer pstUserId) {
this.pstUserId = pstUserId;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((fileName == null) ? 0 : fileName.hashCode());
result = prime * result
+ ((pstUserId == null) ? 0 : pstUserId.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ProfileInfoPK other = (ProfileInfoPK) obj;
if (fileName == null) {
if (other.fileName != null)
return false;
} else if (!fileName.equals(other.fileName))
return false;
if (pstUserId == null) {
if (other.pstUserId != null)
return false;
} else if (!pstUserId.equals(other.pstUserId))
return false;
return true;
}
}
public ProfileInfoPK getProfileInfoPK() {
return profileInfoPK;
}
public void setProfileInfoPK(ProfileInfoPK profileInfoPK) {
this.profileInfoPK = profileInfoPK;
}
}
这是我的DAOImpl:
public List<ProfileInfo> getByEntityandFileName(String entityId,
String fileName)throws NoSuchProfileInfoException , SystemException{
Query query = null;
List<ProfileInfo> results = null;
try{
query = sessionFactory.
getCurrentSession().
createQuery("from ProfileInfo where PST_USER_ID = :PST_USER_ID"); //AND FILE_NAME IN (:FILE_NAME)");
query.setParameter("PST_USER_ID", entityId);
//query.setParameterList("FILE_NAME", fileName.split("\\s*,\\s*"));
}catch(Exception ex){
throw new SystemException(ex);
}
if (query != null) {
results = query.list();
}else{
throw new SystemException("Query Null");
}
if (null != results && !results.isEmpty()) {
return results;
} else {
throw new NoSuchProfileInfoException("No Results Returned");
}
}
}
db在PST_USER_ID,FILE_NAME上有PK索引
和desc
desc PROF_INFO
Name Null Type
---------------- -------- --------------
PST_USER_ID NOT NULL NUMBER(38)
FILE_NAME NOT NULL VARCHAR2(32)
RATING NUMBER(38)
VIEW_DATE NOT NULL DATE
CONTACT_DATE DATE
REPORTED_DATE DATE
REPORTED_TYPE NUMBER
REPORTED_COMMENT VARCHAR2(1000)
所有看起来都很直接但是当我尝试回复时,我收到以下错误:
org.hibernate.hql.internal.ast.QuerySyntaxException: ProfileInfo is not mapped [from ProfileInfo where PST_USER_ID = :PST_USER_ID]
不确定罪魁祸首是什么,但左边有些东西。对你们这些男人和女孩来说有什么不合适的地方吗?
答案 0 :(得分:2)
您将pur SQL与HQL混合使用。它应该是
createQuery("from ProfileInfo where profileInfoPK.pstUserId = :PST_USER_ID");
检查 profileInfoPK.pstUserId 部分。
答案 1 :(得分:0)
在HQL中,您不能引用表名或其列名。相反,您必须使用映射的类及其属性。
您可以查看以下内容:
您可以查看Hibernate配置文件中的类映射详细信息吗?
你能用参数吗?
测试没有参数 createQuery("from ProfileInfo " );
createQuery("from ProfileInfo pfi where pfi.profileInfoPK.pstUserId = :pstUserID");