scala 和 spring 数据 neo4j - 没有为字段配置 FieldAccessor [英] scala and spring data neo4j - No FieldAccessor configured for field

查看:29
本文介绍了scala 和 spring 数据 neo4j - 没有为字段配置 FieldAccessor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 spring-data-neo4j 库开发 Scala 项目.

I am developing scala project using spring-data-neo4j library.

首先,这是我的`build.sbt

Firstly, here is my `build.sbt

name := """scheduling-backend"""

version := "1.0"

scalaVersion := "2.10.2"

resolvers += "spray repo" at "http://repo.spray.io"

resolvers += "spray nightlies" at "http://nightlies.spray.io"

resolvers += "SpringSource Milestone Repository" at "http://repo.springsource.org/milestone"

resolvers += "Neo4j Cypher DSL Repository" at "http://m2.neo4j.org/content/repositories/releases"

libraryDependencies ++= Seq(
  "com.typesafe.akka"  %% "akka-actor"       % "2.2.0",
  "com.typesafe.akka"  %% "akka-slf4j"       % "2.2.0",
  "ch.qos.logback"      % "logback-classic"  % "1.0.13",
  "io.spray"            % "spray-can"        % "1.2-20130712",
  "io.spray"            % "spray-routing"    % "1.2-20130712",
  "io.spray"           %% "spray-json"       % "1.2.3",
  "org.specs2"         %% "specs2"           % "1.14"         % "test",
  "io.spray"            % "spray-testkit"    % "1.2-20130712" % "test",
  "com.typesafe.akka"  %% "akka-testkit"     % "2.2.0"        % "test",
  "com.novocode"        % "junit-interface"  % "0.7"          % "test->default",
  "org.springframework.scala" % "spring-scala" % "1.0.0.M2",
  "org.springframework.data" % "spring-data-neo4j" % "2.3.3.RELEASE",
  "org.springframework.data" % "spring-data-neo4j-rest" % "2.3.3.RELEASE",
  "javax.validation" % "validation-api" % "1.1.0.Final",
  "com.github.nscala-time" %% "nscala-time" % "0.8.0"
)

scalacOptions ++= Seq(
  "-unchecked",
  "-deprecation",
  "-Xlint",
  "-Ywarn-dead-code",
  "-language:_",
  "-target:jvm-1.7",
  "-encoding", "UTF-8"
)

testOptions += Tests.Argument(TestFrameworks.JUnit, "-v")

现在,这是我试图坚持的实体

now, here is my entity which I am trying to persist

trait EntityID {
  @GraphId
  var id: java.lang.Long = _
}

@NodeEntity
class Leg() extends EntityID{

  var name: String = _
  var superName:String = _
  @GraphProperty(propertyType = classOf[java.lang.Long])
  var date: DateTime = _
}

以及我用来测试一切的代码:

and the code which I am using to test everything:

package persistence

import org.junit.runner.RunWith
import org.specs2.runner._
import org.springframework.context.ApplicationContext
import org.specs2.mutable.Specification
import org.springframework.context.support.ClassPathXmlApplicationContext
import org.neo4j.graphdb.GraphDatabaseService
import org.joda.time.DateTime

@RunWith(classOf[JUnitRunner])
class SpringDataTest extends Specification {
  "SpringData should" >> {

    "work" in {
      val ctx2: ApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml")
      val repository = ctx2.getBean(classOf[LegRepository])
      val oldLeg = new Leg
      oldLeg.date = DateTime.now
      oldLeg.name = "newName"
      oldLeg.superName = "asd"
      repository.save(oldLeg)
      val newLeg = repository.findOne(5)
      newLeg.name must beEqualTo("newName")
      success
    }
  }


}

现在,当我尝试持久化我的对象时,我正在获取日志:

now, when I try t persist my object I am getting in logs:

19:46:57.288 [specs2.DefaultExecutionStrategy1] INFO  o.s.d.n.f.DelegatingFieldAccessorFactory - No FieldAccessor configured for field: class org.joda.time.DateTime date rel: false idx: false

所以似乎访问 DateTime 属性肯定有问题,但有趣的是,当我使用 Web 界面查看数据库时,保存了 String 属性.

so it seems there must be some problem with accessing DateTime property, but it is interesting that String properties are saved, when I look at the database using web interface.

我还为 DateTime 类型创建了转换器到 Long:

I also created converter for DateTime type to Long:

package persistence

import org.springframework.core.convert.converter.Converter
import org.joda.time.DateTime
import java.lang

class DateTimeConverter extends Converter[DateTime, java.lang.Long]{
  override def convert(source: DateTime): lang.Long = source.getMillis
}

但它没有帮助'

请求的 .xml 配置

requested .xml config

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/data/neo4j
            http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd">

    <context:annotation-config/>

    <bean id="graphDatabaseService" class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
        <constructor-arg value="http://localhost:7474/db/data/" index="0"/>
    </bean>

    <neo4j:config graphDatabaseService="graphDatabaseService"/>
    <neo4j:repositories base-package="persistence"/>

    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="persistence.DateTimeConverter"/>
            </set>
        </property>
    </bean>

</beans>

推荐答案

我们需要一个双向转换器.你的方向只有一个.

We need a converter in both directions. Yours is only in one direction.

在 java.util.Date 的 SDN 示例中:

In the SDN example for java.util.Date:

public void addConverters(ConversionService service) {
    if (service instanceof ConverterRegistry) {
        ConverterRegistry registry = (ConverterRegistry) service;
        registry.addConverter(new DateToStringConverter());
        registry.addConverter(new DateToLongConverter());
        registry.addConverter(new StringToDateConverter());
        registry.addConverter(new NumberToDateConverter());
        registry.addConverter(new EnumToStringConverter());
        registry.addConverterFactory(new StringToEnumConverterFactory());
    } else {
        throw new IllegalArgumentException("conversionservice is no ConverterRegistry:" + service);
    }
}


public static class DateToLongConverter implements Converter<Date, Long> {

    @Override
    public Long convert(Date source) {
        return source.getTime();
    }
}

public static class NumberToDateConverter implements Converter<Number, Date> {

    @Override
    public Date convert(Number source) {
        return new Date(source.longValue());
    }
}

检查如下

public boolean isSerializablePropertyField(final ConversionService conversionService) {
    if (isRelationship()) return false;
    final Class<?> type = getType();
    if (getTypeInformation().isCollectionLike()) {
        return isConvertible(conversionService, getComponentType());
    }
    return isConvertible(conversionService, type);
}

private boolean isConvertible(ConversionService conversionService, Class<?> type) {
    return conversionService.canConvert(type, propertyType) && conversionService.canConvert(propertyType, type);
}

这篇关于scala 和 spring 数据 neo4j - 没有为字段配置 FieldAccessor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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