Spring-Data for OrientDB Using template:OrientDB图模型开发参考项目求助
Hey there! I totally get how frustrating it can be to hunt down clear examples for working with Spring Data and OrientDB's graph model when you're just starting out. Let me walk you through a simple, self-contained demo project that covers the core basics—you can use this as a starting point for your own project.
1. Project Setup & Dependencies
First, let's get the dependencies sorted. If you're using Maven, add these to your pom.xml:
<dependencies> <!-- Spring Data OrientDB --> <dependency> <groupId>com.orientechnologies</groupId> <artifactId>spring-data-orientdb</artifactId> <version>2.2.0</version> <!-- Use the latest stable version --> </dependency> <!-- Spring Boot Starter (optional but simplifies setup) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies>
For Gradle, add this to your build.gradle:
dependencies { implementation 'com.orientechnologies:spring-data-orientdb:2.2.0' implementation 'org.springframework.boot:spring-boot-starter' }
2. OrientDB Configuration
Create an application.properties file in src/main/resources to configure your OrientDB connection:
# OrientDB Connection Settings spring.data.orientdb.url=remote:localhost/your-graph-db spring.data.orientdb.username=admin spring.data.orientdb.password=admin spring.data.orientdb.pool.max=20
3. Graph Entity Classes
Let's define two simple vertex entities (User) and an edge entity (Friendship) to represent a social network graph:
Vertex: User
import com.orientechnologies.orient.core.metadata.schema.OType; import com.orientechnologies.spring.data.annotation.Vertex; import com.orientechnologies.spring.data.annotation.Property; import org.springframework.data.orientdb.core.mapping.ODocument; @Vertex public class User extends ODocument { @Property(type = OType.STRING) private String username; @Property(type = OType.STRING) private String email; // Constructors, getters, setters public User() {} public User(String username, String email) { this.username = username; this.email = email; } // Getters and setters omitted for brevity }
Edge: Friendship
import com.orientechnologies.spring.data.annotation.Edge; import com.orientechnologies.spring.data.annotation.From; import com.orientechnologies.spring.data.annotation.To; import org.springframework.data.orientdb.core.mapping.ODocument; @Edge public class Friendship extends ODocument { @From private User fromUser; @To private User toUser; private String status; // e.g., "active", "pending" // Constructors, getters, setters public Friendship() {} public Friendship(User fromUser, User toUser, String status) { this.fromUser = fromUser; this.toUser = toUser; this.status = status; } // Getters and setters omitted for brevity }
4. Spring Data Repositories
Create repository interfaces to handle CRUD operations for your graph entities. These extend OrientGraphRepository:
User Repository
import com.orientechnologies.spring.data.repository.OrientGraphRepository; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends OrientGraphRepository<User, String> { // Custom query example: find user by username User findByUsername(String username); }
Friendship Repository
import com.orientechnologies.spring.data.repository.OrientGraphRepository; import org.springframework.stereotype.Repository; @Repository public interface FriendshipRepository extends OrientGraphRepository<Friendship, String> { // Custom query example: find friendships from a specific user Iterable<Friendship> findByFromUser(User user); }
5. Service Layer (Business Logic)
Create a service class to wrap the repository operations and add business logic:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.stream.Collectors; import java.util.stream.StreamSupport; @Service public class SocialNetworkService { private final UserRepository userRepository; private final FriendshipRepository friendshipRepository; @Autowired public SocialNetworkService(UserRepository userRepository, FriendshipRepository friendshipRepository) { this.userRepository = userRepository; this.friendshipRepository = friendshipRepository; } // Create a new user public User createUser(String username, String email) { User user = new User(username, email); return userRepository.save(user); } // Create a friendship between two users public Friendship createFriendship(User fromUser, User toUser) { Friendship friendship = new Friendship(fromUser, toUser, "active"); return friendshipRepository.save(friendship); } // Get all friends of a user public Iterable<User> getUserFriends(User user) { Iterable<Friendship> friendships = friendshipRepository.findByFromUser(user); return StreamSupport.stream(friendships.spliterator(), false) .map(Friendship::getToUser) .collect(Collectors.toList()); } }
6. Testing the Demo
You can create a simple Spring Boot application runner to test the functionality:
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class OrientDbGraphDemoApplication { public static void main(String[] args) { SpringApplication.run(OrientDbGraphDemoApplication.class, args); } @Bean CommandLineRunner run(SocialNetworkService service) { return args -> { // Create two users User alice = service.createUser("alice123", "alice@example.com"); User bob = service.createUser("bob456", "bob@example.com"); // Create a friendship between them service.createFriendship(alice, bob); // Get Alice's friends System.out.println("Alice's friends:"); service.getUserFriends(alice).forEach(friend -> System.out.println("- " + friend.getUsername())); }; } }
Key Notes to Remember
- Make sure your OrientDB server is running locally (or update the connection URL to point to your remote instance).
- The
@Vertexand@Edgeannotations are critical for mapping your Java classes to OrientDB's graph model. - You can extend the repositories with custom OrientDB SQL queries using the
@Queryannotation if needed.
内容的提问来源于stack exchange,提问作者Mustanish Altamash

