How to get the value of a database table in sleep mode?

I am new to Hibernate, I want to get table values ​​from a database, I have code, but it returns the values ​​of the object. 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 some column values ​​... How can I do this?

+4
source share
4 answers

I can easily get the value from the list. But in my question above I only print the property of the object, not the 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()); } 
+2
source

This is what Hibernate (or JPA) is designed for. If you want to access regular values, use JDBC instead.

+1
source
 select main.empid,main.address from Main as main 

check that empid,address is the name of the column in the database or the property name of the main class.

This should be the name of a property of the entity (ie Main) class.

0
source

This is very useful when we extract some fields / properties from our entity class. The above query with the keyword "new" may return a list of types of "Main". If we do not use such a keyword and specify the fields directly, a list of Object [] types is retrieved.

select new Main(main.empid,main.address) from Main as main ,

0
source

Source: https://habr.com/ru/post/1499578/


All Articles