# TuGraph-OGM @TODO ## 1.Introduction > TuGraph-OGM project is open source on other repositories. TuGraph-OGM (Object Graph Mapping) is a graph object mapping tool designed for TuGraph. It supports the mapping of Java objects (POJO) into TuGraph. The classes in Java are mapped to nodes in the graph, the collections in the classes are mapped to edges, and the properties of the classes are mapped to the attributes of the graph objects. It also provides corresponding functions to manipulate the graph database. Therefore, Java developers can easily use TuGraph database in the familiar ecosystem. TuGraph-OGM is also compatible with Neo4j-OGM. Neo4j ecosystem users can migrate seamlessly to TuGraph database. ### 1.1.Functions of TuGraph-OGM TuGraph-OGM provides the following functions for operating TuGraph: | Function | Usage | | ------------------------------- | -------------------------------------------------------------------------------- | | Insert single vertex/edge | void session.save(T object) | | Batch insert vertexs/edges | void session.save(T object) | | Delete vertex and its corresponding edges | void session.delete(T object) | | Delete all vertexs with specific label | void session.deleteAll(Class\ type) | | Clear the database | void purgeDatabase() | | Update vertex | void session.save(T newObject) | | Query single node by id | T load(Class type, ID id) | | Query multiple nodes by ids | Collection\ loadAll(Class\ type, Collection ids) | | Query all nodes with specific label | Collection\ loadAll(Class\ type) | | Query with conditions | Collection\ loadAll(Class\ type, Filters filters) | | Cypher query (specify the return type) | T queryForObject(Class\ objectType, String cypher, Map parameters) | | Cypher query | Result query(String cypher, Map parameters) | ## 2.Compile TuGraph-OGM ```shell cd tugraph-ogm mvn clean install -DskipTests -Denforcer.skip=true ``` ## 3.Use TuGraph-OGM Please refer to the detailed examples in TuGraphOGMDemo under the demo folder.How to import dependencies in `pom.xml`. ``` org.neo4j neo4j-ogm-api 0.1.0-SNAPSHOT org.neo4j neo4j-ogm-core 0.1.0-SNAPSHOT org.neo4j tugraph-rpc-driver 0.1.0-SNAPSHOT ``` ### 3.1.构建图对象 ```java @NodeEntity public class Movie { // create Movie vertex @Id private Long id; // Movie id private String title; // title attribute private int released; // released attribute // create Edge ACTS_IN (actor)-[:ACTS_IN]->(movie) @Relationship(type = "ACTS_IN", direction = Relationship.Direction.INCOMING) Set actors = new HashSet<>(); public Movie(String title, int year) { this.title = title; this.released = year; } public Long getId() { return id; } public void setReleased(int released) { this.released = released; } } @NodeEntity public class Actor { // create Actor vertex @Id private Long id; private String name; @Relationship(type = "ACTS_IN", direction = Relationship.Direction.OUTGOING) private Set movies = new HashSet<>(); public Actor(String name) { this.name = name; } public void actsIn(Movie movie) { movies.add(movie); movie.getActors().add(this); } } ``` ### 3.2.与TuGraph建立连接 ```java // configuration String databaseUri = "list://ip:port"; String username = "admin"; String password = "73@TuGraph"; //Start driver Driver driver = new RpcDriver(); Configuration.Builder baseConfigurationBuilder = new Configuration.Builder() .uri(databaseUri) .verifyConnection(true) .credentials(username, password); driver.configure(baseConfigurationBuilder.build()); driver.configure(baseConfigurationBuilder.build()); // Open session SessionFactory sessionFactory = new SessionFactory(driver, "entity_path"); Session session = sessionFactory.openSession(); ``` ### 3.3.Perform CRUD operations through OGM ```java // new Movie jokes = new Movie("Jokes", 1990); // Create Movie-vertex "jokes" session.save(jokes); // Store "jokes" in TuGraph Movie speed = new Movie("Speed", 2019); Actor alice = new Actor("Alice Neeves"); alice.actsIn(speed); // Connect the "speed" vertex with the "alice" vertex through the ACTS_IN relationship session.save(speed); // Store two vertexs and one edge // delete session.delete(alice); // Delete the "alice" vertex and its connected edge Movie m = session.load(Movie.class, jokes.getId()); // Get the "jokes" vertex based on its id session.delete(m); // Delete the "jokes" node // update speed.setReleased(2018); session.save(speed); // Update the "speed" attribute // get Collection movies = session.loadAll(Movie.class); // Get all Movie vertexs Collection moviesFilter = session.loadAll(Movie.class, new Filter("released", ComparisonOperator.LESS_THAN, 1995)); // Query all movies released before 1995 // Call Cypher HashMap parameters = new HashMap<>(); parameters.put("Speed", 2018); Movie cm = session.queryForObject(Movie.class, "MATCH (cm:Movie{Speed: $Speed}) RETURN *", parameters); // Query the Movie with "Speed" of 2018 session.query("CALL db.createVertexLabel('Director', 'name', 'name'," + "STRING, false, 'age', INT16, true)", emptyMap()); // Create vertex label "Director" session.query("CALL db.createEdgeLabel('DIRECT', '[]')", emptyMap()); // Create edge label "DIRECT" Result createResult = session.query( "CREATE (n:Movie{title:\"The Shawshank Redemption\", released:1994})" + "<-[r:DIRECT]-" + "(n2:Director{name:\"Frank Darabont\", age:63})", emptyMap()); QueryStatistics statistics = createResult.queryStatistics(); // Get the create result System.out.println("created " + statistics.getNodesCreated() + " vertices"); // View the number of vertexs created System.out.println("created " + statistics.getRelationshipsCreated() + " edges"); //View the number of edges created // Clear the database session.deleteAll(Movie.class); // Delete all Movie vertexs session.purgeDatabase(); // Delete all data ```