【发布时间】:2011-12-17 15:16:29
【问题描述】:
我在尝试使用 HQL 子查询检索相关实体时遇到问题。我有三个实体:一个Customer 实体、一个Account 实体和一个我称为Relation 的附加实体。最初我在Customer 和Account 之间有一个多对多的关系,但后来我不得不添加这个名为Relation 的新实体,因为我需要在Customer 和Account 之间的关系中添加额外的信息。由于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
我与子查询的特定部分有关,但不确定它下面发生了什么。任何帮助将不胜感激。
【问题讨论】: