Neo4j OGM example with Scala

本秂侑毒 提交于 2019-12-12 12:06:54

问题


I tried the example mentioned in Luanne's article The essence of Spring Data Neo4j 4 in Scala. The code can be found in the neo4j-ogm-scala repository.

package neo4j.ogm.scala.domain

import org.neo4j.ogm.annotation.GraphId;
import scala.beans.BeanProperty
import org.neo4j.ogm.annotation.NodeEntity
import org.neo4j.ogm.annotation.Relationship
import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory;

abstract class Entity {
  @GraphId
  @BeanProperty
  var id: Long = _

  override def equals(o: Any): Boolean = o match {
    case other: Entity => other.id.equals(this.id)
    case _ => false
  }

  override def hashCode: Int = id.hashCode()
}

@NodeEntity
class Category extends Entity {
  var name: String = _

  def this(name: String) {
    this()
    this.name = name
  }
}

@NodeEntity
class Ingredient extends Entity {
  var name: String = _

  @Relationship(`type` = "HAS_CATEGORY", direction = "OUTGOING")
  var category: Category = _

  @Relationship(`type` = "PAIRS_WITH", direction = "UNDIRECTED")
  var pairings: Set[Pairing] = Set()

  def addPairing(pairing: Pairing): Unit = {
    pairing.first.pairings +(pairing)
    pairing.second.pairings +(pairing)
  }

  def this(name: String, category: Category) {
    this()
    this.name = name
    this.category = category
  }
}

@RelationshipEntity(`type` = "PAIRS_WITH")
class Pairing extends Entity {
  @StartNode
  var first: Ingredient = _

  @EndNode
  var second: Ingredient = _

  def this(first: Ingredient, second: Ingredient) {
    this()
    this.first = first
    this.second = second
  }
}

object Neo4jSessionFactory {
  val sessionFactory = new SessionFactory("neo4j.ogm.scala.domain")

  def getNeo4jSession(): Session = {
    System.setProperty("username", "neo4j")
    System.setProperty("password", "neo4j")
    sessionFactory.openSession("http://localhost:7474")
  }
}

object Main extends App {
  val spices = new Category("Spices")
  val turmeric = new Ingredient("Turmeric", spices)
  val cumin = new Ingredient("Cumin", spices)

  val pairing = new Pairing(turmeric, cumin)
  cumin.addPairing(pairing)

  val session = Neo4jSessionFactory.getNeo4jSession()
  val tx: Transaction = session.beginTransaction()

  try {
    session.save(spices)
    session.save(turmeric)
    session.save(cumin)
    session.save(pairing)
    tx.commit()
  } catch {
    case e: Exception => // tx.rollback()
  } finally {
//    tx.commit()
  }
}

The problem is that nothing gets saved to Neo4j. Can you please point out the problem in my code?

Thanks,

Manoj.


回答1:


Scala’s Long is an instance of a Value class. Value classes were explicitly introduced to avoid allocating runtime objects. At the JVM level therefore Scala's Long is equivalent to Java’s primitive long which is why it has the primitive type signature J. It cannot be therefore be null, and should not be used as a graphId. Although Scala mostly will do auto-boxing between its own Long and Java’s Long class, this doesn’t apply to declarations, only to operations on those objects.




回答2:


The @GraphId isn't being picked up on your entities. I have zero knowledge of Scala but it looks like the scala long isn't liked much by the OGM; var id: java.lang.Long = _ works fine.



来源:https://stackoverflow.com/questions/31989802/neo4j-ogm-example-with-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!