我有一个名为CUSTOMER
的表格,其中包含100多个COLOUMNS。但是我想只选择我在POJO中指定的6列:
实体POJO:
@Entity
@Data
@Table(name = "CUSTOMER")
public class CustomerEntity {
@Id
@Column(name = "C_ID")
private String customerId;
@Id
@Column(name = "C_KEY")
private String customerKey;
@Column(name = "NAME")
private String name;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "AGE")
private String age;
}
NativeQuery:
String query = "select * from CUSTOMER where (C_ID= '1' AND C_KEY= '12') OR (C_ID= '1' AND C_KEY= '13')) AND AGE>25";
Query q = e.createNativeQuery(query,CustomerEntity.class);
[编辑:出于使用原生查询的原因]
选择原生查询的原因:
复杂逻辑:
SELECT * FROM (
SELECT row_number() over(order by C_ID, C_KEY) RN, FEW-COLUMNS(
SELECT * FROM BOOK
WHERE (C_ID, C_KEY) IN (customerId1, customerKey1)
(customerId2, customerKey2)
(customerId3, customerKey3)
.....
(customerIdn, customerKeyn) AND ROWNUM <= 340
)WHERE RN BETWEEN anyNumber and anyNumber
)ORDER BY DESC RN;
问题:
由于它是一个namedQuery,我无法将查询作为
传递String query =“select Customer from CustomerEntity cu where((cu.customerId ='1'AND cu.customerKey = '12')或(cu.customerId ='1'AND cu.customerKey ='13')AND cu.age&gt; 25)“;
如果我使用此查询,我将获得ORA -00947
表或视图不存在异常。
答案 0 :(得分:3)
您必须使用别名才能使其正常工作。否则,持久性提供程序将不知道如何执行匹配:
select c_id as customerId, c_key as customerKey...
from CUSTOMER
where ...
别名应与您映射到的实体类的相应字段名称完全相同。