HQL不会返回Object

时间:2017-01-16 14:43:02

标签: hibernate hql

我希望我的HQL返回Object而不是数组,因为我使用了一些连接表,当我编写包含连接表的列名的查询时,HQL提供数组列表而不是对象。我发现我们应该使用new来使它成为对象。

SELECT new Accommodation(a.name, a.rank, ap.pic) FROM Accommodation AS a INNER JOIN a.accommodationPics AS ap WHERE ap.isPromoPic = 1 AND a.cbState.stateName = :stateName

到目前为止,我已尝试过此操作并收到错误:'Accommodation' cannot be resolved to a type.。有什么不对吗?

更新

新查询:SELECT new ba.reservation.pojo.Accommodation(a.name, a.rank, ap) FROM Accommodation AS a INNER JOIN a.accommodationPics AS ap WHERE ap.isPromoPic = 1 AND a.cbState.stateName = :stateName

@Column(name = "name", nullable = false, length = 200)
private String name;

@Column(name = "rank")
private int rank;

@OneToMany(mappedBy = "accommodationBean", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
@JsonManagedReference
private List<AccommodationPic> accommodationPics;

public Accommodation(String _name, int _rank, ArrayList<AccommodationPic> _pic) {
    this.rank = _rank;
    this.name = _name;
    this.accommodationPics = _pic;
}

2 个答案:

答案 0 :(得分:0)

住宿必须在课程名称前使用完整的包裹名称完全限定。

答案 1 :(得分:0)

关于此查询:

有两种错误:

第一个(如simas_ch所说):

当您使用新对象(在您的情况下为Accomodation)时,您必须使用所有命名空间路径完全限定您的对象

第二个:

您必须在pojo定义中定义一个正确的构造函数。

如果你这样写:

new Accommodation(a.name, a.rank, ap.pic)

你必须在Accomodation类中有一个像这样的构造函数:

public Accommodation(String name, int rank, a.name, ArrayList<AccommodationPic> pic) {
    this.name = name;
    this.rank = rank;
    this.pic = pic;
}