org.hibernate.QueryException:无法解析属性用户名:it.unibo.bd.ucm.model.bankaccounts.entity.BankAccount

时间:2016-06-03 16:29:27

标签: java hibernate hql

我正在尝试选择客户拥有的所有银行账户(可能是私人或公司)。

Customer班级代码:

@Entity
@Table(name = "customers")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
@Getter
@Setter
@NoArgsConstructor
public class Customer implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(unique = true, nullable = false, length = 15)
    @NonNull
    private String username;

    @Column(nullable = false, length = 64)
    @NonNull
    private String password;

    @Column(name = "street_name", unique = true, nullable = false, length = 50)
    @NonNull
    private String streetName;

    @Column(name = "house_number", nullable = false)
    private int houseNumber;

    @ManyToMany(targetEntity = Customer.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinTable(
            name = "ownership_ba",
            joinColumns = {
                @JoinColumn(name = "customer_username", referencedColumnName = "username")})
    @NonNull
    private Set<BankAccount> bankAccounts;

}

BankAccount班级代码:

@Entity
@Table(name = "bank_accounts")
@Getter
@Setter
@NoArgsConstructor
public class BankAccount implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(unique = true, nullable = false)
    private String iban;

    @Column(nullable = false)
    private double balance;

    @ManyToMany(targetEntity = BankAccount.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinTable(
            name = "ownership_ba",
            joinColumns = {
                @JoinColumn(name = "bank_account_iban", referencedColumnName = "iban")})
    @NonNull
    private Set<Customer> customers;

}

和HQL查询:

SELECT b FROM BankAccount b JOIN b.customers c WHERE c.username = :username

这是错误日志:

org.hibernate.QueryException: could not resolve property: username of: it.unibo.bd.ucm.model.bankaccounts.entity.BankAccount [SELECT b FROM it.unibo.bd.ucm.model.bankaccounts.entity.BankAccount b JOIN b.customers c WHERE c.username = 'niklegend2']
    at org.hibernate.QueryException.generateQueryException(QueryException.java:137)
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1796)
    at it.unibo.bd.ucm.controller.ControllerImpl.executeQuery(ControllerImpl.java:235)
    at it.unibo.bd.ucm.controller.ControllerImpl.BAList(ControllerImpl.java:51)
    at it.unibo.bd.ucm.controller.ControllerImpl.main(ControllerImpl.java:262)
Caused by: org.hibernate.QueryException: could not resolve property: username of: it.unibo.bd.ucm.model.bankaccounts.entity.BankAccount
    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:1978)
    at org.hibernate.persister.collection.AbstractCollectionPersister.toType(AbstractCollectionPersister.java:1601)
    at org.hibernate.hql.internal.ast.tree.FromElementType.getPropertyType(FromElementType.java:367)
    at org.hibernate.hql.internal.ast.tree.FromElement.getPropertyType(FromElement.java:500)
    at org.hibernate.hql.internal.ast.tree.DotNode.getDataType(DotNode.java:649)
    at org.hibernate.hql.internal.ast.tree.DotNode.prepareLhs(DotNode.java:272)
    at org.hibernate.hql.internal.ast.tree.DotNode.resolve(DotNode.java:219)
    at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:126)
    at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:121)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:945)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1264)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4639)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:4111)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2096)
    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:278)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
    ... 10 more

为什么Hibernate试图在BankAccount类中查找用户名字段?

更新:我们解决了这个问题,基本上@ManyToMany BankAccount类的targetEntity应该是Customer,而Customer应该是BankAccount

1 个答案:

答案 0 :(得分:0)

使用FetchType.EAGER in Set<Customer>加载实体银行报表时加载客户。

LAZY = fetch when needed
EAGER = fetch immediately
级联后

fetch=FetchType.LAZY,它应如下所示

@ManyToMany(targetEntity = BankAccount.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE},fetch=FetchType.LAZY)
@JoinTable(
        name = "ownership_ba",
        joinColumns = {
            @JoinColumn(name = "bank_account_iban", referencedColumnName = "iban")})
@NonNull
private Set<Customer> customers;