【问题标题】:HQL error with subquery for a related entity相关实体的子查询的 HQL 错误
【发布时间】:2011-12-17 15:16:29
【问题描述】:

我在尝试使用 HQL 子查询检索相关实体时遇到问题。我有三个实体:一个Customer 实体、一个Account 实体和一个我称为Relation 的附加实体。最初我在CustomerAccount 之间有一个多对多的关系,但后来我不得不添加这个名为Relation 的新实体,因为我需要在CustomerAccount 之间的关系中添加额外的信息。由于Customer 不再映射到问题中的任何其他类,我将把它排除在外。

  • 地址(仅限感兴趣的部分)

@Entity(name = "地址")
@Table(name = "地址")
公共类地址扩展{
.....
@OneToMany(fetch = FetchType.LAZY, mappedBy = "address",
orphanRemoval = true)
私有集合关系 = new HashSet();
@柱子() 私有字符串数 = null;
.....
}

  • 关系(仅有趣的部分)

@Entity(name = "关系")
@Table(name = "CustomerAccount")
@IdClass(Relation.RelationId.class)
公共类关系 {
....
@Id
@ManyToOne(fetch = FetchType.LAZY)
私人账户 account = null;
@Id
@ManyToOne(fetch = FetchType.LAZY)
私人客户客户=空; ....
}

我想要获得的是以下内容:

  • 给定一个具体的Customer(使用它的Id)和一个具体的Account号码,获取指定客户的Account和它的Relation(如果有的话)。

这是我尝试执行的 hql 查询:

选择账户,(从account.relations内部连接relation.customer customer where customer.id = :id中选择关系)从Account account where account.number = :number中选择

生成的sql如下:

选择 account0_.Id 作为 col_0_0_,
(选择
(relations1_.AccountId,
关系1_.CustomerId)
来自
客户账户关系1_
内连接
客户 customer2_
关于关系 1_.CustomerId=customer2_.UserId
内连接
OlsUser customer2_1_
关于 customer2_.UserId=customer2_1_.Id
在哪里
account0_.Id=relations1_.AccountId
和 customer2_.UserId=?) 作为 col_1_0_,
account0_.Id 为 Id0_,
account0_.number 为 AccountN2_0_,
account0_.Active 为 Active0_,
account0_.Application 作为 Applic4_0_,
account0_.Description as Descript8_0_,
account0_.LastUpdated 为 LastUpda9_0_,
来自
帐号帐号0_
在哪里
account0_.number = ?

我认为hql中的子查询有问题,最后是异常

Caused by: java.lang.NumberFormatException: For input string: "(2, 3)"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Long.parseLong(Long.java:410)
    at java.lang.Long.parseLong(Long.java:468)
    at org.h2.value.Value.convertTo(Value.java:811) 


... 61 more

我与子查询的特定部分有关,但不确定它下面发生了什么。任何帮助将不胜感激。

【问题讨论】:

    标签: hibernate hql


    【解决方案1】:

    首先。地址其实就是账号吧?

    回答您的问题:您的查询比需要的要复杂得多。你只需要

    select account, relation from Relation relation
    inner join relation.account account
    where account.number = :number
    and relation.customer.id = :customerId
    

    【讨论】:

    • 并非如此。在这种情况下,如果客户与帐户没有特定的关系,则该查询不会返回任何内容。有一个与我没有关系的帐户 A,如果我查询帐号和我的客户实体,我想要的至少是获取 Account 对象,即使没有关系,所以该查询的关系端应该为空。我不知道是否有可能我只是不想发出另一个查询,如果可能的话
    • 那么以下应该没问题:select account,relation from Account account left join account.relations relationship left join relationship.customer customer where relation.id is null or customer.id = :customerId
    • 和上面的不太一样。当该客户与该帐户存在关系时,它可以工作,但是当根本没有关系时,它无法返回任何结果。即使有一个给定号码的帐户。
    【解决方案2】:

    最后.. 如前所述,我把它复杂化了。查询比那更简单

    select account, relation from Account account left join account.relations relation with relation.customer.id = :id where account.number = :number

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      • 2019-04-14
      • 2012-04-11
      • 1970-01-01
      相关资源
      最近更新 更多