java 使用我的spring Boot API在hib中实现双向关系的问题

xytpbqjk  于 2023-01-11  发布在  Java
关注(0)|答案(2)|浏览(154)

我想创建一个具有以下规格的 Spring Boot 托控制器:
电力及气体供应公司的客户可选择透过电邮或平邮收取每月帐单,或两者皆收,或两者皆收。
我的目标是创建java hib实体来管理这些客户和他们发送账单的选择。
公用事业客户由其电子邮件标识,并且可以具有更改客户选择状态的多项选择更改事件。
客户做出的每个选择都会生成一个选择更改事件。
选择更改事件与客户相关。客户可以具有多个选择事件。
下面是我的java实体。

@Entity
@Table(name = "customers")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Email(message="this field must respect the email format !")
    private String email;
    
    @ManyToOne
    private Choices choices;

}

@Entity
@Table(name = "choices")
public class Choices {

    @Id
    private String id;

    @Column(name = "email")
    private boolean isThisChoice;
    
    @OneToOne
    private Customer customer;

}

The resulting customer with id 24587 (GET request):
{
  "id": "24587",
  "email": "tartampion",
  "choices": [
    {
      "id": "regular mail",
      "isThisChoice": false
    },
    {
      "id": "email",
      "isThisChoice": true
    }
  ]
}

我必须有一个客户选择的事件管理实体吗

taor4pac

taor4pac1#

这里你必须使用多对多Map。因为一个客户可以有很多选择,而一个选择可以被很多客户选择。

package com.umesh.entity;

import com.fasterxml.jackson.annotation.JsonManagedReference;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name="customer")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    @Column(name="email")
    private String email;

    @ManyToMany(fetch=FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH })
    @JoinTable(
            name="customer_choice",
            joinColumns=@JoinColumn(name="customer_id"),
            inverseJoinColumns=@JoinColumn(name="choice_id")
    )
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<Choice> choices;

    public void Customer(){}

    public int getId() {
        return id;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public List<Choice> getChoices() {
        return choices;
    }

    public void setChoices(List<Choice> choices) {
        this.choices = choices;
    }

    public void addChoices(Choice choice){
        if(choices == null){
            choices = new ArrayList<>();
            choices.add(choice);
        }
        choices.add(choice);
    }
}

package com.umesh.entity;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(name="choice")
public class Choice {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    @Column(name="choice")
    private String choice;

    @ManyToMany(fetch=FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH })
    @JoinTable(
            name="customer_choice",
            joinColumns=@JoinColumn(name="choice_id"),
            inverseJoinColumns=@JoinColumn(name="customer_id")
    )
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<Customer> customers;

    public void Choics(){}

    public int getId() {
        return id;
    }

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

    public String getChoice() {
        return choice;
    }

    public void setChoice(String choice) {
        this.choice = choice;
    }

    public List<Customer> getCustomers() {
        return customers;
    }

    public void setCustomers(List<Customer> customers) {
        this.customers = customers;
    }

    public void addCustomers(Customer customer){
        if(customers == null){
            customers = new ArrayList<>();
            customers.add(customer);
        }
        customers.add(customer);
    }
}
polkgigr

polkgigr2#

您是指更像以下的模型吗:

@Entity
@Table(name = "customers")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Email(message="this field must respect the email format !")
    private String email;
    
    @ElementCollection
    @CollectionTable(name="Choices")
    @MapKeyColumn(name="CHOICE") //an "EMAIL" or "MAIL" string. You can use an enum instead if you want, but I wouldn't for upgrade reasons.
    @Column(name="enabled")
    private Map<String, Boolean> choices;

}

这将为您提供一个选择Map,从而使JSON更像:

{
  "id": "24587",
  "email": "tartampion",
  "choices": {
      "MAIL": false,
      "EMAIL": true
    }
}

如果你在未来得到其他的选择和组合,它应该会扩展得多。
同样,您可以使用相同的表格结构,将“Choices”作为实体:

@Entity
@Table(name = "customers")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Email(message="this field must respect the email format !")
    private String email;

    @OneToMany(mappedBy = "customer")
    @MapKey(name="type") //don't need this if you want just a list
    private Map<String, Choice> choices;
}

@Entity
@Table(name = "choices")
public class Choice {
    @Id
    @OneToOne
    private Customer customer;

    @Id
    @Enumerated(EnumType.STRING)
    private ChoiceType type;

    @Column(name="enabled")
    private boolean enabled;
}

@IdClass(EmployeeId.class)
public class ChoiceId implements Serializable {
  private Integer customer;
  private ChoiceType type;
}

public enum ChoiceType {
    MAIL, 
    EMAIL;             
}

相关问题