Scala 中的 Neo4j OGM 示例 [英] Neo4j OGM example with Scala

查看:52
本文介绍了Scala 中的 Neo4j OGM 示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了 Luanne 的文章中提到的例子 Spring Data Neo4j 4 在 Scala 中的精髓.代码可以在 neo4j-ogm-scala 存储库中找到.

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()
  }
}

问题是没有任何内容保存到 Neo4j.你能指出我代码中的问题吗?

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

谢谢,

马诺杰.

推荐答案

Scala 的 Long 是 Value 类的一个实例.显式引入了值类以避免分配运行时对象.因此,在 JVM 级别,Scala 的 Long 等效于 Java 的原始 long,这就是它具有原始类型签名 J 的原因.因此它不能为空,并且不应用作 graphId.尽管 Scala 大多会在它自己的 Long 和 Java 的 Long 类之间进行自动装箱,但这不适用于声明,仅适用于对这些对象的操作.

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.

这篇关于Scala 中的 Neo4j OGM 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆