无法以多对多关系在数据库中插入数据

时间:2019-02-23 09:31:40

标签: java json database spring-boot many-to-many

我正在使用Spring Boot。这是我的代码:

会员

@Entity
@Table(name = "member")
public class Member {
@Id
@Column(name = "m_seq")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int seq;

@NotBlank(message = "Name may not be blank")
@Size(min = 2, max = 32, message = "Name must be between 2 and 32 characters long")
@Column(name = "m_username")
private String username;

@NotBlank(message = "password may not be blank")
@Column(name = "m_password")
private String password;

@NotBlank(message = "fullName may not be blank")
@Column(name = "m_fullname")
private String fullName;

@Column(name = "m_regdate")
@CreationTimestamp
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy/MM/dd HH:mm:ss")
private Date regDate;

@NotBlank(message = "phone may not be blank")
@Column(name = "m_phone")
private String phone;

@NotBlank(message = "email may not be blank")
@Email
@Column(name = "m_email")
private String email;

@ManyToOne
@JsonBackReference(value = "member-membergroup")
@JoinColumn(name = "MG_SEQ")
private MemberGroup memberGroup;

@Column(name = "m_useyn")
private int useyn;

@JsonManagedReference(value = "member-dataGroup")
@OneToMany(mappedBy = "member", fetch = FetchType.LAZY)
private List<DataGroup> dataGroups;

@ManyToMany
@JoinTable(name = "member_data_source",
        joinColumns = @JoinColumn(name = "m_seq"),
        inverseJoinColumns = @JoinColumn(name = "DS_SEQ"))
private List<DataSource> dataSources;

public Member() {
    super();
    // TODO Auto-generated constructor stub
}

public Member(int seq,
              @NotBlank(message = "Name may not be blank") @Size(min = 2, max = 32, message = "Name must be between 2 and 32 characters long") String username,
              @NotBlank(message = "password may not be blank") String password,
              @NotBlank(message = "fullName may not be blank") String fullName,
              @NotBlank(message = "phone may not be blank") String phone,
              @NotBlank(message = "email may not be blank") @Email String email, MemberGroup memberGroup, int useyn, Date regDate,
              List<DataSource> dataSources,
              List<DataGroup> dataGroups) {
    super();
    this.seq = seq;
    this.username = username;
    this.password = password;
    this.fullName = fullName;
    this.regDate = regDate;
    this.phone = phone;
    this.email = email;
    this.memberGroup = memberGroup;
    this.useyn = useyn;
    this.dataGroups = dataGroups;
    this.dataSources = dataSources;
}

public int getSeq() {
    return seq;
}

public void setSeq(int seq) {
    this.seq = seq;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getFullName() {
    return fullName;
}

public void setFullName(String fullName) {
    this.fullName = fullName;
}

public Date getRegDate() {
    return regDate;
}

public void setRegDate(Date regDate) {
    this.regDate = regDate;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public String getEmail() {
    return email;
}

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

public MemberGroup getMemberGroup() {
    return memberGroup;
}

public void setMemberGroup(MemberGroup memberGroup) {
    this.memberGroup = memberGroup;
}

public int getUseyn() {
    return useyn;
}

public void setUseyn(int useyn) {
    this.useyn = useyn;
}

public List<DataGroup> getDataGroups() {
    return dataGroups;
}

public void setDataGroups(List<DataGroup> dataGroups) {
    this.dataGroups = dataGroups;
}

public List<DataSource> getDataSources() {
    return dataSources;
}

public void setDataSources(List<DataSource> dataSources) {
    this.dataSources = dataSources;
}

数据源

@Entity
@Table(name = "DATA_SOURCE")
public class DataSource {

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

@NotNull
@Size(min = 5, message = "Data Source Name should have atlest 5 characters")
@Column(name = "DS_NAME")
private String dsName;

@NotNull
@Size(min = 5, message = "Data Source Type should have atlest 5 characters")
@Column(name = "DS_TYPE")
private String dsType;

@NotNull
@Column(name = "DS_URL")
private String dsUrl;

@NotNull
@Column(name = "DS_API_KEY")
private String dsApiKey;

@ManyToMany(cascade = CascadeType.ALL)
@JsonBackReference(value = "member-dataSource")
@JoinTable(name = "member_data_source",
        joinColumns = @JoinColumn(name = "DS_SEQ"),
        inverseJoinColumns = @JoinColumn(name = "m_seq"))
private List<Member> members;

public DataSource() {
    super();
}

public DataSource(int dsSeq, String dsName, String dsType, String dsUrl, String dsApiKey, List<Member> members) {
    super();
    this.dsSeq = dsSeq;
    this.dsName = dsName;
    this.dsType = dsType;
    this.dsApiKey = dsApiKey;
    this.dsUrl = dsUrl;
    this.members = members;
}

public int getDsSeq() {
    return dsSeq;
}

public void setDsSeq(int dsSeq) {
    this.dsSeq = dsSeq;
}

public String getDsName() {
    return dsName;
}

public void setDsName(String dsName) {
    this.dsName = dsName;
}

public String getDsType() {
    return dsType;
}

public void setDsType(String dsType) {
    this.dsType = dsType;
}

public String getDsUrl() {
    return dsUrl;
}

public void setDsUrl(String dsUrl) {
    this.dsUrl = dsUrl;
}

public String getDsApiKey() {
    return dsApiKey;
}

public void setDsApiKey(String dsApiKey) {
    this.dsApiKey = dsApiKey;
}

public List<Member> getMembers() {
    return members;
}

public void setMembers(List<Member> members) {
    this.members = members;
}
}

我在@ManyToMany和@JsonBackReference上遇到麻烦。 创建新成员时,出现“创建新成员成功”消息。 但是当我检查数据库时,member_data_source关系表为空。 即使创建一个新的dataSource。我可以创建新成员和dataSource,但是t不链接到表member_data_source。 这是我的控制器:

@PostMapping
public DataSource Create(@Valid @RequestBody DataSource dataSource) {
    return dataSourceService.create(dataSource);
}

任何人都可以帮助我解决此错误。谢谢

0 个答案:

没有答案