简体   繁体   中英

Neo4jSession won't recognize NodeEntity as persistable class in Spring MVC module

I am working on a multi-module project, which consists of

  • rest - a Spring MVC module, which contains @RestControllers etc.
  • core - a module which utilizes Spring Data Neo4J 4 and contains all my @Services , @Repositories etc.

All unit tests in the core module work as expected, and persist successfully all domain entities

But when invoking any of the @RestController methods the domain entities fail to be persisted, and I get the following message :

INFO org.neo4j.ogm.session.Neo4jSession - server.core.domain.nodes.User is not an instance of a persistable class

core module config class

@EnableTransactionManagement
@Configuration
@ImportResource({"classpath:spring-config.xml"})
@EnableNeo4jRepositories(basePackages = "server.core.repositories")
public class MyNeo4jConfiguration extends Neo4jConfiguration {


    @Override
    public Neo4jServer neo4jServer() {
        return new RemoteServer(url, user, pass);
    }


    @Override
    public SessionFactory getSessionFactory() {
        return new SessionFactory("server.core.domain");
    }
}

web.xml

<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Rest</display-name>

    <servlet>
        <servlet-name>rest</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            WEB-INF/rest-servlet.xml
        </param-value>
    </context-param>

    <servlet-mapping>
        <servlet-name>rest</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

WEB-INF/rest-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <bean class="server.core.MyNeo4jConfiguration" />
    <context:component-scan base-package="rest"/>
    <mvc:annotation-driven />
    <context:annotation-config />



    <bean name="jsonTemplate" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
</beans>

Controller

@RestController
 public class UserController {
    @Autowired
    IUserService userService;

    @RequestMapping(value = "/create-user", method = RequestMethod.GET)
    public ResponseEntity<?> createUser() {
        User user = userService.createOrUpdateUser(new User("john", "doe"));  //does not persist user
        HttpHeaders httpHeaders = new HttpHeaders();
        return new ResponseEntity<>(user, httpHeaders, HttpStatus.ACCEPTED);
    }
}

UserService.java

@Service("userService")
public class UserService implements IUserService {

    @Autowired
    UserRepository userRepository;  // extends GraphRepository

    @Override
    public User createOrUpdateUser(User user){
        user.setStatus(AppEnums.NodeStatus.ACTIVE);
        return userRepository.save(user);
    }
}
INFO org.neo4j.ogm.session.Neo4jSession - server.core.domain.nodes.User is not an instance of a persistable class

Is indicating that the package path where User class lives WAS NOT provided (or scanned) by the SessionFactory constructor @Bean.

So make sure in your Rest app you are initialization the SessionFactory that you posted in the stackoverflow question... with the correct/expected root package path.

Perform a File/String search for "SessionFactory" on your entire project to verify.


Also, Here is the java doc of the SessionFactory Constructor which indicates that one can pass multiple paths as args and all child packages will also be scanned. Moreover, one can pass specific Classes into the args...

@Override
public SessionFactory getSessionFactory() {
    return new SessionFactory("server.core.domain");
}

org.neo4j.ogm.session.SessionFactory.SessionFactory(String... packages)

/**
 * Constructs a new {@link SessionFactory} by initialising the object-graph mapping meta-data from the given list of domain
 * object packages.
 * <p>
 * The package names passed to this constructor should not contain wildcards or trailing full stops, for example,
 * "org.springframework.data.neo4j.example.domain" would be fine.  The default behaviour is for sub-packages to be scanned
 * and you can also specify fully-qualified class names if you want to cherry pick particular classes.
 * </p>
 *
 * @param packages The packages to scan for domain objects
 */

Also, there was a bug about this in 4.0.0.M1 but according to the jira the claim is that it has been fixed as of 4.0.0.RC1

https://jira.spring.io/browse/DATAGRAPH-657

This might still be a bug... for testing purposes try to pass the following arguments to your SessionFactory.

"server.core.domain.nodes"

or

server.core.domain.nodes.User

See if that makes the error go away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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