gpt4 book ai didi

java - criteria.list() 和 sql-query 返回不同的结果集

转载 作者:行者123 更新时间:2023-11-30 03:40:46 25 4
gpt4 key购买 nike

我有以下条件查询:

@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;
}

和映射:

@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;

问题是我无法理解为什么 criteria.list()log4j2 记录的 sql-query 返回不同的结果集。我真的不知道会出现什么问题?你能给我建议吗?

最佳答案

结果会有所不同,因为您的 criteria.list() 结果中可能会出现重复项。您应该替换此行:

criteria.setResultTransformer(Criteria.ROOT_ENTITY);

有了这个:

criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

这样做至少应该确保您的结果不会return duplicates.

关于java - criteria.list() 和 sql-query 返回不同的结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26870238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com