Impetus / kundera

A JPA 2.1 compliant Polyglot Object-Datastore Mapping Library for NoSQL Datastores.Please subscribe to:
http://groups.google.com/group/kundera-discuss/subscribe
Apache License 2.0
903 stars 233 forks source link

Unable to update counter column in Cassandra #1017

Closed sohelmk closed 6 years ago

sohelmk commented 6 years ago

Hi, We are unable to update a counter column in Cassandra using Kundera. Tested with embedded cassandra. Note that table has counter as well as non-counter column. Not sure if that's the issue Please find all details below:

Mapping:

@Entity @Table(name = "s_ans", schema = "cedb@cassandrapu") class SAnsDO { @EmbeddedId var key: SurvKey = @Column(name = "aggr_choice1") var ch1: Int = 0 @Column(name = "aggr_choice2") var ch2: Int = 0 @Column(name = "aggr_choice3") var ch3: Int = 0 @Column(name = "aggr_choice4") var ch4: Int = 0 @Column(name = "aggr_choice5") var ch5: Int = 0

CQL

CREATE TABLE IF NOT EXISTS s_an (s_id UUID, q_id UUID, aggr_choice1 COUNTER, aggr_choice2 COUNTER, aggr_choice3 COUNTER, aggr_choice4 COUNTER, aggr_choice5 COUNTER, PRIMARY KEY ((s_id,q_id)));

Stack trace -ScalaTest-running-SurveyControllerPlaySpec] ERROR c.c.d.SurveysDAOImpl - Error : com.impetus.kundera.KunderaException: com.impetus.kundera.KunderaException: InvalidRequestException(why:INSERT statements are not allowed on c at com.impetus.kundera.persistence.EntityManagerImpl.merge(EntityManagerImpl.java:210) at com.consultengine.daos.SurveysDAOImpl$.$anonfun$surveySubmission$1(SurveysDAO.scala:92) at com.consultengine.daos.SurveysDAOImpl$.$anonfun$surveySubmission$1$adapted(SurveysDAO.scala:64) at scala.collection.immutable.List.foreach(List.scala:389) at com.consultengine.daos.SurveysDAOImpl$.surveySubmission(SurveysDAO.scala:64) at com.consultengine.services.SurveyService.surveySubmission(SurveyService.scala:61) at com.consultengine.controllers.v1.SurveyController.$anonfun$surveySubmission$1(SurveyController.scala:73) at scala.Function1.$anonfun$andThen$1(Function1.scala:52) at play.api.mvc.ActionBuilderImpl.invokeBlock(Action.scala:482) at play.api.mvc.ActionBuilderImpl.invokeBlock(Action.scala:480) at play.api.mvc.ActionBuilder$$anon$2.apply(Action.scala:419) at com.consultengine.controllers.v1.SurveyControllerPlaySpec.$anonfun$new$6(SurveyControllerPlaySpec.scala:159) at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85) at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83) at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104) at org.scalatest.Transformer.apply(Transformer.scala:22) at org.scalatest.Transformer.apply(Transformer.scala:20) at org.scalatest.WordSpecLike$$anon$1.apply(WordSpecLike.scala:1078) at org.scalatest.TestSuite.withFixture(TestSuite.scala:196) at org.scalatest.TestSuite.withFixture$(TestSuite.scala:195) at org.scalatest.WordSpec.withFixture(WordSpec.scala:1881) at org.scalatest.WordSpecLike.invokeWithFixture$1(WordSpecLike.scala:1076) at org.scalatest.WordSpecLike.$anonfun$runTest$1(WordSpecLike.scala:1088) at org.scalatest.SuperEngine.runTestImpl(Engine.scala:289) at org.scalatest.WordSpecLike.runTest(WordSpecLike.scala:1088) at org.scalatest.WordSpecLike.runTest$(WordSpecLike.scala:1070) at org.scalatest.WordSpec.runTest(WordSpec.scala:1881) at org.scalatest.WordSpecLike.$anonfun$runTests$1(WordSpecLike.scala:1147) at org.scalatest.SuperEngine.$anonfun$runTestsInBranch$1(Engine.scala:396) at scala.collection.immutable.List.foreach(List.scala:389) at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:384) at org.scalatest.SuperEngine.runTestsInBranch(Engine.scala:373) at org.scalatest.SuperEngine.$anonfun$runTestsInBranch$1(Engine.scala:410) at scala.collection.immutable.List.foreach(List.scala:389) at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:384) at org.scalatest.SuperEngine.runTestsInBranch(Engine.scala:379) at org.scalatest.SuperEngine.runTestsImpl(Engine.scala:461) at org.scalatest.WordSpecLike.runTests(WordSpecLike.scala:1147) at org.scalatest.WordSpecLike.runTests$(WordSpecLike.scala:1146) at org.scalatest.WordSpec.runTests(WordSpec.scala:1881) at org.scalatest.Suite.run(Suite.scala:1147) at org.scalatest.Suite.run$(Suite.scala:1129) at org.scalatest.WordSpec.org$scalatest$WordSpecLike$$super$run(WordSpec.scala:1881) at org.scalatest.WordSpecLike.$anonfun$run$1(WordSpecLike.scala:1192) at org.scalatest.SuperEngine.runImpl(Engine.scala:521) at org.scalatest.WordSpecLike.run(WordSpecLike.scala:1192) at org.scalatest.WordSpecLike.run$(WordSpecLike.scala:1190) at com.consultengine.controllers.v1.SurveyControllerPlaySpec.org$scalatestplus$play$BaseOneAppPerSuite$$super$run(SurveyControllerPlay at org.scalatestplus.play.BaseOneAppPerSuite.run(BaseOneAppPerSuite.scala:46) at org.scalatestplus.play.BaseOneAppPerSuite.run$(BaseOneAppPerSuite.scala:41) at com.consultengine.controllers.v1.SurveyControllerPlaySpec.run(SurveyControllerPlaySpec.scala:95) at org.scalatest.Suite.callExecuteOnSuite$1(Suite.scala:1210) at org.scalatest.Suite.$anonfun$runNestedSuites$1(Suite.scala:1257) at scala.collection.IndexedSeqOptimized.foreach(IndexedSeqOptimized.scala:32) at scala.collection.IndexedSeqOptimized.foreach$(IndexedSeqOptimized.scala:29) at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:191) at org.scalatest.Suite.runNestedSuites(Suite.scala:1255) at org.scalatest.Suite.runNestedSuites$(Suite.scala:1189) at org.scalatest.Suites.runNestedSuites(Suites.scala:56) at org.scalatest.Suite.run(Suite.scala:1144) at org.scalatest.Suite.run$(Suite.scala:1129) at com.consultengine.controllers.v1.DBAwareSuite.org$scalatest$BeforeAndAfterAll$$super$run(DBAwareSuite.scala:51) at org.scalatest.BeforeAndAfterAll.liftedTree1$1(BeforeAndAfterAll.scala:213) at org.scalatest.BeforeAndAfterAll.run(BeforeAndAfterAll.scala:210) at org.scalatest.BeforeAndAfterAll.run$(BeforeAndAfterAll.scala:208) at com.consultengine.controllers.v1.DBAwareSuite.run(DBAwareSuite.scala:51) at org.scalatest.tools.Framework.org$scalatest$tools$Framework$$runSuite(Framework.scala:314) at org.scalatest.tools.Framework$ScalaTestTask.execute(Framework.scala:507)

persistence.xml

..................................

com.consultengine.dataobjects.SAnsDO true

karthikprasad13 commented 6 years ago

Hi @sohelmk

Counters are a special case in Cassandra and they are not handled in Kundera after they are redesigned in Cassandra 2.1. You are getting this exception because Kundera constructs INSERT query for persist operations, but counters can only be used with UPDATE queries.

You can use createNativeQuery with custom UPDATE queries as a workaround.

Example:

String updateQuery = "UPDATE counters SET c1 = c1 + 10 WHERE \"userId\" = 'id123'";
Query q = em.createNativeQuery(updateQuery);
q.executeUpdate();

-Karthik

sohelmk commented 6 years ago

Thanks Karthik, will try this. Impressed with Kundera's excellent turnaround time and prompt support. Kudos to Kundera for excellent work!

Regards Sohel Khan