Spring JpaRepository manyToMany bidirectional应该保存而不是更新

时间:2016-12-15 11:28:00

标签: spring jpa save many-to-many bidirectional

如果得到一个语言表和一个具有多对多关系的系统表:

语言:

@JsonAutoDetect
@Entity
@Table(name = "language")
public class Language implements Serializable {   

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "language_id", nullable = false)
    private int languageId;


    @Column(name = "language_name", nullable = false)
    private String languageName;

    @Column(name = "language_isocode", nullable = false)
    private String languageIsoCode;


    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "system_language", joinColumns = {@JoinColumn(name = "language_id", updatable = false)},
        inverseJoinColumns = {
            @JoinColumn(name = "system_id", updatable = false)}, uniqueConstraints = {
        @UniqueConstraint(columnNames = {
            "language_id",
            "system_id"
        })})
    private List<System> systems;


    public Language() {

    }

    // GETTER & SETTERS 
   // ....

}

系统

@JsonAutoDetect
@Entity
@Table(name = "system")
public class System implements Serializable {


    @Id
    @Column(name = "system_id", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer systemId;


    @Column(name = "system_name", nullable = false, unique = true)
    private String systemName;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "university_id", nullable = false)
    private University university;

    @JoinColumn(name = "calender_id", nullable = false)
    @OneToOne(fetch = FetchType.EAGER)
    private Calendar calender;    

    @OneToMany(mappedBy = "system")
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<SystemUserRole> systemUserRoleList;

    @OneToMany(mappedBy = "system")
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<Role> roleList;

    @OneToOne(mappedBy = "system")
    @LazyCollection(LazyCollectionOption.FALSE)
    private CsmUserEntity csmUserEntity;    


    @ManyToMany(mappedBy = "systems")
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<Language> languages;


    public System() {

    }

   // GETTER & SETTERS 
   // ....

}

当我将第一个数据集(systemId = 1,language_id = 20)写入表格时,一切正常。但是当我尝试使用相同的language_id编写第二个数据集但使用其他system_id(systemId = 2,language_id = 20)时,则会更新现有数据集。但我希望有一个新的数据集。我能做什么?

提前致谢!

0 个答案:

没有答案