Hibernate filters









up vote
3
down vote

favorite












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.










share|improve this question



























    up vote
    3
    down vote

    favorite












    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.










    share|improve this question

























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      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.










      share|improve this question















      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.







      java spring hibernate






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago









      Gayan Mettananda

      1355




      1355










      asked 2 days ago









      VortexFX

      516




      516



























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225221%2fhibernate-filters%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225221%2fhibernate-filters%23new-answer', 'question_page');

          );

          Post as a guest














































































          Popular posts from this blog

          Kleinkühnau

          Makov (Slowakei)

          Deutsches Schauspielhaus