Hibernate filters

Viewed 61

I've been trying to implement hibernate filters however I seem to be getting some strange results.

When I get a list the first result of Ship Entity seems to have gotten the correct BuildFormula when I move to the next object the BuildFormula is incorrect.

1st: itemType = Ship 2nd: itemType = Building

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());

The main entity

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

}

And the BuildFormulas class

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

And the 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;

}

Any help would be appreciated to unlocking this mystery.

0 Answers
Related