【发布时间】:2018-03-07 09:22:41
【问题描述】:
我正在尝试从我的实体中获取特定字段。我需要我的实体结构中的结果。
以下是我的实体:
国家
public class CountryModel {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "CmtID")
private int id;
@Column(name = "CmtName")
private String name;
@JoinColumn(name="CmtStateID")
@OneToMany(targetEntity=StateModel.class,fetch=FetchType.EAGER)
private List<StateModel> state;
}
州
public class StateModel {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "SmtID")
private int id;
@Column(name = "SmtName")
private String name;
}
以下是正在执行的 HQL 查询:
Query query = session.createQuery("select c.name, s.name from CountryModel c join c.state s where c.id=2");
CountryModel stateModel = (CountryModel) query.uniqueResult();
但出现以下错误:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.muziris.entity.CountryModel
感谢您的帮助。
预期结果:
Country :
name : india
state :
name : kerala
name : goa
name : Pak
state :
name : karachi
【问题讨论】: