以下是我的POJO和用于检索给定帐户角色的HQL查询的代码。出于某种原因,HQL查询导致了' 0'即使数据库中存在匹配项,也会选择行。
@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如下:
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;
需要一些帮助来调试查询导致空列表(或&#39; 0&#39;行)的原因
答案 0 :(得分:0)
发现了这个问题。我面临的问题是递归的@Transactional事件的结果。一旦我分离出调用,查询和插入工作正常。