Reputation: 1
Below is the table
CREATE TABLE MyTable(
'a_id' varchar(50) NOT NULL,
'b_id' varchar(100) NOT NULL,
'my_field' varchar(1) DEFAULT '0',
PRIMARY KEY ('a_id','b_id'));
Values will be
a_id - b_id - my_field
aa - b1 - N
aa-b2-N
aa-b3-Y
aa-b5-N
pp-q1-Y
pp-q2-N
pp-q3-N
pp-q4-B
I want to have two Java beans as
Pojo1.java
private String aId
private List<Pojo2> pojo2
Pojo2.java
private String bId
private boolean myField
I want to retrieve the a_id for a given b_id (I'm am able to retrieve this)
I want to retrieve the List of pojo2
for a single a_id
(Have issue with this)
My Java code which throws
[b]java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to pojo2[/b]
try {
session = // create Hibernate session;
String query = "select x.bId,x.myField from MyTable x where x.aId=?";
Query queryObj = session.createQuery(query);
queryObj.setString(0, "value to aId");
Pojo2 mypojo2 = (Pojo2) queryObj.list();
} catch (Exception e) {
} finally {
close the connection
}
Below is the hibernate file for MyTable
<hibernate-mapping auto-import="true" default-lazy="false">
<class name="com.db.beans.MyTable" table="MYTABLE">
<composite-id>
<key-property name="aId" type="string" column="a_id" length="100"/>
<key-property name="bId" type="string" column="b_id" length="100"/>
</composite-id>
<property name="myField" type="string" column="my_field" length="1" />
</class>
</hibernate-mapping>
Please help in correcting my mapping file or Java code.
Upvotes: 0
Views: 67
Reputation: 13999
queryObj.list()
returns a list. You can't cast a list to a single Pojo2. If your query and mapping are right, then you'll be able to use
Pojo2 mypojo2 = (Pojo2) (queryObj.list().get(0));
Upvotes: 1