spring-data-neo4j 4.2.4release文档概要
Neo4j是一种开源的NoSQL图数据库,将数据以图(把一个个实体当作节点,连接节点的边表示节点间的关系)的形式保存,Neo4j也支持ACID事务管理。关系型数据库数据访问采用的是ORM(对象关系映射),而Neo4j数据库采用的是OGM(对象图形映射, Object Graph Mapper)。
与SDN相匹配的jar包版本
使用Spring Data Neo4j 4.2.x版的需求:
JDK1.8及以上版本
Neo4j Graph DataBase 2.3.x及以上版本
Spring Framework 4.3.9RELEASE及以上版本
使用OGM时确保版本在2.1.1+RELEASE
版本控制
Spring Data Neo4j 4.2及以后的版本的配置,更加简便。不需要在配置类中继承Neo4jConfiguration,也不需要定义SessionBean。而是定义SessionFactory和Neo4jTransactionManager的对象bean。连接Neo4j时,SDN创建session(org.neo4j.ogm.session.Session)实例时,需要声明SessionFactory.当SessionFactory被创建,它设置了OGM的元数据,使用它来创建所有的Session,同时packages to scan for domain object 元数据需要提供给SessionFactory的构造函数。,@EnableTransactionManagement是用来申明Neo4jTransactionManager的,配置SDN的事物,配置SessionFactory Bean是为了申明OGM的配置,如下图:
使用JPA代理实现repository的config
声明一个Spring Data JPA的依赖
neo4j应用
neo4j的driver有三种:Embedd(内嵌式),HTTP以及二进制协议Bolt
OGM(Object Graph Mapper)neo4j数据库的节点与java实体建立映射关系
SDN在顶端为OGM提供语法以及代码,建立基于Spring的Neo4j/OGM的应用
添加依赖
spring-data-neo4j
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>{version}</version>
</dependency>
添加dirver的依赖,三种:embedded-driver,http-driver,bolt-driver
<!-- add this dependency if you want to use the embedded driver -->
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-embedded-driver</artifactId>
<version>{ogm-version}</version>
</dependency>
<!-- add this dependency if you want to use the HTTP driver -->
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-http-driver</artifactId>
<version>{ogm-version}</version>
</dependency>
<!-- the neo4j-ogm-test jar provides access to the http and embedded drivers for testing purposes -->
<dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-ogm-test</artifactId> <version>${neo4j-ogm.version}</version> <type>test-jar</type> <scope>test</scope> </dependency>
<dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-kernel</artifactId> <version>${neo4j.version}</version> <type>test-jar</type> </dependency>
<dependency> <groupId>org.neo4j.app</groupId> <artifactId>neo4j-server</artifactId> <version>${neo4j.version}</version> <type>test-jar</type> </dependency>
<dependency> <groupId>org.neo4j.test</groupId> <artifactId>neo4j-harness</artifactId> <version>${neo4j.version}</version> <scope>test</scope> </dependency>
driver的配置,SDN会自动扫描ogn.properties文件,也可以通过java Configuration来配置,其中<driver>对应依赖中申明的driver,<uri>对应对应neo4j数据库链接地址(http://username:password@localhost:7474)如下:
import org.neo4j.ogm.config.Configuration;
@Bean
public Configuration configuration() {
Configuration config = new Configuration();
config .driverConfiguration() .setDriverClassName("org.neo4j.ogm.drivers.<driver>.driver.<driver>Driver") .setURI("<uri>");
return config;
}
@Bean
public SessionFactory sessionFactory() {
return new SessionFactory(configuration(), <packages> );
}
Neo4j OGM支持
OGM将图数据库中的节点和关系与域模型中的对象和引用映射起来,节点与对象实例映射起来,而对象引用与关系映射起来。OGM抽象化了数据库,提供一种方便的方式在图数据库持久化域模型(domain model),并且查询不需要使用低级别的驱动。
Session用于驱动对象图映射框架。所有存储库实现都是由Session驱动的。它跟踪对实体及其关系所做的更改,这样做的好处是,只需要保存修改的部分。
Neo4j Repositories使用
@Repository
public interface PersonRepository extends Neo4jRepository<Person, Long> {}
public class MySpringBean {
@Autowired private PersonRepository repo;
...
} // then you can use the repository as you would any other object
Person michael = repo.save(new Person("Michael", 36));
Person dave = repo.load(123);
long numberOfPeople = repo.count();
Mapping Query Results
public interface MovieRepository extends GraphRepository<Movie> {
@Query("MATCH (movie:Movie)-[r:RATING]\->(), (movie)<-[:ACTS_IN]-(actor:Actor) " + "WHERE movie.id={0} " + "RETURN movie as movie, COLLECT(actor) AS 'cast', AVG(r.stars) AS 'averageRating'")
MovieData getMovieData(String movieId);
@QueryResult
public class MovieData {
Movie movie;
Double averageRating;
Set<Actor> cast;
}
}
Neo4j是一种事务型数据库。对所有与SDN交互的方法标定@Transactional
@Service
class UserManagementImpl implements UserManagement {
private final UserRepository userRepository;
private final RoleRepository roleRepository;
@Autowired
public UserManagementImpl(UserRepository userRepository, RoleRepository roleRepository) {
this.userRepository = userRepository;
this.roleRepository = roleRepository;
}
@Transactional
public void addRoleToAllUsers(String roleName) {
Role role = roleRepository.findByName(roleName);
for (User user : userRepository.findAll()) {
user.addRole(role);
userRepository.save(user);
}
}
Driver Configuration
Annotating Entities