5 Replies Latest reply on Oct 8, 2014 7:41 AM by rvansa

    Entity id value must equal to key of cache entry

    sowjanya86

      Hi, I am using jpa 2.0 with infinispan cache but i have stucked with an error.

       

      I have my pojo class

       

       

      import java.util.Date;

      import javax.persistence.Column;

      import javax.persistence.Entity;

      import javax.persistence.GeneratedValue;

      import javax.persistence.Id;

      import javax.persistence.Lob;

      import javax.persistence.NamedQuery;

      import javax.persistence.Table;

      import javax.persistence.Temporal;

      import javax.persistence.TemporalType;

       

       

        import org.hibernate.annotations.Cache;

      import org.hibernate.annotations.CacheConcurrencyStrategy;

       

        @Entity

        @Table (name="details")

        @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)

        public class Details {

        @Id

        private int userId;

        @Column

        private String userName;

       

        @Temporal (TemporalType.DATE)

        private Date joindate = new Date();

       

        private String Address;

        @Lob

        private String description;

        public Date joindate() {

        return joindate;

        }

        public void setJoindate(Date joindate) {

        this.joindate = joindate;

        }

        public String getAddress() {

        return Address;

        }

        public void setAddress(String address) {

        Address = address;

        }

        public String getDescription() {

        return description;

        }

        public void setDescription(String description) {

        this.description = description;

        }

        public int getUserId(){

        return userId;

        }

        public void setUserId(int userId) {

        this.userId = userId;

        }

        public String getUserName() {

        return userName;

        }

        public void setUserName(String userName) {

        this.userName = userName;

        }

        }

       

      and the main class

       

      import org.infinispan.Cache;

      import org.infinispan.configuration.cache.Configuration;

      import org.infinispan.configuration.cache.ConfigurationBuilder;

      import org.infinispan.manager.DefaultCacheManager;

      import org.infinispan.manager.EmbeddedCacheManager;

      import org.infinispan.persistence.jpa.configuration.JpaStoreConfigurationBuilder;

      import org.jpa.infinispan.Details;

       

       

      public class Main {

        public static void main(String[] args){

        Configuration builder = new ConfigurationBuilder().persistence().addStore(JpaStoreConfigurationBuilder.class).persistenceUnitName("myPersistenceUnit").entityClass(Details.class)

          .build();

          builder.invocationBatching().enabled();

          EmbeddedCacheManager cacheManager = new DefaultCacheManager(builder);

          cacheManager.defineConfiguration("userCache", builder);

          Cache<String, Details> userCache = cacheManager.getCache("userCache");

          userCache.put("zac", new Details());

          userCache.put("newstreet", new Details());

          userCache.put("loged", new Details());

          System.out.println(userCache.toString());

        }

      }


      I am having the following error i am unable to resolve it can anyone one help me out please.

       

      log4j:WARN No appenders could be found for logger (org.jboss.logging).

      log4j:WARN Please initialize the log4j system properly.

      log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

      Exception in thread "main" org.infinispan.persistence.jpa.JpaStoreException: Entity id value must equal to key of cache entry: key = [zac], id = [0]

        at org.infinispan.persistence.jpa.JpaStore.write(JpaStore.java:342)

        at org.infinispan.persistence.manager.PersistenceManagerImpl.writeToAllStores(PersistenceManagerImpl.java:456)

        at org.infinispan.interceptors.CacheWriterInterceptor.storeEntry(CacheWriterInterceptor.java:336)

        at org.infinispan.interceptors.CacheWriterInterceptor.visitPutKeyValueCommand(CacheWriterInterceptor.java:170)

        at org.infinispan.commands.write.PutKeyValueCommand.acceptVisitor(PutKeyValueCommand.java:71)

        at org.infinispan.interceptors.base.CommandInterceptor.invokeNextInterceptor(CommandInterceptor.java:98)

        at org.infinispan.interceptors.CacheLoaderInterceptor.visitPutKeyValueCommand(CacheLoaderInterceptor.java:113)

        at org.infinispan.commands.write.PutKeyValueCommand.acceptVisitor(PutKeyValueCommand.java:71)

        at org.infinispan.interceptors.base.CommandInterceptor.invokeNextInterceptor(CommandInterceptor.java:98)

        at org.infinispan.interceptors.EntryWrappingInterceptor.invokeNextAndApplyChanges(EntryWrappingInterceptor.java:376)

        at org.infinispan.interceptors.EntryWrappingInterceptor.setSkipRemoteGetsAndInvokeNextForDataCommand(EntryWrappingInterceptor.java:464)

        at org.infinispan.interceptors.EntryWrappingInterceptor.visitPutKeyValueCommand(EntryWrappingInterceptor.java:177)

        at org.infinispan.commands.write.PutKeyValueCommand.acceptVisitor(PutKeyValueCommand.java:71)

        at org.infinispan.interceptors.base.CommandInterceptor.invokeNextInterceptor(CommandInterceptor.java:98)

        at org.infinispan.interceptors.locking.AbstractLockingInterceptor.visitPutKeyValueCommand(AbstractLockingInterceptor.java:48)

        at org.infinispan.commands.write.PutKeyValueCommand.acceptVisitor(PutKeyValueCommand.java:71)

        at org.infinispan.interceptors.base.CommandInterceptor.invokeNextInterceptor(CommandInterceptor.java:98)

        at org.infinispan.interceptors.base.CommandInterceptor.handleDefault(CommandInterceptor.java:112)

        at org.infinispan.commands.AbstractVisitor.visitPutKeyValueCommand(AbstractVisitor.java:33)

        at org.infinispan.commands.write.PutKeyValueCommand.acceptVisitor(PutKeyValueCommand.java:71)

        at org.infinispan.interceptors.base.CommandInterceptor.invokeNextInterceptor(CommandInterceptor.java:98)

        at org.infinispan.interceptors.CacheMgmtInterceptor.updateStoreStatistics(CacheMgmtInterceptor.java:148)

        at org.infinispan.interceptors.CacheMgmtInterceptor.visitPutKeyValueCommand(CacheMgmtInterceptor.java:134)

        at org.infinispan.commands.write.PutKeyValueCommand.acceptVisitor(PutKeyValueCommand.java:71)

        at org.infinispan.interceptors.base.CommandInterceptor.invokeNextInterceptor(CommandInterceptor.java:98)

        at org.infinispan.interceptors.InvocationContextInterceptor.handleAll(InvocationContextInterceptor.java:108)

        at org.infinispan.interceptors.InvocationContextInterceptor.handleDefault(InvocationContextInterceptor.java:71)

        at org.infinispan.commands.AbstractVisitor.visitPutKeyValueCommand(AbstractVisitor.java:33)

        at org.infinispan.commands.write.PutKeyValueCommand.acceptVisitor(PutKeyValueCommand.java:71)

        at org.infinispan.interceptors.InterceptorChain.invoke(InterceptorChain.java:333)

        at org.infinispan.cache.impl.CacheImpl.executeCommandAndCommitIfNeeded(CacheImpl.java:1490)

        at org.infinispan.cache.impl.CacheImpl.putInternal(CacheImpl.java:968)

        at org.infinispan.cache.impl.CacheImpl.put(CacheImpl.java:960)

        at org.infinispan.cache.impl.CacheImpl.put(CacheImpl.java:1560)

        at org.infinispan.cache.impl.CacheImpl.put(CacheImpl.java:231)

        at org.jpa.infinispan.Main.main(Main.java:21)

        • 1. Re: Entity id value must equal to key of cache entry
          rvansa

          It's exactly what the exception says: when using JpaStore, you have to keep the key in put(key, value) same as entity @Id. Instead of

           

          userCache.put("zac", new Details());

           

          use

           

          Details d = new Details();

          d.setUserName("zac");

          userCache.put(d.userId, d);

           

          Also, remember that the cache stores always a copy of the object you pass into that, therefore:

           

          Foo foo = new Foo("bar");

          cache.put("foo", foo);

          System.out.println(cache.get("foo").getBar()) // writes "bar"

          foo.setBar("goo");

          System.out.println(cache.get("foo").getBar()) // writes "bar" again, not "goo"

          cache.put("foo", foo);

          System.out.println(cache.get("foo").getBar()) // now it writes "goo"

           

          That's why inserting empty Details() does not make much sense, and auto-generating the userId either.

          • 2. Re: Entity id value must equal to key of cache entry
            sowjanya86

            Thanks a lot for the response  the answer it helped me. But i have done another change to my code.

            Cache<Object, Object> userCache = cacheManager.getCache("userCache").getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE, Flag.CACHE_MODE_LOCAL);

            I think the issue is with the default cache entry and the entity id. I don't know the correct issue the above change worked fine.

            • 3. Re: Entity id value must equal to key of cache entry
              rvansa

              With those flags, you're simply not storing into JPA cache store, and not replicating to another nodes. The question is, what do you want, and why have you set JPA persistence in the first place?

               

              Infinispan does not require the key to match to the @Id field of value, but JPA stores do.

              • 4. Re: Entity id value must equal to key of cache entry
                sowjanya86

                Thanks for the suggestion. I am trying to use jpa with infinispan cahe. for Update or delete values in database using jpa. And also it need to be updated in Infinispan cache.

                My requirement is any changes in database need to be effected in infinispan. 

                Radim Vansa wrote:

                 

                The question is, what do you want, and why have you set JPA persistence in the first place?

                Because i am not using jdbc cache loaders for the database connection in infinispan. instead of that i am using myPersistenceUnit for the connections.

                is Their any solution for this? any answer will be a great help for me.

                • 5. Re: Entity id value must equal to key of cache entry
                  rvansa

                  I should rather ask 'Do you need the persistence at all, or just preload the DB in the beginning?'

                   

                  If you change the entry directly in DB, this change won't be reflected in Infinispan cache. Infinispan cache should be the only one accessing the DB.

                   

                  Anyway, I've already said what you need to do: when inserting entry into cache, use the field tagged with @Id as key.