gremlin-scala icon indicating copy to clipboard operation
gremlin-scala copied to clipboard

@id annotation doesn't work after recompilation

Open ddrozdov opened this issue 8 years ago • 9 comments

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.

ddrozdov avatar May 24 '16 21:05 ddrozdov

Sounds like it's related to #133 . IIRC @mpollmeier is working on an alternate shapeless-based (de)serializer to work around the problem.

mikolak-net avatar May 25 '16 09:05 mikolak-net

That's true.

mpollmeier avatar May 25 '16 11:05 mpollmeier

@id annotation is just not working independently of classes order =(

voroninp avatar Sep 03 '18 13:09 voroninp

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 avatar Sep 04 '18 17:09 mpollmeier

@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"

voroninp avatar Sep 05 '18 15:09 voroninp

Hmm, this looks like a regression - it works fine until gremlin-scala 3.3.1.2. I'll have a look.

mpollmeier avatar Sep 07 '18 15:09 mpollmeier

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 avatar Sep 07 '18 15:09 mpollmeier

@mpollmeier Unfortunately, not an option for us. =( We get ids from external system and they are UUIDs

voroninp avatar Sep 10 '18 08:09 voroninp

I'd say you have at least these alternatives:

  1. store them in a separate (indexed) property. That would be my preference, since it works with all graph dbs.
  2. only use the case class marshalling for retrieving them from the graph, and write them using the graph api.

mpollmeier avatar Sep 10 '18 08:09 mpollmeier