简体   繁体   中英

How to fix this query in hibernate?

So I have this query as below :

/**
 * @param partnerId .
 * @return Notification
 */
@Query("SELECT p FROM PartnerNotification p "
        + "INNER JOIN account AS account " + "WHERE account.accountId = :partnerId ")
 PartnerNotification findNotificationByPartnerId(@Param("partnerId") Integer partnerId);

where I am trying to select all the column in partner notification @Entity where partner id is equal with the parameter. partner_id is a foreign key at my table so at my entities I have :

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "account_id")
private Account account;

and maybe here is the problem any way I have tried to test some idea I had but as is the first time there I could not find what's is wrong.

SO i have tried :

//    /**
//     * @param partnerId partnerId.
//     * @return PartnerNotification
//     */
//    PartnerNotification findNotificationByPartnerId(Integer partnerId);

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "account_id")
private Account account;

/**
 * @param partnerId .
 * @return Notification
 */
@Query("SELECT p FROM PartnerNotification p "
        + "JOIN p.account acc WHERE acc.accountId = :partnerId ")
 PartnerNotification findNotificationByPartnerId(@Param("partnerId") Integer partnerId);

and

 @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "account_id")
    private Account account;

Try this

@Query(SELECT p FROM PartnerNotification p JOIN p.account acc WHERE acc.accountId = :partnerId)

Hope it helps!

实际上,您不需要任何自定义查询。

List<PartnerNotification> findByAccountId(Integer partnerId);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM