2 Replies Latest reply on Feb 12, 2018 2:11 AM by rajivpatil

    Lazy loading not working

    rajivpatil

      Hi

      I am using 9.1.4 version in embedded replicated mode (however currently testing with one instance only).

       

      This is my configuration, I am using JPACacheStore and have set Preload to false

       

      Configuration routingRuleCacheConfig = new ConfigurationBuilder().clustering()

                      .cacheMode(CacheMode.REPL_SYNC)

                      .persistence().passivation(false)

                      .addStore(JpaStoreConfigurationBuilder.class)

                          .purgeOnStartup(false).shared(true)

                          .persistenceUnitName("RULES")

                          .entityClass(RoutingRule.class)

                          .storeMetadata(false)

                          .preload(false)

                          .build();

       

      1. Now if I lookup for a particular Id, I get null, even though data is there in table for that Id

       

      2. If I iterate through all values, I can see data being returned correctly. 

      Cache<Long, Rule> cache1 = this.cacheManager.getRuleCache();

              if (cache1 != null) {

                  CacheSet<Map.Entry<Long, Rule>> rulesCollection = cache1.entrySet();

                  CloseableIterator<Map.Entry<Long, Rule>> itr = rulesCollection.iterator();

                  itr.forEachRemaining(entry -> {

                      try {

                          System.out.println(entry.getKey()+" "+entry.getValue()); <-- This returns correct values

                          System.out.println(cache1.get(entry.getKey()) );  <--- This returns Null

                      } catch (Exception e) {

                          e.printStackTrace();

                      }

                  });

                  itr.close();

              }

        • 1. Re: Lazy loading not working
          ryanemerson

          Hi! I have tried to reproduce this issue, however so far it has worked as expected. Could you tell me more about your setup? Ideally the contents of RoutingRule, the properties set in persistence.xml and your db manufacturer/version. If it's not possible to provide the contents of RoutingRule.class, could you try to create a simple reproducer app which you are able to share?

          • 2. Re: Lazy loading not working
            rajivpatil

            Sorry for the delay, here are the details about my set-up. I have pasted relevant contents of the class, unfortunately I cant upload the actual code, if this does not suffic i can create a reproducer app as u suggested.

             

            Infinispan - 9.1.4 final , being used in embedded REPL_SYNC mode

            Jdk - 1.8.40

            springboot - 1.5.3 Release

            Hibernate Entity Manager - 5.2.12 final

            Hibernate JAP 2.0 API - 1.0.1 Final

            Apache Derby Client  - 10.14.1.0

             

            Database - Apache Derby ( network mode )

             

            <?xml version="1.0" encoding="UTF-8" ?>

            <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"

                version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">

              

                  <persistence-unit name="ROUTING_RULES" transaction-type="RESOURCE_LOCAL">

                    <class>xxx.xxx.xx.BaseRuleEntity</class>

                    <class>xxx.xxx.xx..RoutingRule</class>

                    <class>xxx.xxx.xx..Group</class>

                    <class>xxx.xxx.xx..GroupRule</class>

                    <class>xxx.xxx.xx..GroupRuleId</class>

                    <exclude-unlisted-classes>true</exclude-unlisted-classes>

                    <properties>

            <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver" />

            <property name="javax.persistence.jdbc.url"  value="jdbc:derby://localhost:1527/rulesdb" />

            <property name="javax.persistence.jdbc.user" value="" />

            <property name="javax.persistence.jdbc.password" value="" />

            <property name="javax.persistence.schema-generation.database.action" value="none"/>

            </properties>

                </persistence-unit>

             

             

                <persistence-unit name="GROUPS" transaction-type="RESOURCE_LOCAL">

                    <class>xxx.xxx.xx..BaseRuleEntity</class>

                    <class>xxx.xxx.xx..RoutingRule</class>

                    <class>xxx.xxx.xx..Group</class>

                    <class>xxx.xxx.xx..GroupRule</class>

                    <class>xxx.xxx.xx..GroupRuleId</class>

                     <exclude-unlisted-classes>true</exclude-unlisted-classes>

                    <properties>

            <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver" />

            <property name="javax.persistence.jdbc.url"  value="jdbc:derby://localhost:1527/rulesdb" />

            <property name="javax.persistence.jdbc.user" value="" />

            <property name="javax.persistence.jdbc.password" value="" />

            <property name="javax.persistence.schema-generation.database.action" value="none"/>

            </properties>

                </persistence-unit>

             

            BaseRuleEntity

            @MappedSuperclass

            public abstract class BaseRuleEntity implements Serializable{

             

            private static final long serialVersionUID = -4784557672503387600L;

             

            @Id

            @Column(name="ID")

            protected long Id;

             

            @Column(name="A")

            protected String a;

             

            @Column(name="B")

            protected String b;

             

            @Column(name="C")

            protected String c;

             

            ... setters / getters removed

            }

             

            RoutingRule Class

             

            @Entity

            @Table(name="ROUTING_RULES")

            public class RoutingRule extends BaseRuleEntity {

             

            private static final long serialVersionUID = 5661912585688125323L;

             

            @Transient

            private XXXX yyyy;

             

            public RoutingRule(){

             

            }

             

            @PostLoad

            public void init(){

            try{

             

            this.yyyy = new XXXX(a,b,c)

             

            }catch(Exception e){

            e.printStackTrace();

            }

            }

             

            public XXXX getYYYY(){

            return this.yyyy;

            }

             

            /* (non-Javadoc)

            * @see java.lang.Object#hashCode()

            */

            @Override

            public int hashCode() {

            final int prime = 31;

            int result = super.hashCode();

            result = prime * result + a.hashCode();

            result = prime * result + b.hashCode();

            result = prime * result + c.hashCode();

            return result;

            }

             

             

            /* (non-Javadoc)

            * @see java.lang.Object#equals(java.lang.Object)

            */

            @Override

            public boolean equals(Object obj) {

            if (this == obj) {

            return true;

            }

            if (!super.equals(obj)) {

            return false;

            }

            if (!(obj instanceof RoutingRule)) {

            return false;

            }

            RoutingRule other = (RoutingRule) obj;

             

            if (routingRule == null) {

            if (other.routingRule != null) {

            return false;

            }

            } else if (!a.equals(other.a) || !b.equals(other.b) || !c.equals(other.c) ) {

            return false;

            }

            return true;

            }

            }

             

            Group Class

            @Entity

            @Table(name="GROUP_DEFINITION")

            public class Group extends BaseRuleEntity {

             

            private static final long serialVersionUID = 6465683385425103260L;

             

            @Transient

            private XXXX yyyy;

             

            @OneToMany(mappedBy="group",orphanRemoval = true)

            private Set<GroupRule> groupRules = new HashSet<GroupRule>(0);

             

            public Group(){

             

            }

             

            public Group(String a,String b,String c){

            init();

            }

             

            @PostLoad

            public void init(){

            try{

            this.yyyy = new XXXX(a,b,c);

             

            }catch(Exception e){

            e.printStackTrace();

            }

            }

             

            public Rule getYYYY(){

            return this.yyyy;

            }

             

            public Set<GroupRule> getGroupRules() {

            return groupRules;

            }

             

            public void setGroupRules(Set<GroupRule> groupRules) {

            this.groupRules = groupRules;

            }

             

            public GroupRule addRule(RoutingRule rule,int rulePriority){

            GroupRule groupRule = new GroupRule();

            groupRule.setRule(rule);

            groupRule.setRule_Id(rule.Id);

            groupRule.setGroup(this);

            groupRule.setGroup_Id(Id);

            groupRule.setRulePriority(rulePriority);

            this.groupRules.add(groupRule);

            return groupRule;

            }

             

            public GroupRule removeRule(RoutingRule rule){

            GroupRule groupRuleToRemove = null;

            for(GroupRule groupRule : groupRules){

            if(groupRule.getRule().Id ==  rule.Id){

            groupRuleToRemove = groupRule;

            break;

            }

            }

            if(groupRuleToRemove == null) return null;

            groupRules.remove(groupRuleToRemove);

            return groupRuleToRemove;

            }

             

             

            /* (non-Javadoc)

            * @see java.lang.Object#hashCode()

            */

            @Override

            public int hashCode() {

            final int prime = 31;

            int result = 1;

            result = prime * result + a.hashCode();

            result = prime * result + b.hashCode();

            result = prime * result + c.hashCode();

            return result;

            }

             

             

            /* (non-Javadoc)

            * @see java.lang.Object#equals(java.lang.Object)

            */

            @Override

            public boolean equals(Object obj) {

            if (this == obj) {

            return true;

            }

            if (obj == null) {

            return false;

            }

            if (!(obj instanceof Group)) {

            return false;

            }

            Group other = (Group) obj;

             

             

            if (groupRules == null) {

            if (other.groupRules != null) {

            return false;

            }

            } else if (!a.equals(other.a)|| !b.equals(other.b) || !c.equals(other.c)) {

            return false;

            }

            return true;

            }

             

            public String toString(){

            if(a== null || b== null || c== null)

            return System.identityHashCode(this)+"";

            else

            return "Group ["+a+"]["+b+"]["+c+"] :: "+System.identityHashCode(this);

            }

             

            GroupRule Class

             

            @Entity

            @Table(name="GROUP_RULE")

            @IdClass(GroupRuleId.class)

            public class GroupRule implements Serializable{

             

            private static final long serialVersionUID = -644168055906255069L;

             

            @Id

            @Access(AccessType.PROPERTY)

            private long group_Id;

             

            @Id

            @Access(AccessType.PROPERTY)

            private long rule_Id;

             

            @ManyToOne

            @JoinColumn(name="GROUP_ID", insertable=false, updatable=false)

            private Group group;

             

            @ManyToOne

            @JoinColumn(name="RULE_ID", insertable=false, updatable=false)

            private RoutingRule rule;

             

            @Column(name="RULE_PRIORITY")

            private int rulePriority;

             

            ...getters & setters

            }

             

            GroupRuleId class

             

            @Embeddable

            public class GroupRuleId implements Serializable{

             

            private static final long serialVersionUID = -2060339638799835193L;

             

            private long group_Id;

             

             

            private long rule_Id;

             

            public GroupRuleId(){

             

            }

             

            ...getters & setters

            }