简体   繁体   中英

Relationship between entity classes in microservices

I want to create a simple microservice application in springboot. My project contain 2 services. One of them is userService, the other one is noteService.

For each service, I want to use a schema. These are userService schema and noteService schema. One user creates many notes. There is a one-to-many relationship. I cannot create that relationship between 2 service. I don't understand which columns should tables contain? Do you have any idea? My pojo classes are below:

User.java

@Document(collection = "User")
public class User {

    @Id
    private String id;
    private String userName;

    public User(String id, String userName) {
        this.id = id;
        this.userName = userName;
    }

    public User(){

    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", userName='" + userName + '\'' +
                '}';
    }
}

Note.java

@Document(collection = "Note")
public class Note {

    @Id
    private String id;
    private String caption;
    private String userNotes;

    public Note() {
    }

    public Note(String id, String caption, String userNotes) {
        this.id = id;
        this.caption = caption;
        this.userNotes = userNotes;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCaption() {
        return caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

    public String getUserNotes() {
        return userNotes;
    }

    public void setUserNotes(String userNotes) {
        this.userNotes = userNotes;
    }


    @Override
    public String toString() {
        return "Note{" +
                "id='" + id + '\'' +
                ", caption='" + caption + '\'' +
                ", userNotes='" + userNotes + '\'' +
                '}';
    }
}

A service is a layer which facilitates access to your entities via DAO's. So typically you would have a list of DAO's in a service, using which you would access all your entities.

You can have multiple entities in a single service. For details on how to create DAO's and use them, please refer to the spring-data-mongo guide

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM