부트 개념과 활용-29.Neo4j(스프링 데이터)


스프링 데이터 11부: Neo4j

Neo4j는 노드간의 연관 관계를 영속화하는데 유리한 그래프 데이터베이스 입니다.

의존성 추가

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>

Neo4j 설치 및 실행 (도커)

  • docker run -p 7474:7474 -p 7687:7687 -d –name noe4j_boot neo4j
  • http://localhost:7474/browser

스프링 데이터 Neo4J

  • Neo4jTemplate (Deprecated)
  • SessionFactory
  • Neo4jRepository

실습 (SessionFactory)

@NodeEntity
public class Account {

    @Id
    @GeneratedValue
    private Long id;

    private String username;

    private String email;

    @Relationship(type = "has")
    private Set<Role> roles = new HashSet<>();
...
@NodeEntity
public class Role {

    @Id @GeneratedValue
    private Long id;

    private String name;
...
@Component
public class Neo4jRunner implements ApplicationRunner {

    @Autowired
    SessionFactory sessionFactory;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Account account = new Account();
        account.setEmail("jaeuk2274@gmail.com");
        account.setUsername("jaeuk");

        Role role = new Role();
        role.setName("admin");

        account.getRoles().add(role);

        Session session = sessionFactory.openSession();
        session.save(account);
        sessionFactory.close();

        System.out.println("finished");
    }
}

실습하다 보니 상당히 재밌는 점은.. 관계형으로 이렇게 볼 수 있는 화면이었다.. neo4j

실습 (AccountRepository)

public interface AccountRepository extends Neo4jRepository<Account, Long> {
}
@Component
public class Neo4jRunner implements ApplicationRunner {

    @Autowired
    SessionFactory sessionFactory;

    @Autowired
    AccountRepository accountRepository;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Account account = new Account();
        account.setEmail("hunwook@gmail.com");
        account.setUsername("hunwook");

        Role role = new Role();
        role.setName("user");

        account.getRoles().add(role);

        accountRepository.save(account);
        /*
        Session session = sessionFactory.openSession();
        session.save(account);
        sessionFactory.close();
        */
        System.out.println("finished");
    }
}





© 2019. by jaeuk