Reputation: 2436
I am new to the Hibernate, i want to retrieve table values from database, I have a code but it returns object values. My sample code is,
Configuration conf=new Configuration();
@SuppressWarnings("deprecation")
SessionFactory sessionfactory=conf.configure().buildSessionFactory();
Session session=sessionfactory.openSession();
List maintable = null;
try
{
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery ("select main.empid,main.address from Main as main");
maintable =q.list();
Object[] obj=maintable.toArray();
for(int i=0;i<obj.length;i++)
{
System.out.println("column valuse : "+obj[i]);
}
tx.commit();
session.close();
}
catch(Exception e1)
{
System.out.println("Exception");
}
I need to get multiple column values...How can i do that?
Upvotes: 4
Views: 9457
Reputation: 2436
I can retrieve value from list easily.But in my above question i print only object property not a value.
Query qry=session.createQuery("from Main");
List<Main> user=(List<Main>) qry.list();
session.getTransaction().commit();
session.close();
for(Main u : user)
{
System.out.println("User id : "+u.getEmpid());
System.out.println("User Address:"+u.getAddress());
}
Upvotes: 2
Reputation: 190
select main.empid,main.address from Main as main
please check that empid,address
are the column name in the database or the property name of Main class.
It should be the property name of entity(i.e Main) class.
Upvotes: 0
Reputation: 1737
It’s very useful when we retrieve some fields/properties from our entity class. The above query with “new” keyword can return a list of type “Main”. If we do not use such a keyword and specify the fields directly, a list of type Object [ ] is retrieved.
select new Main(main.empid,main.address) from Main as main
,
Upvotes: 0
Reputation: 73528
This is what Hibernate (or JPA rather) is meant for. If you want to access regular values, use JDBC instead.
Upvotes: 1