通过在控制台中执行查询和namedQuery

时间:2019-10-10 08:42:17

标签: java hibernate named-query nativequery

在我的Java Web应用程序中,我需要按customerNumber从名为VwDepositsInfo的视图中查询存款列表。

当我执行查询时:

select * from VW_DEPOSIT_INFO v where v.CUSTOMER_NUMBER=:customerNo

在数据库控制台中,我的resultList大小为2,并且具有以下内容:

1- {depositTypeDesc =“ shortTerm”} {depositTypeCode =“ 850”}

2- {depositTypeDesc =“ longTerm”} {depositTypeCode =“ 2”}

但是当我测试包括namedQuery的代码时:

    @NamedQuery(
        name = "inquireAccountByCustomerNumber",
         query = "select c from VWDepositInfo c where c.customerNumber=:customerNo"
            )

我得到一个大小为2的resultList,但是两者都一样,就像这样:

1- {depositTypeDesc =“ shortTerm”} {depositTypeCode =“ 850”}

2- {depositTypeDesc =“ shortTerm”} {depositTypeCode =“ 850”}

当我通过定义结果类使其成为nativeQuery时:

Query query = entityManager.createNativeQuery("select * from VW_DEPOSIT_INFO v where v.CUSTOMER_NUMBER=:customerNo", VWDepositInfo.class);

再次得到错误的结果。

最后,我尝试使用nativeQuery而不定义结果类:

Query query = entityManager.createNativeQuery("select * from VW_DEPOSIT_INFO v where v.CUSTOMER_NUMBER=:customerNo");    

结果和我预期的一样。

这是我的VwDepositsInfo.class:

@Entity
@Table(name = "VW_DEPOSIT_INFO")
@Audited(withModifiedFlag = false)
@NamedQueries(
    {@NamedQuery(
            name = "inquireAccountByCustomerNumber",
            query = "select c from VWDepositInfo c where c.customerNumber=:customerNo"
    )
    }
)
public class VWDepositInfo implements Serializable {

@Id
@Column(name = "CUSTOMER_NUMBER")
private Long customerNumber;

@Column(name = "BRANCH_CODE")
private Long branchCode;

@Column(name = "DEPOSIT_TYPE_CODE")
private Long depositTypeCode;

@Column(name = "DEPOSIT_SERIAL")
private Long depositSerial;

@Column(name = "DEPOSIT_TYPE_DESC")
private String depositTypeDesc;

@Column(name = "CURRENCY_TYPE_DESC")
private String currencyTypeDesc;

@Column(name = "DEPOSIT_OPEN_DATE")
private Date depositOpenDate;

有人知道为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

大众=查看? 您可能需要指定主密钥

在唯一字段中使用@id:)

您唯一的一行可能需要多个@id字段。

例如DEPOSIT_TYPE_CODE和customerNumber