当使用连接在 HQL 中执行休眠查询并随后调用query.list
以返回匹配对象的列表时,我最终得到的是实际对象实例(即query.list().get(0).getClass() == Object.getClass()
)的列表,而不是预期对象的实例。
在没有连接的情况下运行查询会正确返回预期类型的对象,并且可以正确地转换和使用它们。
到目前为止,我的搜索还没有发现任何可能导致这种情况的信息。在 hql 中使用连接以确保正确映射对象时,我还需要做些什么吗?
编辑:在下面添加了代码摘录。我不得不更改所有名称并尝试仅提取相关部分(真正的代码与汽车无关)。
工作查询:
from Car car where car.name like :name
非工作查询:
from Car car left join car.occupants occupant where car.name like :name OR (occupant.name like :oname)
汽车实体:
@Entity
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"someId"}),
@UniqueConstraint(columnNames = {"someOtherId"})})
public class Car extends SomeParentEntity
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 64)
private String someId;
@Column(length = 64)
private String name;
// ... Many columns and mappings removed ...
@OneToMany(mappedBy = "car", fetch = FetchType.LAZY)
private List<Occupant> occupants;
// ...
}
居住实体:
@Entity(name = "car.Occupant")
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = { "name" }) })
public class User extends SomeParentEntity
{
@ManyToOne
@JoinColumn(name = "carId", nullable = false)
private Car car;
@Column(length = 64, nullable = false)
private String name;
// ... Many mappings / columns removed ...
}