Java 如何使用JPA从表中获取多列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22653858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:07:30  来源:igfitidea点击:

How to get multiple columns from table using JPA?

javahibernatejpaeclipselinknativequery

提问by Prabha

For example I have a table as Studentit's contain columns like id, name, ageI am reverting particular column values by using NativeQuerylike below.

例如,我有一个作为Student的表,它包含像idnameage这样的列, 我正在使用如下所示的NativeQuery恢复特定的列值。

Query query = entityManager.createNativeQuery("SELECT age FROM Student");
List list=query.getResultList(); 

By using above query We will get list of ages in Studenttable Now I want to get ageand namefrom table.

通过使用上面的查询,我们将在Student表中获取年龄列表现在我想从表中获取年龄姓名

Query query = entityManager.createNativeQuery("SELECT age,name FROM Student");
List list=query.getResultList();

If I do like this My code is execute well, but How can I get name in one list and age in another list.So how can I do this. Thank you very much

如果我喜欢这个我的代码执行得很好,但是我如何在一个列表中获取名称并在另一个列表中获取年龄。那么我该怎么做。非常感谢

NoteI don't have any Entity class or POJO classes in my project I am getting table from Database using Native Query.

注意我的项目中没有任何实体类或 POJO 类我使用本机查询从数据库获取表。

回答by Sebastian ?askawiec

I believe this question has been answered here.

我相信这里已经回答这个问题。

However I strongly suggest to create standard entity called Student with column name and age. This approach will be much easier to implement and to maintain it in the future.

但是,我强烈建议使用列名和年龄创建名为 Student 的标准实体。这种方法在未来更容易实现和维护。

回答by dispake

You could approach it two ways.

你可以通过两种方式来处理它。

  1. Parse the values from your result set into separate Lists. Since you don't have an entity defined for it, the query will return a

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

    For each row returned, you can access age and name by the index in which they appear. i.e. Age is selected first in your above example so you would it would be index 0 and Name is index 1.

    for (Object[] obj : list) {
         age = obj[0];
         name = obj[1];
         // ..put them into whatever collection you desire
    }
    
  2. Use two queries. One that selects AGE and the other that selects NAME. Each one will return the corresponding values to it's own list.

    Query query = entityManager.createNativeQuery("SELECT age FROM Student");
    List ages=query.getResultList(); 
    
    query = entityManager.createNativeQuery("SELECT name FROM Student");
    List names=query.getResultList();
    

    Only caveat to this method is that there is no true relationship between the two lists. In other words, ages[3] may not logically match names[3]. But... as it is, the original question does not have any definition as to how the two lists should be created.

  1. 将结果集中的值解析为单独的列表。由于您没有为其定义实体,查询将返回一个

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

    对于返回的每一行,您可以通过它们出现的索引访问年龄和姓名。即在上面的示例中首先选择了年龄,因此您将是索引 0,名称是索引 1。

    for (Object[] obj : list) {
         age = obj[0];
         name = obj[1];
         // ..put them into whatever collection you desire
    }
    
  2. 使用两个查询。一个选择 AGE,另一个选择 NAME。每个都将相应的值返回到它自己的列表中。

    Query query = entityManager.createNativeQuery("SELECT age FROM Student");
    List ages=query.getResultList(); 
    
    query = entityManager.createNativeQuery("SELECT name FROM Student");
    List names=query.getResultList();
    

    唯一需要注意的是,这两个列表之间没有真正的关系。换句话说,年龄[3] 可能在逻辑上与名称[3] 不匹配。但是......实际上,原始问题没有关于应该如何创建这两个列表的任何定义。

回答by Abderrahim

I based on the answer of @dispake, he suggested two ways for fix this issue but he forgot tu put the casting for the first ways. In my opinion it is the optimized one (you just need to do one query instead of two) do it like this :

我根据@dispake 的回答,他提出了两种解决此问题的方法,但他忘记了将演员表放在第一种方法上。在我看来,它是优化的(您只需要执行一个查询而不是两个查询)这样做:

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

for (Object[] obj : list) {
     int age = (int) obj[0];
     String name = (String) obj[1];
}

I hope this will help you

我希望这能帮到您