I am trying to fetch specific fields from my entities. I need the result in my entity structure.
Following are my entities:
Country
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;
}
State
public class StateModel {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "SmtID")
private int id;
@Column(name = "SmtName")
private String name;
}
Following is the HQL query am executing:
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();
But am getting the following error:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.muziris.entity.CountryModel
Thanks for helping.
Expected result:
Country :
name : india
state :
name : kerala
name : goa
name : Pak
state :
name : karachi