0

My current configuration

class Neo4jSessionFactory (implicit inj: Injector) extends Injectable  {
  val neo4jHost           = inject [String] (identified by "neo4j.server.host")
  val neo4jPort           = inject [Integer] (identified by "neo4j.server.port")
  val neo4jDb             = inject [String] (identified by "neo4j.server.db")
  val neo4jUser           = inject [String] (identified by "neo4j.server.user")
  val neo4jPass           = inject [String] (identified by "neo4j.server.pass")

  def neo4jServer() : Neo4jServer = {
    return new RemoteServer("http://localhost:7474", "neo4j", "neo4j");
  }

  def getNeo4jConfiguration : Configuration = {
    var config = new Configuration
    config.driverConfiguration()
          .setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver")
          .setURI("http://neo4j:neo4j@localhost:7474")
    return config
  }

  def getNeo4jSessionFactory: SessionFactory = {
    System.setProperty("username", "neo4j")
    System.setProperty("password", "neo4j")
    var config = getNeo4jConfiguration
    return new SessionFactory(getNeo4jConfiguration,"domain")
  }

 def getNeo4jTemplate : Neo4jOperations = {
     return new Neo4jTemplate(getNeo4jSession)
 }

 def getNeo4jSession : Session = {
    return getNeo4jSessionFactory.openSession
  }

and my Controller class

class Application (implicit inj : Injector) extends Controller with Injectable {
  val neo4jService = inject[Neo4jSessionFactory]
  val session = neo4jService.getNeo4jTemplate

  def insertStudentNeo4j(name: String, age: Int) = Action { implicit request =>
    val test = new domain.Student
    test.name=Option(name).getOrElse("default").toString
    test.age=Option(age).getOrElse(0).toInt
    session.save(test)
    // var jason = MyJsonUtil.convertStudentToJsonOrig(test)
    Ok(Json.obj("Name:" -> name))
  }

when I attempt to save an object to neo4j, it returns NullPointerException and I tried to println(session) and println(test) they were both not null, I still wonder what could be the reason?

this is my Student class

@NodeEntity
class Student extends Entity {
    var name: String = _
    var age: Int = _
    var txt: String = _

  def this(name: String, age: Int, txt: String) = {
    this()
    this.name=name
    this.age=age
    this.txt=txt
  }
}

and my entity class which contain a graphId whom value not to be assigned.

abstract class Entity {
  @GraphId
  @BeanProperty
  var graph_id: java.lang.Long = _

  override def equals(o: Any): Boolean = o match {
    case other: Entity => other.graph_id.equals(this.graph_id)
    case _ => false
  }
}
kenlz
  • 461
  • 7
  • 22
  • Do you have a stacktrace? – Luanne May 27 '16 at 11:04
  • Unfortunately I don't. It just shows as Execution Error and indicate the exception was thrown from `session.save(test)`. however I tried saving it directly `neo4jService.getNeo4jTemplate.save(test)` and it worked. Perhaps a session cannot be instantiated twice? – kenlz May 27 '16 at 15:06
  • Hmm, there's no reason why you can't instantiate more than one session. There's also no advantage if you instantiate the SessionFactory multiple times- it just results in re-scanning packages. Recommend you create it only once and use that to produce sessions – Luanne Jun 06 '16 at 02:30

0 Answers0