gpt4 book ai didi

java - Hibernate/JPA 新增功能 : java. 表主键上的 lang.NullPointerException 过滤

转载 作者:行者123 更新时间:2023-12-01 18:18:30 28 4
gpt4 key购买 nike

我是 Hibernate/JPA 新手。我编写了一个简单的演示项目来开始了解该技术。我有这个问题:一切正常,但过滤表主键字段上的持久性对象。即使我使用多个过滤器,其他每个字段都可以用于过滤对象,没有任何问题。

这是我编写的简单演示代码:

CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();

CriteriaQuery<BaRoles> criteriaQuery = criteriaBuilder.createQuery(BaRoles.class);

Root<BaRoles> root = criteriaQuery.from(BaRoles.class);

criteriaQuery.select(root).where(criteriaBuilder.equal(root.get("rocode"), 78));
//criteriaQuery.select(root).where(criteriaBuilder.equal(root.get("rodescri"), "xxxxxxxx")); <-- Works fine

Query<BaRoles> query = session.createQuery(criteriaQuery);

List<BaRoles> roles = query.getResultList(); // <-- Returns nothing filtering rocode, correctly returns obejcts when filtering rodescri

for (int roleCounter = 0; roleCounter < roles.size(); roleCounter++) {
BaRoles role = (BaRoles) roles.get(roleCounter);
}

这是完整的堆栈跟踪:

java.lang.NullPointerException 在 org.hibernate.query.sqm.sql.internal.BasicValuedPathInterpretation.from(BasicValuedPathInterpretation.java:50) 在 org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitBasicValuedPath(BaseSqmToSqlAstConverter.java:747) 在 org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitBasicValuedPath(BaseSqmToSqlAstConverter.java:158) 在 org.hibernate.query.sqm.tree.domain.SqmBasicValuedSimplePath.accept(SqmBasicValuedSimplePath.java:87) 在org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitComparisonPredicate(BaseSqmToSqlAstConverter.java:1545) 在org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitComparisonPredicate(BaseSqmToSqlAstConverter.java:158) 在 org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate.accept(SqmComparisonPredicate.java:67) 在 org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitQuerySpec(BaseSqmToSqlAstConverter.java:364) 在 org.hibernate.query.sqm.sql.internal.StandardSqmSelectTranslator.visitSelectStatement(StandardSqmSelectTranslator.java:169) 在 org.hibernate.query.sqm.sql.internal.StandardSqmSelectTranslator.translate(StandardSqmSelectTranslator.java:111) 在 org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.buildCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:224) 在 org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.resolveCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:191) 在 org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.performList(ConcreteSqmSelectQueryPlan.java:153) 在 org.hibernate.query.sqm.internal.QuerySqmImpl.doList(QuerySqmImpl.java:413) 在 org.hibernate.query.spi.AbstractQuery.list(AbstractQuery.java:1356) 在 org.hibernate.query.Query.getResultList(Query.java:136) 在 com.xxxx.xxxxx.mavenproject1.BaRoleManager.readWithFilter(BaRoleManager.java:153) 在 com.xxxx.xxxxx.mavenproject1.BaRoleManager.main(BaRoleManager.java:201)

这是 BaRoles 类(使用 JPA 生成):

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name = "ba_roles")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "BaRoles.findAll", query = "SELECT b FROM BaRoles b"),
@NamedQuery(name = "BaRoles.findByRocode", query = "SELECT b FROM BaRoles b WHERE b.rocode = :rocode"),
@NamedQuery(name = "BaRoles.findByRodescri", query = "SELECT b FROM BaRoles b WHERE b.rodescri = :rodescri"),
@NamedQuery(name = "BaRoles.findByCpccchk", query = "SELECT b FROM BaRoles b WHERE b.cpccchk = :cpccchk"),
@NamedQuery(name = "BaRoles.findByRodescriEng", query = "SELECT b FROM BaRoles b WHERE b.rodescriEng = :rodescriEng")
})
public class BaRoles implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
//@TableGenerator(name = "roleGenerator", table = "cpwarn", allocationSize = 1, pkColumnName = "tablecode", valueColumnName = "autonum", pkColumnValue = "prog\\ba_roles", schema="dbo", initialValue = 0)
@TableGenerator(name = "roleGenerator", table = "cpwarn", allocationSize = 1, pkColumnName = "tablecode", valueColumnName = "autonum", pkColumnValue = "prog\\ba_roles")
@GeneratedValue(strategy = GenerationType.TABLE, generator = "roleGenerator")
@Column(name = "ROCODE", updatable = false, nullable = false)
private Integer rocode;
@Column(name = "RODESCRI")
private String rodescri;
@Column(name = "cpccchk")
private String cpccchk;
@Column(name = "RODESCRI_ENG")
private String rodescriEng;

public BaRoles() {
}

public BaRoles(Integer rocode) {
this.rocode = rocode;
}

public Integer getRocode() {
return rocode;
}

public void setRocode(Integer rocode) {
this.rocode = rocode;
}

public String getRodescri() {
return rodescri;
}

public void setRodescri(String rodescri) {
this.rodescri = rodescri;
}

public String getCpccchk() {
return cpccchk;
}

public void setCpccchk(String cpccchk) {
this.cpccchk = cpccchk;
}

public String getRodescriEng() {
return rodescriEng;
}

public void setRodescriEng(String rodescriEng) {
this.rodescriEng = rodescriEng;
}

@Override
public int hashCode() {
int hash = 0;
hash += (rocode != null ? rocode.hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof BaRoles)) {
return false;
}
BaRoles other = (BaRoles) object;
if ((this.rocode == null && other.rocode != null) || (this.rocode != null && !this.rocode.equals(other.rocode))) {
return false;
}
return true;
}

@Override
public String toString() {
return "com.xxxx.xxxx.mavenproject1.BaRoles[ rocode=" + rocode + " ]";
}

}

最佳答案

解决了!将 Hibernate 版本从 6.0.0 alpha 4 恢复到 5.4.12 Final(Maven 存储库)。一切都按预期进行。感谢大家付出的时间和精力。

关于java - Hibernate/JPA 新增功能 : java. 表主键上的 lang.NullPointerException 过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60322102/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com