简体   繁体   中英

Issues with Hibernate HQL select query

Below is the code snipped for my POJO and the HQL query to retrieve the roles for the given account. For some reason the HQL query results in a '0' rows selected even though a match exists in the DB.

@Entity
@Table(name="account_roles")

public class AccountRoles implements Serializable {

     @Column(name = "roleName")
     private String roleName;

     @Column(name = "accountNumber")
     private String accountNumber;

     @Id
     @Column(name = "iD")
     private int iD;

     public String getRoleName() { return roleName; }
     public void setRoleName(String roleName){ this.roleName = roleName;}

     public String getAccountNumber() { return accountNumber;}
     public void setAccountNumber(String accountNumber){ this.accountNumber = accountNumber;}

     public int getId() { return iD;}
     public void setId(int iD){ this.iD = iD; }

}

@Override
@SuppressWarnings("unchecked")
public AccountRoles getKey(String accountNum, String roleName) throws Exception{
    String sql = "from AccountRoles where accountNumber= :accountNumber AND roleName= :roleName";
    Session session = getSessionFactory().getCurrentSession();
    List<AWSAccountRoles> accountRoles;
    try{
        accountRoles = session.createQuery(sql)
                .setString("accountNumber",accountNum)
                .setString("roleName",roleName)
                .list();
        if(accountRoles.size() >0 ){
            System.out.println(" found at least 1 row");
            return accountRoles.get(0);
        }else{
            System.out.println("returned empty list");
            return null;
        }
    }catch (Exception ex){
        ex.printStackTrace();
        throw ex;
    }

DDL for the table is as follows:

CREATE TABLE `account_roles` (
  `iD` int(11) NOT NULL AUTO_INCREMENT,
  `accountNumber` varchar(15) NOT NULL,
  `roleName` varchar(25) NOT NULL,
   PRIMARY KEY (`id`),
   KEY `accountNumber` (`accountNumber`,`roleName`)
) ENGINE=InnoDB AUTO_INCREMENT=63 DEFAULT CHARSET=latin1;

Need some help with debugging why the query results in an empty list ( or '0' rows )

Found the issue. The issue that I was facing was a result of recursive @Transactional events. Once I separated out the calls , the query and insert work fine .

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