0
I am a beginner to hibernate....
I have a two tables like,

t_2013
id  name
1   donald


t_2014
id   name
2    charles

This is my jpa,


  @Entity
  @Table(name="t_2013")
  public class Test implements Serializable {

@Id
@GeneratedValue
@Column(name="id")
private int id;

@Column(name="name")
private String name;

Here i create one jpa with table name of t_2013. My Question is how to get the data from t_2014 using same jpa. Is there any way to access single jpa to many tables with similar table structure

 Query
 -----
  List<Test> list=    hibernateTemplate.getSessionFactory().openSession().createSQLQuery("Select * from t_2014").list();

error:java.lang.ClassCastException:

 or

 List<Object> list=    hibernateTemplate.getSessionFactory().openSession().createSQLQuery("Select * from t_2014").list();

 if i use above code how to iterate it.

 Iterator ir=list.iterator();
 while (pairs.hasNext()) {
 Object[] pair = (Object[]) pairs.next();
 Test obj=(Test)pair[0];
 }

If i iterate above format i got this error : java.lang.Integer cannot be cast to com.model.Test. how to get data from similar table structure...

2
Contributors
1
Reply
12
Views
3 Years
Discussion Span
Last Post by cool_zephyr
0

try this

List<Object[]> list=(List<Object[]>)query.list();

In order to loop through the records in the list

for(Object[] row:list) {
    Integer id=(Integer)row[0];
    String name=(String)row[1]);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.