[英]How to retrieve database table value in hibernate?
I am new to the Hibernate, i want to retrieve table values from database, I have a code but it returns object values. 我是Hibernate的新手,我想从数据库中检索表值,我有一个代码,但它返回对象值。 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? 我需要获得多个列值......我怎么能这样做?
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());
}
This is what Hibernate (or JPA rather) is meant for. 这就是Hibernate(或JPA)的意思。 If you want to access regular values, use JDBC instead.
如果要访问常规值,请改用JDBC。
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. 请检查
empid,address
是数据库中的列名称还是Main类的属性名称。
It should be the property name of entity(ie Main) class. 它应该是实体(即Main)类的属性名称。
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”.
上面带有“new”关键字的查询可以返回“Main”类型的列表。 If we do not use such a keyword and specify the fields directly, a list of type Object [ ] is retrieved.
如果我们不使用这样的关键字并直接指定字段,则会检索Object []类型的列表。
select new Main(main.empid,main.address) from Main as main
, select new Main(main.empid,main.address) from Main as main
,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.