- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这句话在 HQL 中可以正确运行:
字符串查询字符串=
"SELECT gmr.id.expedientId.idEns as idEns,"
+ " gmr.id.expedientId.anyExp as anyExp,"
+ " gmr.id.expedientId.numExp as numExp,"
+ " gmr.id.numOrdre as numOrdre,"
+ " gmr.idRecursRebuig.desRecursRebuig as descripcioRebuig,"
+ " gmr.desAmpliRebuig as observacionsRebuig,"
+ " gmr.tipusRebuig as tipusRebuig"
+ " FROM GirMotiuRebuig gmr"
+ " where gmr.id.expedientId.idEns =:idEns and"
+ " gmr.id.expedientId.anyExp =:anyExp and"
+ " gmr.id.expedientId.numExp =:numExp"
+ " ORDER BY gmr.id.numOrdre";
Query queryObject = getSessionFactory().getCurrentSession()
.createQuery(queryString);
queryObject.setResultTransformer(Transformers
.aliasToBean(MotiusRebuig.class));
queryObject.setParameter("idEns",clauExpedient.getIdEns());
queryObject.setParameter("anyExp",clauExpedient.getAnyExp());
queryObject.setParameter("numExp",clauExpedient.getNumExp());
llistaMotiusRebuig =queryObject.list();
和我的标准版本:
Criteria consulta = this.getSessionFactory().getCurrentSession()
.createCriteria(GirMotiuRebuig.class)
.add(Restrictions.eq("id.expedientId.idEns", clauExpedient.getIdEns()))
.add(Restrictions.eq("id.expedientId.anyExp", clauExpedient.getAnyExp()))
.add(Restrictions.eq("id.expedientId.numExp", clauExpedient.getNumExp()))
.addOrder(Order.asc("id.numOrdre"))
.setProjection(Projections.projectionList()
.add(Projections.property("id.expedientId.idEns"),"idEns")
.add(Projections.property("id.expedientId.anyExp"),"anyExp")
.add(Projections.property("id.expedientId.numExp"),"numExp")
.add(Projections.property("id.numOrdre"),"numOrdre")
.add(Projections.property("idRecursRebuig.desRecursRebuig"),"descripcioRebuig")
.add(Projections.property("desAmpliRebuig"),"observacionsRebuig")
.add(Projections.property("tipusRebuig"),"tipusRebuig")
.add(Projections.property("expedientId.numOrdre"),"numOrdre"))
.setResultTransformer(
Transformers.aliasToBean(MotiusRebuig.class));
llistaMotiusRebuig = consulta.list();
还有异常(exception):
Caused by: org.hibernate.QueryException: could not resolve property: idRecursRebuig.desRecursRebuig of: cat.base.gir.logica.domini.impl.GirMotiuRebuig
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:44)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:38)
at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1362)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getType(CriteriaQueryTranslator.java:477)
at org.hibernate.criterion.PropertyProjection.getTypes(PropertyProjection.java:36)
at org.hibernate.criterion.AliasedProjection.getTypes(AliasedProjection.java:37)
at org.hibernate.criterion.ProjectionList.getTypes(ProjectionList.java:38)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getProjectedTypes(CriteriaQueryTranslator.java:318)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:77)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:59)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:67)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1550)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at cat.base.gir.dao.GirMotiuRebuigDao.obtenirMotiusRebuig(GirMotiuRebuigDao.java:73)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy1183.obtenirMotiusRebuig(Unknown Source)
at cat.base.gir.serveis.GirRecursService.obtenirMotiusRebuig(GirRecursService.java:381)
... 127 more
实体:GirMotiuRebuig
@Entity
@Table(name = "gir_exp_motiu_rebuig")
public class GirMotiuRebuig implements IGirMotiuRebuig {
/**
*
*/
private static final long serialVersionUID = -5587115692447683913L;
private ExpedientIdMultiple id;
private MRecursRebuig idRecursRebuig;
private String desAmpliRebuig;
private GirExpedient expedientGir;
protected Integer tipusRebuig;
public GirMotiuRebuig() {
}
public GirMotiuRebuig(ExpedientIdMultiple id, MRecursRebuig idRecursRebuig, String desAmpliRebuig,
GirExpedient expedientGir) {
this.id = id;
this.idRecursRebuig = idRecursRebuig;
this.desAmpliRebuig = desAmpliRebuig;
this.expedientGir = expedientGir;
}
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = "expedientId.idEns", column = @Column(name = "id_ens", nullable = false, precision = 5, scale = 0)),
@AttributeOverride(name = "expedientId.anyExp", column = @Column(name = "any_exp", nullable = false, precision = 4, scale = 0)),
@AttributeOverride(name = "expedientId.numExp", column = @Column(name = "num_exp", nullable = false, precision = 10, scale = 0)),
@AttributeOverride(name = "numOrdre", column = @Column(name = "num_ordre", nullable = false, precision = 10, scale = 0)) })
public ExpedientIdMultiple getId() {
return id;
}
public void setId(ExpedientIdMultiple id) {
this.id = id;
}
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_recurs_rebuig")
public MRecursRebuig getIdRecursRebuig() {
return idRecursRebuig;
}
public void setIdRecursRebuig(MRecursRebuig idRecursRebuig) {
this.idRecursRebuig = idRecursRebuig;
}
@Column(name = "des_ampli_rebuig", nullable = false, length = 250)
public String getDesAmpliRebuig() {
return desAmpliRebuig;
}
public void setDesAmpliRebuig(String desAmpliRebuig) {
this.desAmpliRebuig = desAmpliRebuig;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns( {
@JoinColumn(name = "id_ens", referencedColumnName = "id_ens", nullable = false, insertable = false, updatable = false),
@JoinColumn(name = "any_exp", referencedColumnName = "any_exp", nullable = false, insertable = false, updatable = false),
@JoinColumn(name = "num_exp", referencedColumnName = "num_exp", nullable = false, insertable = false, updatable = false) })
public GirExpedient getExpedientGir() {
return expedientGir;
}
public void setExpedientGir(GirExpedient expedientGir) {
this.expedientGir = expedientGir;
}
@Column(name = "tipus_rebuig", nullable = false)
public Integer getTipusRebuig() {
return tipusRebuig;
}
public void setTipusRebuig(Integer tipusRebuig) {
this.tipusRebuig = tipusRebuig;
}
嗯,这个标准有效:
Criteria consulta = this.getSessionFactory().getCurrentSession()
.createCriteria(GirMotiuRebuig.class, "g")
.add(Restrictions.eq("g.id.expedientId.idEns", clauExpedient.getIdEns()))
.add(Restrictions.eq("g.id.expedientId.anyExp", clauExpedient.getAnyExp()))
.add(Restrictions.eq("g.id.expedientId.numExp", clauExpedient.getNumExp()))
.addOrder(Order.asc("g.id.numOrdre"))
.setProjection(Projections.projectionList()
.add(Projections.property("g.id.expedientId.idEns"),"idEns")
.add(Projections.property("g.id.expedientId.anyExp"),"anyExp")
.add(Projections.property("g.id.expedientId.numExp"),"numExp")
.add(Projections.property("g.desAmpliRebuig"),"observacionsRebuig")
.add(Projections.property("g.id.numOrdre"),"numOrdre")
.add(Projections.property("g.tipusRebuig"),"tipusRebuig")
)
.setResultTransformer(
Transformers.aliasToBean(MotiusRebuig.class));
llistaMotiusRebuig = consulta.list();
但是当我尝试攻击“idRecursRebuig.desRecursRebuig as descripcioRebuig”时像:
Criteria consulta = this.getSessionFactory().getCurrentSession()
.createCriteria(GirMotiuRebuig.class, "g")
.add(Restrictions.eq("g.id.expedientId.idEns", clauExpedient.getIdEns()))
.add(Restrictions.eq("g.id.expedientId.anyExp", clauExpedient.getAnyExp()))
.add(Restrictions.eq("g.id.expedientId.numExp", clauExpedient.getNumExp()))
.addOrder(Order.asc("g.id.numOrdre"))
.setProjection(Projections.projectionList()
.add(Projections.property("g.id.expedientId.idEns"),"idEns")
.add(Projections.property("g.id.expedientId.anyExp"),"anyExp")
.add(Projections.property("g.id.expedientId.numExp"),"numExp")
.add(Projections.property("g.desAmpliRebuig"),"observacionsRebuig")
.add(Projections.property("g.id.numOrdre"),"numOrdre")
.add(Projections.property("g.tipusRebuig"),"tipusRebuig")
**.add(Projections.property("g.idRecursRebuig.desRecursRebuig"),"descripcioRebuig")**
)
.setResultTransformer(
Transformers.aliasToBean(MotiusRebuig.class));
llistaMotiusRebuig = consulta.list();
查询失败...
nested exception is org.hibernate.QueryException: could not resolve property: idRecursRebuig.desRecursRebuig of: cat.base.gir.logica.domini.impl.GirMotiuRebuig
所以:问题是当我尝试恢复 idRecurs...所以..我需要额外的咖啡因...
这是 MRecursRebuig 的映射:
@Entity
@Table(name = "MRecursRebuig")
public class MRecursRebuig implements IMRecursRebuig {
/**
*
*/
private static final long serialVersionUID = 7439723582297629986L;
private Integer idRecursRebuig;
private String desRecursRebuig;
private Integer tipusRebuig;
public MRecursRebuig() {
}
public MRecursRebuig(Integer idRecursRebuig, String desRecursRebuig) {
this.idRecursRebuig = idRecursRebuig;
this.desRecursRebuig = desRecursRebuig;
}
@Id
@Column(name = "id_recurs_rebuig", unique = true, nullable = false, precision = 22, scale = 0)
public Integer getIdRecursRebuig() {
return idRecursRebuig;
}
public void setIdRecursRebuig(Integer idRecursRebuig) {
this.idRecursRebuig = idRecursRebuig;
}
@Column(name = "des_recurs_rebuig", nullable = false, length = 100)
public String getDesRecursRebuig() {
return desRecursRebuig;
}
public void setDesRecursRebuig(String desRecursRebuig) {
this.desRecursRebuig = desRecursRebuig;
}
@Column(name = "tipus_rebuig", nullable = false)
public Integer getTipusRebuig() {
return tipusRebuig;
}
如果我尝试这个:
Criteria consulta = this.getSessionFactory().getCurrentSession()
.createCriteria(GirMotiuRebuig.class, "g")
.createAlias("g.idRecursRebuig", "idRecursRebuig")
.add(Restrictions.eq("g.id.expedientId.idEns", clauExpedient.getIdEns()))
.add(Restrictions.eq("g.id.expedientId.anyExp", clauExpedient.getAnyExp()))
.add(Restrictions.eq("g.id.expedientId.numExp", clauExpedient.getNumExp()))
.add(Restrictions.eq("g.idRecursRebuig", clauExpedient.getNumExp()))
.addOrder(Order.asc("g.id.numOrdre"))
.setProjection(Projections.projectionList()
.add(Projections.property("g.id.expedientId.idEns"),"idEns")
.add(Projections.property("g.id.expedientId.anyExp"),"anyExp")
.add(Projections.property("g.id.expedientId.numExp"),"numExp")
.add(Projections.property("g.desAmpliRebuig"),"observacionsRebuig")
.add(Projections.property("g.id.numOrdre"),"numOrdre")
.add(Projections.property("g.tipusRebuig"),"tipusRebuig")
**.add(Projections.property("idRecursRebuig.desRecursRebuig"),"descripcioRebuig")**
)
.setResultTransformer(
Transformers.aliasToBean(MotiusRebuig.class));
llistaMotiusRebuig = consulta.list();
我收到:
nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of cat.base.gir.logica.domini.impl.MRecursRebuig.idRecursRebuig
cat.base.gir.serveis.GirRecursService.obtenirMotiusRebuig(GirRecursService.java:387)
最佳答案
在 HQL 查询中,当您有类似的内容时
select entityA.entityB.foo from EntityA entityA ...
您实际上是在 EntityA 和 EntityB 之间创建隐式内部联接。相当于
select entityB.foo from EntityA entityA
inner join entityA.entityB entityB ...
在 Criteria 查询中,您无法像在 HQL 中那样链接属性。相反,每次需要连接时,您都必须使用 subCriteria 或(我的偏好)别名显式创建它。所以你需要做
Criteria consulta = this.getSessionFactory().getCurrentSession()
.createCriteria(GirMotiuRebuig.class, "girMotiuRebuig")
.createAlias("girMotiuRebuig.id.expedientId", "expedient")
.add(Restrictions.eq("expedient.idEns", clauExpedient.getIdEns()))
...
请注意,您的设计和命名使您的代码难以理解。您应该无论如何避免使用复合键,并且您不应该将 idRecursRebuig
命名为实际上是 MRecursRebuig 实例的字段,而根本不是 ID。
关于java - Hql查询转Criteria查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22074729/
我有实体和 NamedQuery: @Entity @Table(name="MY_TABLE") @NamedQueries({ @NamedQuery(name="myQuery", quer
hql 中的 row_number() 与分区的等价物是什么 我在 hql 中有以下查询: select s.Companyname, p.Productname, sum(od.Unitprice
hql 函数 current_timestamp() 使用的是来自运行 java 代码的服务器的时间还是来自运行数据库的服务器的时间? 最佳答案 HQL 查询被翻译成 SQL 查询,SQL 查询由数据
我有一个 Hibernate HQL 问题。 我想将子查询编写为派生表(出于性能原因)。 可以在 HQL 中做到这一点吗? 例子: FROM Customer WHERE country.id in
我有三个表 A B 和 C。现在我想在 HQL 中执行这个 sql 查询: select * from A as a left join B as b on a.id = b.id left join
我是 Hive 的新手,想知道如何直接从 .hql 文件执行 hive 命令。 最佳答案 如 @rajshukla4696 所述,hive -f filename.hql 或 beeline -f f
出于各种原因,我正在尝试编写一个部分动态的 HQL 查询,而不求助于 Criteria API。我想知道是否有一种简单的方法可以使用 HQL 表达式来短路 where 限制。例如,这是工作正常的原始查
我正在自学 hibernate ,并且很困惑为什么我不能只编写简单的 SQL 查询。 我发现它比普通 SQL(我习惯的)使用起来更困惑 PLUS:我发现 NetBeans HQL 编辑器非常烦人,对我
非常感谢您帮助将以下 SQL 语句转换为有效的 HQL 语句。我尝试了几个小时但没有成功: SELECT * FROM master as m left outer join (select * fr
我有一个 HQL 查询: query = select item.itemNumber from items item where item.stock>0 and item.price it = q
我在以下代码段中遇到错误 using (var session = Database.OpenSession()) { var q = from x in session.Query()
我正在使用以下 C# 代码: public IList GetAllByExpression(Expression> expression, int startIndex, int count, Fu
我有 HQL,我试图在其中获取没有分类的工件(当 Activity 为 0 时) artifacts = Artifact.findAll("FROM Artifact WHERE id NOT IN
在 JPQL 中,我可以通过以下方式检索实体: query = entityManager.createQuery("select c from Category c"); List categori
我正在尝试使用 HQL 查询更新记录,但我收到了 CastException。如果有人可以帮助我,我将不胜感激。我已经在网上查了一段时间,但我找不到这方面的任何信息。如果您有关于此异常的更多信息,请告
我正在尝试在 JPA 中执行一个 select 语句,这会抛出上述错误。代码是: TypedQuery typedQuery = null; String query = ""; List list
我有问题要执行一个更新 HQL 查询。 我想编辑保存在配置文件实体中的值,但没有运气。 我的实体如下所示 @Entity @Table(name = "users", catalog = "testd
如何设置此 hql 查询的限制?当我在查询中添加 limit 关键字时,会引发错误。 @Query("from voucher v where v.voucherType.typeDescript
假设有两个表,A[a_id, b_id]和 B[b_id,c] . 我需要执行表单 "From A a ORDER BY a.b.c" 的 HQL 查询, 而 b在类 A 中可以为空. 但是,该查询仅
是否可以在 HQL 中运行与此类似的查询而无需指定所有列名。 select med, MAX(med.PrescriptionLines.Prescription.PrescriptionDate)
我是一名优秀的程序员,十分优秀!