我编写了以下代码段,以使用zips
作为JPA提供程序从hibernate
表中获取某些zip文件的记录。
public List<ZipEntity> getZipEntityFromZipName(String zipName, String version, String createdBy,
String type) throws FileException {
int numAttempts = 0;
do {
numAttempts++;
EntityManager entityManager = getNewEntityManager();
try {
TypedQuery<ZipEntity> query = entityManager
.createNamedQuery(Constants.Database.Queries.GET_FROM_ZIP_NAME, ZipEntity.class)
.setParameter("zipName", zipName)
.setParameter("version", version)
.setParameter("createdBy", createdBy)
.setParameter("type", type);
return query.getResultList();
} catch (PersistenceException e) {
validatePersistenceException(e);
} finally {
closeEntityManager(entityManager);
}
} while (numAttempts <= maxRetries);
throw new FileException("Database connection failed.");
以下是相关的实体类
@NamedNativeQueries({
@NamedNativeQuery(
name = Constants.Database.Queries.GET_FROM_ZIP_NAME,
query = Constants.Database.Queries.GET_FROM_ZIP_NAME_QUERY,
resultClass = ZipEntity.class
)
})
@Entity
@Table(name = "zips")
public class ZipEntity {
@EmbeddedId
private ZipKey ZipKey;
public ZipEntity() {
}
public ZipEntity(String zipName, String version, String createdBy, String file, String type,
String extension) {
this.ZipKey = new ZipKey(zipName, version, createdBy, file, type, extension);
}
}
@Embeddable
public class ZipKey implements Serializable {
static final long serialVersionUID = 1L;
@Column(name = "zip_name")
private String zipName;
@Column(name = "version")
private String version;
@Column(name = "created_by")
private String createdBy;
@Column(name = "filepath")
private String file;
@Column(name = "type")
private String type;
@Column(name = "extension")
private String extension;
// Getter, setters and Constructor
}
Constant类中的查询如下,
public static final String GET_FROM_ZIP_NAME = "getFile";
public static final String GET_FROM_ZIP_NAME_QUERY = "SELECT * FROM zips WHERE zip_name = " +
":zipName AND version = :version AND created_by = :createdBy AND type = :type";
尽管没有为上述查询定义setMaxResults()
事件,但是从DB代码执行的相同查询得到35条记录,但从上述代码段获得的结果限制为25条记录。我在这里做错了什么?
答案 0 :(得分:1)
请调试您的解决方案,并检查“ zipName”,“版本”,“ createdBy”以及“类型”参数的值,以验证它们是否为您期望的值。此查询包含由AND逻辑组合的条件,这些条件会影响您的结果。要获得35条记录,您的参数应使所有35条记录的条件都成立。
您可以在NamedNativeQuery中限制以下记录,一次可以为您提供35条记录。
@NamedNativeQuery(
name = Constants.Database.Queries.GET_FROM_ZIP_NAME,
query = Constants.Database.Queries.GET_FROM_ZIP_NAME_QUERY,
fetchSize = 35,
resultClass = ZipEntity.class
)