JPA本地查询(Native Query)的总结2
OK,现在对同一个查询,再把结果集全部映射进实体对象。
?
首先
@NamedNativeQueries
(
??? {
?????? @NamedNativeQuery(
?????????? name="ReturnOrderListWithFullEntityType",
c.name as customer_name,c.ctype as customer_type,c.id as customer_id from orders o join customer c on o.cust_id=c.id where o.cust_id=?1",
?????????? resultSetMapping="ReturnOrderListWithFullEntityType"),
??? ??? 。。。。。。。。。。。。。可能还有更多的本地查询设置
}
)
改变本地查询的resultSetMapping
?
??? @SqlResultSetMapping
??? (
?????? name="ReturnOrderListWithFullEntityType",
?????? entities=
?????? {
?????????? @EntityResult
?????????? (
????????????? entityClass=entity.Order.class,
????????????? fields=
????????????? {
??? @FieldResult(name="id",column="order_id"),
????????????????? ?????? @FieldResult(name="date",column="order_creation_date"),
????????????????? @FieldResult(name="desc",column="order_description"),
??? @FieldResult(name="sum",column="order_sum_total")
????????????? }
?????????? ),
?
?????????? @EntityResult
?????????? (
????????????? entityClass=entity.Customer.class,
????????????? discriminatorColumn="customer_type",
????????????? fields=
????????????? {
????????????????? @FieldResult(name="id",column="customer_id"),
????????????????? @FieldResult(name="name",column="customer_name")
????????????? }
?????????? )
?????? },
?????? columns={}
??? )
?
entities属性是一个包含与返回结果集相映射的所有实体的一个数组。这条查询得到了订单和用户的信息,所以,与结果集映射的实体,就应该是Order和Customer。
每一个映射实体用@EntityResult表示,entityClass表示实体类,fields表示该实体类中的属性,与查询结果中的哪些个列相映射。而每一个“属性<->列”的映射关系,又保存在@FieldResult里面。name是实体属性,column是查询列(或列别名)
?
要注意的是,如果要将结果集映射到实体对象,则
1.Select 语句中必须包含实体所映射的表中的PK,也就是 select o.id as order_id
?
否则,JPA就会抛出一个exception
oracle.toplink.essentials.exceptions.QueryException
Exception Description: The primary key read from the row [DatabaseRecord(
??? orders.ID => null
??? orders.CREATE_DATE => 2009-05-27 00:00:00.0
??? orders.SUM_PRICE => 36817.0
??? orders.DESCRIPTION => This is an order creation example.
??? orders.cust_id => null)] during the execution of the query was detected to be null.? Primary keys must not contain null.
?
?
?
?
?
再看一下关于Customer实体的映射,与Order不一样的地方,是discriminatorColumn="customer_type" (可能为列别名)
加上这个属性的原因是,Customer类是一个父类,以供其他子类继承,而J PA的内在多态性机制将会获取到Customer的实际类型。根据J PA @Inheritance批注,得知,父类实体的表中必须有一个字段(默认为DTYPE)来表示各个子类的类型。所以当要将查询结果集保存为实体的时候,它必须要知道你所保存的这个Customer实体到底是哪种类型。
所以,你还必须在select子句中取得这个Discrimator Type的字段。
否则又会抛出异常:?
oracle.toplink.essentials.exceptions.QueryException
Exception Description: Custom SQL failed to provide descriminator column : , as defined in SQLResultSetMapping : ReturnOrderListWithFullEntityType.
或者:
oracle.toplink.essentials.exceptions.QueryException
Exception Description: Custom SQL failed to provide descriminator column : customer_type, as defined in SQLResultSetMapping : ReturnOrderListWithPartEntityPartScalarType.
?
?
通过这种映射方式,我们可以推测,得到的结果集List中,数据会是这样:
[? {“Order对象1”, “Customer对象1”},? {“Order对象2”, “Customerr对象2”}, ……? ]
?
经过运行测试程序,得到了我们的推论
?
*****ReturnOrderListWithFullEntityType*****
entity.Order@48edb5? entity.GoldenCustomer@1ee2c2c??
entity.Order@1402d5a entity.GoldenCustomer@1ee2c2c??
entity.Order@1e13e07 entity.GoldenCustomer@1ee2c2c??
entity.Order@9cfec1? entity.GoldenCustomer@1ee2c2c??
entity.Order@747fa2? entity.GoldenCustomer@1ee2c2c??
……
看来的确是保存了对象,而且注意第二个对象不是Customer而是GoldenCustomer,这说明,JPA自动将数据映射进了GoldenCustomer实体(尽管我们使用的是Customer实体进行保存? “entityClass=entity.Customer.class”)