简体   繁体   English

如何在hibernate中检索数据库表值?

[英]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.

相关问题 如何使用嵌套属性从 thymeleaf、hibernate 中的数据库中检索表值? - How to retrieve Table value from database in thymeleaf , hibernate using nested attribute? 如何在数据库中的表计算中实现价值(休眠)? - How to make value in table computational in database (hibernate)? 如何使用Hibernate检查值并从数据库中检索它(如果存在)? - How to check the value and retrieve it from database if it exists using Hibernate? 使用休眠从表中检索最大值 - Retrieve max value from table using hibernate 如何通过使用Spring 2.0和Hibernate SpringMVC从数据库中检索记录 - how to retrieve the record from database by using spring 2.0 with hibernate springMVC 如何使用Hibernate从数据库中检索列的Java数据类型? - How to retrieve the java data types of columns from the database using Hibernate? hibernate如何从现有数据库视图中检索数据? - How hibernate retrieve data from existing database view? 如何从数据库中检索实例并使用struts2和hibernate显示它? - How to retrieve an instance from the database and display it using struts2 and hibernate? 如何使用Hibernate对数据库中表的列进行排序 - how to sort a column of a table in database with hibernate 如何使用Hibernate将Java对象与数据库表同步 - How to synchronize a Java Object with a Database table with Hibernate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM