Search code examples
javahibernatenamed-querynativequery

different result by executing query in console and namedQuery


in my java web application i need to inquire a list of deposits from a view named VwDepositsInfo by customerNumber.

when i execute my query:

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

in database console my resultList size is 2 and have something like this:

1-{depositTypeDesc="shortTerm"} {depositTypeCode="850"}

2-{depositTypeDesc="longTerm"} {depositTypeCode="2"}

but when i test my code that includes a namedQuery:

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

i get a resultList with size 2 but both the same, sth like this:

1-{depositTypeDesc="shortTerm"} {depositTypeCode="850"}

2-{depositTypeDesc="shortTerm"} {depositTypeCode="850"}

when i make it nativeQuery with defining the result class:

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

again i get the wrong results.

finally i tried nativeQuery without defining the result class:

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

and result was as i expected to be.

and this is my 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;

Does anyone know why this is happening???


Solution

  • VW = view? You probably need to specify the master key

    use @id for unique field :)

    you probably need more than one field with @id for a unique row.

    for example both of DEPOSIT_TYPE_CODE and customerNumber