关于JPA规范中 EQL 查询的一点建议!

本文探讨了在使用JPA 2.0作为ORM技术时遇到的问题:如何在不依赖构造函数的情况下,通过EQL SQL语句创建Java类实例。作者提出了一种利用getter方法而非构造函数的方法,并给出了具体的实现案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

When I use JPA as ORM technology in my project,I have some problem.

Following is my problem.


[b]FROM JPA2.0 SPEC
4.8.2Constructor Expressions in the SELECT Clause

A constructor may be used in the SELECT list to return an instance of a Java class. The specified class is not required to be an entity or to be mapped to the database. The constructor name must be fully qualified.

If an entity class name is specified as the constructor name in the SELECT NEW clause, the resulting entity instances are in the new state.
For example,

SELECT NEW com.acme.example.CustomerDetails(c.id, c.status, o.count)
FROM Customer c JOIN c.orders o
WHERE o.count>100[/b]



We can write EQL SQL like that


[color=blue] SELECT NEW com.acme.example.CustomerDetails(c.id, c.status as cdStatus, o.count)
FROM Customer c JOIN c.orders o
WHERE o.count>100[/color]

[color=darkred] without having appropriate Constructor.(Because in real environment,We should randomly access entity's properties.)
instead,JPA implmentation can invoke the get Mehtod.[/color]


(like the native sql
select c.id,c.status as cdStatus,o.count from cust c join order o on(c.id=o.cust_id) where o.count>100 random select clause)





class CustomerDetails
{
private String _id;
private String _status;
private String _count;

public CustomerDetails()
{
}

public void setId(String id)
{
_id = id;
}

public String getId()
{
return _id;
}

public void setCdStatus(String status)
{
_status = status;
}

public String getCdStatus()
{
return _status;
}

public void setCount(String count)
{
_count = count;
}

public String getCount()
{
return _count;
}

}


我曾经发给JSR委员会,没回音,到javaeye上请教高手,我的想法是否可行!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值