[英]criteria.list() and sql-query return different result sets
I've the following criteria-query: 我有以下条件查询:
@Transactional(
propagation = Propagation.REQUIRED,
isolation = Isolation.READ_COMMITTED,
readOnly = true
)
public List<GameSession> getGameSessions(Integer maxId,
boolean active, Integer gameId, GameType gameType,
Integer playerCategoryId, String playerEmail, String playerLogin, Date from, Date to, Integer playerId, CasinoCurrency currency,
CasinoGameSessionSortField sortField, SortOrder sortOrder,
int offset, int limit) {
Criteria criteria = getSession().createCriteria(GameSession.class);
if (maxId != null) {
criteria.add(Restrictions.le("id", maxId));
}
if (active) {
criteria.add(Restrictions.eq("active", active));
}
Criteria gameCriteria = criteria.createCriteria("game", "g");
if (gameId != null && gameId > 0) {
gameCriteria.add(Restrictions.eq("g.id", gameId));
}
if (gameType != null) {
gameCriteria.add(Restrictions.eq("g.type", gameType));
}
if (from != null) {
criteria.add(Restrictions.ge("startDate", from));
}
if (to != null) {
criteria.add(Restrictions.le("startDate", to));
}
Criteria playerCriteria = criteria.createCriteria("player", "p");
if (playerId != null) {
playerCriteria.add(Restrictions.eq("p.id", playerId));
}
if (playerCategoryId != null) {
playerCriteria.add(Restrictions.eq("p.category.id", playerCategoryId));
}
if (StringUtils.isNotBlank(playerEmail)) {
playerCriteria.add(Restrictions.like("p.email", "%" + playerEmail.toLowerCase() + "%"));
}
if (StringUtils.isNotBlank(playerLogin)) {
playerCriteria.add(Restrictions.like("p.login", "%" + playerLogin.toLowerCase() + "%"));
}
if (currency != null) {
criteria.add(Restrictions.eq("currency", currency));
}
if (sortField != null && sortOrder != null) {
Order order = (sortOrder == SortOrder.ASC) ? Order.asc(sortField.getValue()) : Order.desc(sortField.getValue());
criteria.addOrder(order);
}
criteria.setFirstResult(offset);
criteria.setMaxResults(limit);
criteria.setResultTransformer(Criteria.ROOT_ENTITY);
List<GameSession> list = (List<GameSession>) criteria.list();
//Native sql, returned by Log4j and that criteria.list() returns different result set
//Why?
return list;
}
and Mapping: 和映射:
@Entity
@Table(name = "game_session")
public class GameSession {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "last_access_date", nullable = false)
private Date lastAccessedDate;
@Column(
name = "start_date",
nullable = false,
insertable = true,
updatable = false
)
private Date startDate;
@Column(name = "end_date", nullable = true)
private Date endDate;
@ManyToOne(targetEntity = Player.class, fetch = FetchType.LAZY)
@JoinColumn(name = "player_id")
private Player player;
@ManyToOne(targetEntity = Game.class, fetch = FetchType.LAZY)
@JoinColumn(name = "casino_game_id")
private Game game;
@Column(
name = "payout",
nullable = false,
insertable = true,
updatable = true
)
private BigDecimal payout;
@Type(
type = "com.strg.utils.db.hibernate.GenericEnumUserType",
parameters = {
@Parameter(
name = "enumClass",
value = "com.strg.commons.db.entity.CasinoCurrency"),
@Parameter(
name = "identifierMethod",
value = "getValue"),
@Parameter(
name = "valueOfMethod",
value = "getByValue")
}
)
@Column(name = "currency_id", nullable = true, updatable = true)
private Currency currency;
@Column(name = "balance", nullable = true)
private BigDecimal balance;
@Column(name = "active", nullable = false)
private boolean active;
@Column(
name = "rounds_count",
nullable = false,
insertable = true,
updatable = true
)
private int roundsCount;
The issue is I can't understand why that criteria.list()
and sql-query logged by log4j2
returns different result set. 问题是我不明白为什么
log4j2
记录的criteria.list()
和sql-query返回不同的结果集。 I really have no idea what could be a problem with? 我真的不知道有什么问题吗? Could you suggest me anything?
你能建议我什么吗?
The results are different because you are likely getting duplicates in your criteria.list()
results. 结果有所不同,因为您可能会在
criteria.list()
结果中出现重复项。 You should replace this line: 您应该替换此行:
criteria.setResultTransformer(Criteria.ROOT_ENTITY);
With this one: 有了这个:
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
Doing so should at least ensure that your results won't return duplicates. 这样做至少应确保您的结果不会返回重复项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.