gremlin-scala
gremlin-scala copied to clipboard
@id annotation doesn't work after recompilation
I have an issue with the field annotated with @id
.
If the case class declaration goes after the class that calls toCC
, the id
field is not filled in.
If I move the case class to the beginning of the file, everything works well.
It is not a big issue when all your code is in the single file, but when the case classes are in a separate file, the id
is filled depending on the order the sources are compiled - sometimes it works and after the gradle clean
it doesn't.
Sounds like it's related to #133 . IIRC @mpollmeier is working on an alternate shapeless
-based (de)serializer to work around the problem.
That's true.
@id annotation is just not working independently of classes order =(
well that's a bit of a stretch, here's some working examples: https://github.com/mpollmeier/gremlin-scala/blob/master/gremlin-scala/src/test/scala/gremlin/scala/MarshallableSpec.scala
@mpollmeier could you a give me a hint what I am doing wrong? This test fails for me =(
@label("user")
case class UserWithId(@id id: Int, name: String, age: Int)
class GremlinQueriesSpec extends FlatSpec
with ScalaFutures with MustMatchers with BeforeAndAfterAllConfigMap {
behavior of "Gremlin queries"
it must "add vertex with id" in {
val user = new UserWithId(1, "anonymous", 35)
val graph = TinkerGraph.open.asScala
graph + user
val vertex = graph.V(user.id).head().toCC[UserWithId]
vertex must be eq user
}
}
sbt.version=0.13.16
scalaVersion := "2.11.12"
Hmm, this looks like a regression - it works fine until gremlin-scala 3.3.1.2. I'll have a look.
Actually, this was tradeoff based on a design decision back then. I would have preferred if it worked both ways, but IMO it's best to leave id assignment to the graph.
Anyway, due to that tradeoff @id
only works when reading the case class back out of the graph. I will update readme and tests to reflect that.
Here's a working version of your test:
@label("user")
case class UserWithId(name: String, age: Int, @id id: Option[Int] = None)
it must "add vertex" in {
val graph = TinkerGraph.open.asScala
val vertex = graph + new UserWithId("anonymous", 35)
val user = graph.V(vertex.id).toCC[UserWithId].head
println(user) // UserWithId(anonymous,35,Some(0))
}
@mpollmeier Unfortunately, not an option for us. =( We get ids from external system and they are UUID
s
I'd say you have at least these alternatives:
- store them in a separate (indexed) property. That would be my preference, since it works with all graph dbs.
- only use the case class marshalling for retrieving them from the graph, and write them using the graph api.