我一直在尝试实现休眠过滤器,但是似乎得到了一些奇怪的结果。
当我得到一个列表时,当我移到下一个对象时,Ship Entity的第一个结果似乎已经获得了正确的BuildFormula。
1st:itemType =发货 第二名:itemType =建筑物
Session session = entityManager.unwrap(Session.class);
session.enableFilter(BY_USER_ID_PLANET_ID_STAR_ID)
.setParameter(USER_ID, UserInfoHolder.getLocation().getUserId())
.setParameter(PLANET_ID, UserInfoHolder.getLocation().getPlanetId())
.setParameter(STAR_ID, UserInfoHolder.getLocation().getStarId());
session.enableFilter(TYPE).setParameter("itemType", SHIP);
List<Ship> buildingList = session.createQuery("FROM Ship", Ship.class).getResultList();
return buildingList.stream().map(this::convertToVO).collect(Collectors.toList());
主要实体
@Data
@Table
@Entity
public class Ship {
public static final String SHIP = "ship";
@Id
private Integer id;
private String name;
private String description;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "itemId", referencedColumnName="id", insertable = false, updatable = false)
@Filter(name = "byType", condition = "item_type = :itemType")
private List<BuildFormulas> buildFormulas;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "shipId", insertable = false, updatable = false)
@Filter(name = "byUserIdPlanetIdStarId")
private List<ShipToUser> shipToUser;
}
还有BuildFormulas类
@Data
@Entity
@FilterDef(
name = "byType",
defaultCondition = "item_type = :itemType",
parameters = {
@ParamDef(name = "itemType", type = "string")
}
)
public class BuildFormulas {
@Id
private Integer itemId;
private String metalCost;
private String crystalCost;
private String deuteriumCost;
private String energyCost;
private String speed;
private String itemType;
}
还有ShipToUser
@Data
@Entity
@FilterDef(
name = "byUserIdPlanetIdStarId",
defaultCondition = "user_id = :userId AND planet_id = :planetId AND star_id = :starId",
parameters = {
@ParamDef(name = "userId", type = "int"),
@ParamDef(name = "planetId", type = "string"),
@ParamDef(name = "starId", type = "string")
}
)
public class ShipToUser {
@EmbeddedId
private ShipToUserKey shipToUserKey;
private int amount;
}
为解开这个谜团提供任何帮助。