The following error occurs in @NamedQuery if the annotations are put on property(getter) not on the field(member variable).
ERROR org.hibernate.internal.SessionFactoryImpl - HHH000177: Error in named query: myNamedQuery org.hibernate.QueryException: could not resolve property: xxx of: xxxx [from EntityA a where a.entityB.someId = :someId ] at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83) at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:77) at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1968) at org.hibernate.hql.internal.ast.tree.FromElementType.getPropertyType(FromElementType.java:313) at org.hibernate.hql.internal.ast.tree.FromElement.getPropertyType(FromElement.java:490) at org.hibernate.hql.internal.ast.tree.DotNode.getDataType(DotNode.java:616) at org.hibernate.hql.internal.ast.tree.DotNode.prepareLhs(DotNode.java:267) at org.hibernate.hql.internal.ast.tree.DotNode.resolve(DotNode.java:214) at org.hibernate.hql.internal.ast.tree.DotNode.resolveFirstChild(DotNode.java:178) at org.hibernate.hql.internal.ast.HqlSqlWalker.lookupProperty(HqlSqlWalker.java:578) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.addrExpr(HqlSqlBaseWalker.java:4719) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1260) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4426) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3898) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2076) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:794) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:595) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:250) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138) at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105) at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80) at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168) at org.hibernate.internal.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:1083) at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:520) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1794)
example java source code:
@Entity @Table(name="ENTITY_A") @NamedQuery(name="getEntityBById", query = "from EntityA s where s.entityB.entityBId = :entityBId") public class EntityA implements Serializable{
private Long entityAId;
@SequenceGenerator( allocationSize=1, name="ENTITY_A", sequenceName="ENTITY_A_SEQ" ) @GeneratedValue( strategy=GenerationType.SEQUENCE, generator="ENTITY_A") @Id @Column(name="ENTITY_A_ID") public Long getEntityAId() { return this.entityAId; } public void setEntityAId(Long entityAId) { this.entityAId = entityAId; } @ManyToOne @JoinColumn(name="ENTITY_B_ID") private EntityB entityB; public EntityB getEntityB() { return this.entityB; } public void setEntityB(EntityB entityB) { this.entityB = entityB; } } @Entity @Table(name="ENTITY_B") public class EntityB implements Serializable{ @SequenceGenerator( allocationSize=1, name="ENTITY_B", sequenceName="ENTITY_B_SEQ" ) @GeneratedValue( strategy=GenerationType.SEQUENCE, generator="ENTITY_B") @Id @Column(name="ENTITY_B_ID") private Long entityBId; public Long getEntityBId() { return this.entityBId; } public void setEntityBId(Long entityBId) { this.entityBId = entityBId; } @Column(name="ENTITY_NAME") private String entityName; public String getEntityName() { return this.entityName; } public void setEntityName(String entityName) { this.entityName = entityName; } } The error pops up when trying to build the sessionFactory: Configuration configuration = new Configuration().configure(); serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); If I change the code EntityA to the following the error is gone. @Entity @Table(name="ENTITY_A") @NamedQuery(name="getEntityBById", query = "from EntityA s where s.entityB.entityBId = :entityBId") public class EntityA implements Serializable{ @SequenceGenerator( allocationSize=1, name="ENTITY_A", sequenceName="ENTITY_A_SEQ" ) @GeneratedValue( strategy=GenerationType.SEQUENCE, generator="ENTITY_A") @Id @Column(name="ENTITY_A_ID") private Long entityAId; public Long getEntityAId() { return this.entityAId; }
public void setEntityAId(Long entityAId) { this.entityAId = entityAId; }
@ManyToOne @JoinColumn(name="ENTITY_B_ID") private EntityB entityB;
public EntityB getEntityB() { return this.entityB; }
public void setEntityB(EntityB entityB) { this.entityB = entityB; }
}
It appears that there's discrepancy in Hibernate regarding the annotations on filed and property(getter) when interpreting the @NamedQuery.
|