如何将 JPQL 查询分配给简单的 Java 对象?

发布于 2024-09-17 12:26:39 字数 367 浏览 5 评论 0原文

我需要将 jpql 结果的结果分配给一个简单的类 java 对象

我有这样的东西

class myObject() {
@id
private Long id;
private String Name;
private String description;
...
//getters and setters
}

我需要以某种方式存储此 SQL 查询的结果,例如

// 可以是 anytable SELECT DISTINCT c.table_id, c.name, NULL AS description FROM anyTable

如何在 JPQL 中执行此操作,然后将结果分配给我的对象?

I need to assign the result of a jpql result to a simple class java object

I have something like this

class myObject() {
@id
private Long id;
private String Name;
private String description;
...
//getters and setters
}

I need to somehow to store the result of this SQL query, example

// could be anytable
SELECT DISTINCT c.table_id, c.name, NULL AS description FROM anyTable

How do I do this in JPQL and then assign the result to my object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠北森屋 2024-09-24 12:26:39

这个问题非常不清楚。所以这是一个模糊的答案:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyPu");
EntityManager em = emf.createEntityManager();    
Query q = em.createQuery("select f.id, f.name from Foo f");
List<Object[]> foos = (List<Object([]>)q.getResultList();

MyObject o = new MyObject();
o.setFoos(foos);

The question is extremely unclear. So here is a vague answer:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyPu");
EntityManager em = emf.createEntityManager();    
Query q = em.createQuery("select f.id, f.name from Foo f");
List<Object[]> foos = (List<Object([]>)q.getResultList();

MyObject o = new MyObject();
o.setFoos(foos);
ˉ厌 2024-09-24 12:26:39

我想,这就是答案!

SELECT NEW entities.PersonWell(c.peopleId, c.name, c.age, c.height, c.weight)
FROM 
  People AS c;

它是Entity.PersonWell类的构造函数:

public PersonWell(Integer peopleId, String name, short age, short height, short weight, short speechspeed) {
        this.peopleId = peopleId;
        this.name = name;
        this.age = age;
        this.height = height;
        this.weight = weight;
        this.speechspeed = speechspeed;
    }

有一些用于提取结果的代码:

List<PersonWell> resultList = query.getResultList();

我希望,它会对您有所帮助! :)

I think, it's the answer!

SELECT NEW entities.PersonWell(c.peopleId, c.name, c.age, c.height, c.weight)
FROM 
  People AS c;

And it's a constructor of entities.PersonWell class:

public PersonWell(Integer peopleId, String name, short age, short height, short weight, short speechspeed) {
        this.peopleId = peopleId;
        this.name = name;
        this.age = age;
        this.height = height;
        this.weight = weight;
        this.speechspeed = speechspeed;
    }

There's some code for an extraction of result:

List<PersonWell> resultList = query.getResultList();

I hope, It'll help you! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文