Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composite Primary Key using MongoDB and Spring Data JPA

Could someone tell how to create a composite key in Spring Data JPA using Mongo DB

like image 646
Viswanath Nuggu Avatar asked Dec 22 '22 22:12

Viswanath Nuggu


1 Answers

You cannot use MongoDB with Spring Data JPA because JPA is for relational databases like MySQL.

Instead you have to use Spring Data MongDB and you will find the documentation here:

https://docs.spring.io/spring-data/mongodb/docs/2.1.8.RELEASE/reference/html/

But here's an example with a composite key:

class StudentChairman {

    @Id
    private CompositeKey id;

    // getters and setters

    static class CompositeKey implements Serializable {
        private String studentId;
        private String groupId;

        // getters and setters
    }
}
like image 119
Simon Martinelli Avatar answered Jan 07 '23 12:01

Simon Martinelli