我在REST API中嵌套资源时未获取到不同的标识符。

huangapple go评论63阅读模式
英文:

I'm not getting distinct ID's for nested resources in REST api

问题

以下是您提供的内容的中文翻译:

我正在编写一个ContactManager的REST API,其中包括ContactGroup和Contact实体。Contact实体嵌套在ContactGroup中。我想要在每个ContactGroup内生成从1开始递增的联系人Id,并且独立于其他ContactGroup的联系人。

但是,在我的代码中,每个特定ContactGroup中创建的新联系人的Id生成取决于其他ContactGroup中最后一个创建的联系人的Id。

例如,如果有两个contactGroupId为1、2的ContactGroup。我已经在contactGroupId为1的ContactGroup中创建了2个联系人,其contactId分别为1和2。然后,在创建contactGroupId为2的ContactGroup中的新联系人时,生成的contactId是3,但应该生成为1。

我正在添加这两个实体:

ContactGroup.java

@Entity
public class ContactGroup {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long groupId;

    private String name;

    private String description;

    @OneToMany(targetEntity = Contact.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JsonIgnore
    private List<Contact> contacts;

    private Integer numberOfContacts;

    public ContactGroup() {
    }

    public ContactGroup(String name, String description) {
        this.name = name;
        this.description = description;
    }

    // getters & setters
}

Contact.java

@Entity
public class Contact {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long contactId;

    private String name;

    private String email;

    private Long phoneNumber;

    private String address;

    @ManyToOne(targetEntity = ContactGroup.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JsonIgnore
    private ContactGroup contactGroup;

    public Contact() {
    }

    public Contact(String name, String email, Long phoneNumber, String address) {
        this.name = name;
        this.email = email;
        this.phoneNumber = phoneNumber;
        this.address = address;
    }
    // getters & setters
}

请帮我解决这个问题。任何帮助都将不胜感激。

英文:

I am writing a REST api of ContactManager, which consists of ContactGroup and Contact entity. The Contact entity is nested in ContactGroup. I want to generate Id of contacts within each ContactGroup starting with 1 and increases sequentially and is independent of other Contact group's contacts.

But, In my code the Id generation of each new contact created in a particular ContactGroup is dependent on the Id of last contact created in other ContactGroup.

For example, If there is two ContactGroup with contactGroupId: 1,2. And I have created 2 contacts in ContactGroupId: 1 with contactId: 1 & 2. Then while creating new contact in ContactGroupId: 2, the contactId generated is 3 but it should be generated as 1.

I am adding the both entities:

ContactGroup.java

 @Entity
    public class ContactGroup {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long groupId;
    
        private String name;
    
        private String description;
    
        @OneToMany(targetEntity = Contact.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
        @JsonIgnore
        private List&lt;Contact&gt; contacts;
    
        private Integer numberOfContacts;
    
        public ContactGroup() {
        }
    
        public ContactGroup(String name, String description) {
            this.name = name;
            this.description = description;
        }
    
        //getters &amp; setters
    }

Contact.java

@Entity
public class Contact {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long contactId;

    private String name;

    private String email;

    private Long phoneNumber;

    private String address;

    @ManyToOne(targetEntity = ContactGroup.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JsonIgnore
    private ContactGroup contactGroup;

    public Contact() {
    }

    public Contact(String name, String email, Long phoneNumber, String address) {
        this.name = name;
        this.email = email;
        this.phoneNumber = phoneNumber;
        this.address = address;
    }
    // getters &amp; setters
}

Kindly, help me out in solving this problem. Any help would be appreciated.

答案1

得分: 0

Contact.contactId是主键,它必须在所有联系人中是唯一的。您不能在不同的联系人组中拥有具有相同id的联系人。
您可以通过使用复合id(contactId,contactGroup)和自定义生成器来实现您的目标,但我不确定这是否是一个好主意。

英文:

Contact.contactId is a primary key, it must be unique for all contacts. You cannot have contacts with the same id in different contact groups.
You could achieve your goal by using a composite id (contactId, contactGroup) and custom generator for the contactId, but I'm not sure it's a good idea.

huangapple
  • 本文由 发表于 2020年9月9日 13:45:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63805405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定