简体   繁体   English

使用Scala的Neo4j OGM示例

[英]Neo4j OGM example with Scala

I tried the example mentioned in Luanne's article The essence of Spring Data Neo4j 4 in Scala. 我尝试了在Luanne的文章中提到的例子Scala中的Spring Data Neo4j 4的本质 The code can be found in the neo4j-ogm-scala repository. 代码可以在neo4j-ogm-scala存储库中找到。

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. 问题是没有任何东西被保存到Neo4j。 Can you please point out the problem in my code? 你能在我的代码中指出问题吗?

Thanks, 谢谢,

Manoj. 马诺。

Scala's Long is an instance of a Value class. Scala的Long是Value类的一个实例。 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 . 因此,在JVM级别,Scala的Long等同于Java的原始long ,这就是它具有原始类型签名J It cannot be therefore be null, and should not be used as a graphId . 因此它不能为null,不应该用作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主要在自己的Long和Java的Long类之间进行自动装箱,但这不适用于声明,只适用于对这些对象的操作。

The @GraphId isn't being picked up on your entities. @GraphId未被您的实体选中。 I have zero knowledge of Scala but it looks like the scala long isn't liked much by the OGM; 我对Scala一无所知,但看起来像OGM不太喜欢Scala; var id: java.lang.Long = _ works fine. var id: java.lang.Long = _工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM