neo4j(1)Introduction to neo4j and sample project
1. Introduction to Graph Database
Node ------relationship -------> property
name=wheel number=4 name=car,color=red,
A Graph --- records data in ---->Nodes -----which have ----> Properties
Nodes ------are organized by -----> Relationships ---- which also have -----> Properties
A Traversal ---- navigates ----> a Graph; it ----identifies ---> Paths ---- which order -----> Nodes
An Index ----maps from ----> Properties ---to either ----> Nodes or Relationships
A Graph Database ---- manages a ----> Graph and ----also manages related ---> Indexs
2. The domain Layer
The User Object
@NodeEntity
public class Userneo4j {
@GraphId
private Long id;
private String firstName;
private String lastName;
@Indexed
private String username;
private String password;
@Fetch @RelatedTo(type = "HAS_ROLENEO4J")
private Roleneo4j roleneo4j;
public Userneo4j() {}
public Userneo4j(String username) {
this.username = username;
}
public Userneo4j(String username, String firstName, String lastName, Roleneo4j roleneo4j) {
this.username = username;
this.firstName = firstName;
this.lastName = lastName;
this.roleneo4j = roleneo4j;
}
public Userneo4j(String username, String password, String firstName, String lastName, Roleneo4j roleneo4j) {
this.username = username;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.roleneo4j = roleneo4j;
}
...snip...
The Role domain:
@NodeEntity
public class Roleneo4j {
@GraphId
private Long id;
private Userneo4j userneo4j;
private Integer role;
public Roleneo4j() {
}
public Roleneo4j(Integer role) {
this.role = role;
}
...snip...
The relationship domain object:
@RelationshipEntity(type = "HAS_ROLENEO4J")
public class Userneo4jRoleneo4jRelationship {
private String description;
@StartNode
private Userneo4j userneo4j;
@EndNode
private Roleneo4j roleneo4j;
...snip...
@NodeEntity annotation is used to turn a POJO class into an entity backed by a node in the graph database. Fields on the entity are by default mapped to properties of the node.
@GraphId
For the simple mapping this is a required field which must be of type Long. It is used by Spring Data Neo4j to store the node or relationship-id to re-connect the entity to the graph.
The @Indexed annotation can be declared on fields that are intended to be indexed by the Neo4j indexing facilities.
@Fetch
To have the collections of relationships being read eagerly ... we have to annotate it with the @Fetch annotation.
@RelatedTo: Connecting node entities
Every field of a node entity that references one or more other node entities is backed by relationships in the graph.
@RelationshipEntity, making them relationship entities. Just as node entities represent nodes in the graph, relationship entities represent relationships.
3. The Service Layer
Init Service to add the init data:
package com.sillycat.easynosql.dao.neo4j.init;
import org.springframework.beans.factory.annotation.Autowired;
import com.sillycat.easynosql.dao.neo4j.model.Roleneo4j;
import com.sillycat.easynosql.dao.neo4j.model.Userneo4j;
import com.sillycat.easynosql.dao.neo4j.repository.Userneo4jRepository;
public class InitNeo4jService {
@Autowired
private Userneo4jRepository userneo4jRepository;
public void init() {
if (userneo4jRepository.findByUsername("john") != null) {
userneo4jRepository.delete(userneo4jRepository
.findByUsername("john"));
}
if (userneo4jRepository.findByUsername("jane") != null) {
userneo4jRepository.delete(userneo4jRepository
.findByUsername("jane"));
}
// Create new records
Userneo4j john = new Userneo4j();
john.setFirstName("John");
john.setLastName("Smith");
john.setPassword("111111");
john.setRoleneo4j(new Roleneo4j(1));
john.setUsername("john");
Userneo4j jane = new Userneo4j();
jane.setFirstName("Jane");
jane.setLastName("Adams");
jane.setPassword("111111");
jane.setRoleneo4j(new Roleneo4j(2));
jane.setUsername("jane");
john.getRoleneo4j().setUserneo4j(john);
jane.getRoleneo4j().setUserneo4j(jane);
userneo4jRepository.save(john);
userneo4jRepository.save(jane);
userneo4jRepository.findByUsername("john").getRoleneo4j().getRole();
}
}
The Service will access the data via repository classes:
package com.sillycat.easynosql.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.conversion.EndResult;
import com.sillycat.easynosql.dao.neo4j.model.Userneo4j;
import com.sillycat.easynosql.dao.neo4j.repository.Roleneo4jRepository;
import com.sillycat.easynosql.dao.neo4j.repository.Userneo4jRepository;
import com.sillycat.easynosql.model.User;
import com.sillycat.easynosql.model.convert.UserConvert;
import com.sillycat.easynosql.service.UserService;
public class UserServiceNeo4jImpl implements UserService{
@Autowired
private Userneo4jRepository userneo4jRepository;
@Autowired
private Roleneo4jRepository roleneo4jRepository;
public User create(User user) {
Userneo4j existingUser = userneo4jRepository.findByUsername(user
.getUsername());
if (existingUser != null) {
throw new RuntimeException("Record already exists!");
}
Userneo4j saveUser = UserConvert.convertUser2Userneo4j(user);
saveUser.getRoleneo4j().setUserneo4j(saveUser);
userneo4jRepository.save(saveUser);
return UserConvert.convertUserneo4j2User(saveUser);
}
public User read(User user) {
Userneo4j existingUser = userneo4jRepository.findByUsername(user
.getUsername());
user = UserConvert.convertUserneo4j2User(existingUser);
return user;
}
public List<User> readAll() {
List<User> users = new ArrayList<User>();
EndResult<Userneo4j> results = userneo4jRepository.findAll();
for (Userneo4j r : results) {
users.add(UserConvert.convertUserneo4j2User(r));
}
return users;
}
public User update(User user) {
Userneo4j existingUser = userneo4jRepository.findByUsername(user
.getUsername());
if (existingUser == null) {
return null;
}
existingUser.setFirstName(user.getFirstName());
existingUser.setLastName(user.getLastName());
existingUser.getRoleneo4j().setRole(user.getRole().getRole());
roleneo4jRepository.save(existingUser.getRoleneo4j());
userneo4jRepository.save(existingUser);
return UserConvert.convertUserneo4j2User(existingUser);
}
public Boolean delete(User user) {
Userneo4j existingUser = userneo4jRepository.findByUsername(user
.getUsername());
if (existingUser == null) {
return false;
}
userneo4jRepository.delete(existingUser);
return true;
}
}
references:
http://www.infoq.com/cn/articles/graph-nosql-neo4j
http://krams915.blogspot.com/2012/03/spring-mvc-31-implement-crud-with_3045.html
https://github.com/krams915/spring-neo4j-tutorial.git
http://neo4j.org/
http://www.iteye.com/topic/978371
http://agapple.iteye.com/blog/1128400
http://blog.youkuaiyun.com/jdream314/article/details/6633655
http://hi.baidu.com/luohuazju/blog/item/085eff062ac71475020881d3.html
1. Introduction to Graph Database
Node ------relationship -------> property
name=wheel number=4 name=car,color=red,
A Graph --- records data in ---->Nodes -----which have ----> Properties
Nodes ------are organized by -----> Relationships ---- which also have -----> Properties
A Traversal ---- navigates ----> a Graph; it ----identifies ---> Paths ---- which order -----> Nodes
An Index ----maps from ----> Properties ---to either ----> Nodes or Relationships
A Graph Database ---- manages a ----> Graph and ----also manages related ---> Indexs
2. The domain Layer
The User Object
@NodeEntity
public class Userneo4j {
@GraphId
private Long id;
private String firstName;
private String lastName;
@Indexed
private String username;
private String password;
@Fetch @RelatedTo(type = "HAS_ROLENEO4J")
private Roleneo4j roleneo4j;
public Userneo4j() {}
public Userneo4j(String username) {
this.username = username;
}
public Userneo4j(String username, String firstName, String lastName, Roleneo4j roleneo4j) {
this.username = username;
this.firstName = firstName;
this.lastName = lastName;
this.roleneo4j = roleneo4j;
}
public Userneo4j(String username, String password, String firstName, String lastName, Roleneo4j roleneo4j) {
this.username = username;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.roleneo4j = roleneo4j;
}
...snip...
The Role domain:
@NodeEntity
public class Roleneo4j {
@GraphId
private Long id;
private Userneo4j userneo4j;
private Integer role;
public Roleneo4j() {
}
public Roleneo4j(Integer role) {
this.role = role;
}
...snip...
The relationship domain object:
@RelationshipEntity(type = "HAS_ROLENEO4J")
public class Userneo4jRoleneo4jRelationship {
private String description;
@StartNode
private Userneo4j userneo4j;
@EndNode
private Roleneo4j roleneo4j;
...snip...
@NodeEntity annotation is used to turn a POJO class into an entity backed by a node in the graph database. Fields on the entity are by default mapped to properties of the node.
@GraphId
For the simple mapping this is a required field which must be of type Long. It is used by Spring Data Neo4j to store the node or relationship-id to re-connect the entity to the graph.
The @Indexed annotation can be declared on fields that are intended to be indexed by the Neo4j indexing facilities.
@Fetch
To have the collections of relationships being read eagerly ... we have to annotate it with the @Fetch annotation.
@RelatedTo: Connecting node entities
Every field of a node entity that references one or more other node entities is backed by relationships in the graph.
@RelationshipEntity, making them relationship entities. Just as node entities represent nodes in the graph, relationship entities represent relationships.
3. The Service Layer
Init Service to add the init data:
package com.sillycat.easynosql.dao.neo4j.init;
import org.springframework.beans.factory.annotation.Autowired;
import com.sillycat.easynosql.dao.neo4j.model.Roleneo4j;
import com.sillycat.easynosql.dao.neo4j.model.Userneo4j;
import com.sillycat.easynosql.dao.neo4j.repository.Userneo4jRepository;
public class InitNeo4jService {
@Autowired
private Userneo4jRepository userneo4jRepository;
public void init() {
if (userneo4jRepository.findByUsername("john") != null) {
userneo4jRepository.delete(userneo4jRepository
.findByUsername("john"));
}
if (userneo4jRepository.findByUsername("jane") != null) {
userneo4jRepository.delete(userneo4jRepository
.findByUsername("jane"));
}
// Create new records
Userneo4j john = new Userneo4j();
john.setFirstName("John");
john.setLastName("Smith");
john.setPassword("111111");
john.setRoleneo4j(new Roleneo4j(1));
john.setUsername("john");
Userneo4j jane = new Userneo4j();
jane.setFirstName("Jane");
jane.setLastName("Adams");
jane.setPassword("111111");
jane.setRoleneo4j(new Roleneo4j(2));
jane.setUsername("jane");
john.getRoleneo4j().setUserneo4j(john);
jane.getRoleneo4j().setUserneo4j(jane);
userneo4jRepository.save(john);
userneo4jRepository.save(jane);
userneo4jRepository.findByUsername("john").getRoleneo4j().getRole();
}
}
The Service will access the data via repository classes:
package com.sillycat.easynosql.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.conversion.EndResult;
import com.sillycat.easynosql.dao.neo4j.model.Userneo4j;
import com.sillycat.easynosql.dao.neo4j.repository.Roleneo4jRepository;
import com.sillycat.easynosql.dao.neo4j.repository.Userneo4jRepository;
import com.sillycat.easynosql.model.User;
import com.sillycat.easynosql.model.convert.UserConvert;
import com.sillycat.easynosql.service.UserService;
public class UserServiceNeo4jImpl implements UserService{
@Autowired
private Userneo4jRepository userneo4jRepository;
@Autowired
private Roleneo4jRepository roleneo4jRepository;
public User create(User user) {
Userneo4j existingUser = userneo4jRepository.findByUsername(user
.getUsername());
if (existingUser != null) {
throw new RuntimeException("Record already exists!");
}
Userneo4j saveUser = UserConvert.convertUser2Userneo4j(user);
saveUser.getRoleneo4j().setUserneo4j(saveUser);
userneo4jRepository.save(saveUser);
return UserConvert.convertUserneo4j2User(saveUser);
}
public User read(User user) {
Userneo4j existingUser = userneo4jRepository.findByUsername(user
.getUsername());
user = UserConvert.convertUserneo4j2User(existingUser);
return user;
}
public List<User> readAll() {
List<User> users = new ArrayList<User>();
EndResult<Userneo4j> results = userneo4jRepository.findAll();
for (Userneo4j r : results) {
users.add(UserConvert.convertUserneo4j2User(r));
}
return users;
}
public User update(User user) {
Userneo4j existingUser = userneo4jRepository.findByUsername(user
.getUsername());
if (existingUser == null) {
return null;
}
existingUser.setFirstName(user.getFirstName());
existingUser.setLastName(user.getLastName());
existingUser.getRoleneo4j().setRole(user.getRole().getRole());
roleneo4jRepository.save(existingUser.getRoleneo4j());
userneo4jRepository.save(existingUser);
return UserConvert.convertUserneo4j2User(existingUser);
}
public Boolean delete(User user) {
Userneo4j existingUser = userneo4jRepository.findByUsername(user
.getUsername());
if (existingUser == null) {
return false;
}
userneo4jRepository.delete(existingUser);
return true;
}
}
references:
http://www.infoq.com/cn/articles/graph-nosql-neo4j
http://krams915.blogspot.com/2012/03/spring-mvc-31-implement-crud-with_3045.html
https://github.com/krams915/spring-neo4j-tutorial.git
http://neo4j.org/
http://www.iteye.com/topic/978371
http://agapple.iteye.com/blog/1128400
http://blog.youkuaiyun.com/jdream314/article/details/6633655
http://hi.baidu.com/luohuazju/blog/item/085eff062ac71475020881d3.html