Skip to content

Commit 7d38405

Browse files
committed
Adding example project
1 parent b0b3a20 commit 7d38405

5 files changed

Lines changed: 210 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.spring.data.neo4j;
2+
3+
import com.baeldung.spring.data.neo4j.config.LibraryNeo4jConfiguration;
4+
import com.baeldung.spring.data.neo4j.model.Book;
5+
import com.baeldung.spring.data.neo4j.model.Person;
6+
import com.baeldung.spring.data.neo4j.service.BookService;
7+
import org.apache.commons.logging.Log;
8+
import org.apache.commons.logging.LogFactory;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.test.context.ContextConfiguration;
13+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14+
15+
import static org.junit.Assert.assertEquals;
16+
17+
@RunWith(SpringJUnit4ClassRunner.class)
18+
@ContextConfiguration(classes = LibraryNeo4jConfiguration.class)
19+
public class BookServiceTest {
20+
21+
private static final Log LOGGER = LogFactory.getLog(BookServiceTest.class);
22+
23+
@Autowired
24+
private BookService bookService;
25+
26+
@Test
27+
public void testSaveBook() {
28+
final Person author1 = new Person();
29+
author1.setName("Mark Twain");
30+
author1.setBorn(1835);
31+
final Book book = new Book();
32+
book.setTitle("The Adventures of Tom Sawyer");
33+
book.setReleased(1876);
34+
book.setPerson(author1);
35+
36+
bookService.save(book);
37+
final Book savedBook = bookService.findBookById(book.getId());
38+
assertEquals(book.getTitle(), savedBook.getTitle());
39+
}
40+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.spring.data.neo4j.config;
2+
3+
4+
import org.neo4j.ogm.session.SessionFactory;
5+
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Import;
8+
import org.springframework.data.neo4j.config.Neo4jConfiguration;
9+
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
10+
import org.springframework.data.neo4j.server.Neo4jServer;
11+
import org.springframework.data.neo4j.server.RemoteServer;
12+
import org.springframework.scheduling.annotation.EnableScheduling;
13+
import org.springframework.transaction.annotation.EnableTransactionManagement;
14+
15+
16+
@Configuration
17+
@ComponentScan("com.baeldung.spring.data.neo4j")
18+
@EnableNeo4jRepositories(basePackages = "com.baeldung.spring.data.neo4j.repostory")
19+
public class LibraryNeo4jConfiguration extends Neo4jConfiguration {
20+
21+
public static final String URL = System.getenv("NEO4J_URL") != null ? System.getenv("NEO4J_URL") : "http://localhost:7474";
22+
23+
@Override
24+
public Neo4jServer neo4jServer() {
25+
return new RemoteServer(URL,"neo4j","password");
26+
}
27+
28+
@Override
29+
public SessionFactory getSessionFactory() {
30+
return new SessionFactory("com.baeldung.spring.data.neo4j.model");
31+
}
32+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.spring.data.neo4j.model;
2+
3+
import org.neo4j.graphdb.Direction;
4+
import org.neo4j.ogm.annotation.*;
5+
6+
import java.util.Collection;
7+
import java.util.List;
8+
import java.util.concurrent.atomic.AtomicLong;
9+
10+
@NodeEntity
11+
public class Book {
12+
13+
private static final AtomicLong TS = new AtomicLong();
14+
15+
@GraphId
16+
private Long id;
17+
private String title;
18+
private int released;
19+
20+
@Relationship(type="AUTHORED_BY", direction = Relationship.INCOMING)
21+
private Person person;
22+
23+
public Book() {
24+
this.id = TS.getAndIncrement();
25+
}
26+
27+
public Long getId() {
28+
return id;
29+
}
30+
31+
public String getTitle() {
32+
return title;
33+
}
34+
35+
public void setTitle(String title) {
36+
this.title = title;
37+
}
38+
39+
public int getReleased() {
40+
return released;
41+
}
42+
43+
public void setReleased(int released) {
44+
this.released = released;
45+
}
46+
47+
public Person getPerson() {
48+
return person;
49+
}
50+
51+
public void setPerson(Person person) {
52+
this.person = person;
53+
}
54+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.spring.data.neo4j.repostory;
2+
3+
import com.baeldung.spring.data.neo4j.model.Book;
4+
import org.springframework.data.neo4j.annotation.Query;
5+
import org.springframework.data.neo4j.repository.GraphRepository;
6+
import org.springframework.data.repository.query.Param;
7+
import org.springframework.stereotype.Repository;
8+
9+
import java.util.Collection;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
@Repository
14+
public interface BookRepository extends GraphRepository<Book> {
15+
Book findByTitle(@Param("title") String title);
16+
17+
@Query("MATCH (m:Book) WHERE m.title =~ ('(?i).*'+{title}+'.*') RETURN m")
18+
Collection<Book> findByTitleContaining(@Param("title") String title);
19+
20+
@Query("MATCH (m:Book)<-[:ACTED_IN]-(a:Person) RETURN m.title as Book, collect(a.name) as cast LIMIT {limit}")
21+
List<Map<String,Object>> graph(@Param("limit") int limit);
22+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.spring.data.neo4j.service;
2+
3+
4+
import com.baeldung.spring.data.neo4j.model.Book;
5+
import com.baeldung.spring.data.neo4j.repostory.BookRepository;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Service;
8+
9+
import java.util.*;
10+
11+
@Service
12+
public class BookService {
13+
14+
@Autowired
15+
private BookRepository bookRepository;
16+
17+
private Map<String, Object> toD3Format(final Iterator<Map<String, Object>> result) {
18+
List<Map<String,Object>> nodes = new ArrayList<Map<String,Object>>();
19+
List<Map<String,Object>> rels= new ArrayList<Map<String,Object>>();
20+
int i=0;
21+
while (result.hasNext()) {
22+
Map<String, Object> row = result.next();
23+
nodes.add(map("title",row.get("book"),"label","book"));
24+
int target=i;
25+
i++;
26+
for (Object name : (Collection) row.get("cast")) {
27+
Map<String, Object> actor = map("title", name,"label","actor");
28+
int source = nodes.indexOf(actor);
29+
if (source == -1) {
30+
nodes.add(actor);
31+
source = i++;
32+
}
33+
rels.add(map("source",source,"target",target));
34+
}
35+
}
36+
return map("nodes", nodes, "links", rels);
37+
}
38+
39+
private Map<String, Object> map(final String key1, final Object value1, final String key2, final Object value2) {
40+
Map<String, Object> result = new HashMap<String,Object>(2);
41+
result.put(key1,value1);
42+
result.put(key2,value2);
43+
return result;
44+
}
45+
46+
public Map<String, Object> graph(final int limit) {
47+
Iterator<Map<String, Object>> result = bookRepository.graph(limit).iterator();
48+
return toD3Format(result);
49+
}
50+
51+
public Book save(final Book book){
52+
return bookRepository.save(book);
53+
}
54+
55+
public Book findBookById(final Long id){
56+
return bookRepository.findOne(id);
57+
}
58+
59+
public void deleteAllInGraph(){
60+
bookRepository.deleteAll();
61+
}
62+
}

0 commit comments

Comments
 (0)