在两个实体中映射相同的列表并使用Spring Data更新列表

时间:2015-09-08 15:01:54

标签: java spring hibernate jpa spring-data

我有2个实体SellBuy,每个实体都有Offer列表,如下所示。

我怀疑的是我如何在Offer中映射这两个实体。我是否必须为每个属性添加一个带有@ManyToOne注释的属性?我当前的映射是否正确?

另外,当我在Offer列表中添加一个新元素时,如何使用Spring Data更新它? 我正在使用JpaRepository的存储库,但没有找到更新或合并bean的方法。

卖实体:

@Entity
public class Sell implements Serializable {

    @Id
    @GeneratedValue(generator = "sell_sequence", strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name = "sell_sequence", sequenceName = "sell_sequence", allocationSize = 1)
    private Long id;

    // other fields...

    @OneToMany(cascade = { CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST })
    @JoinTable(name = "selloffer",
        joinColumns = @JoinColumn(name = "sell_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "offer_id", referencedColumnName = "id"))
    private List<Offer> offers = new ArrayList<>();

    // constructors, getters and setters...
}

购买实体:

@Entity
public class Buy implements Serializable {

    @Id
    @GeneratedValue(generator = "buy_sequence", strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name = "buy_sequence", sequenceName = "buy_sequence", allocationSize = 1)
    private Long id;

    // other fields...

    @OneToMany(cascade = { CascadeType.REMOVE })
    @JoinTable(name = "buyoffer",
        joinColumns = @JoinColumn(name = "buy_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "offer_id", referencedColumnName = "id"))
    private List<Offer> offers;

    // constructors, getters and setters...
}

优惠:

@Entity
public class Offer implements Serializable {

    @Id
    @GeneratedValue(generator = "offer_sequence", strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name = "offer_sequence", sequenceName = "offer_sequence", allocationSize = 1)
    private Long id;

    @Column(length = 500)
    String description;

    @ManyToOne(cascade = { CascadeType.PERSIST })
    @JoinColumn(name = "bidder_id", nullable = false)
    User bidder;
}

1 个答案:

答案 0 :(得分:1)

虽然我推测,你的关系应该是OneToOne(或者很多连接表),没有连接表,这似乎可以解决你的问题,只需检查商品类中的连接列(不确定它们是否正确)为你的情况)

@Entity
public class Sell implements Serializable {

    @Id
    @GeneratedValue(generator = "sell_sequence", strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name = "sell_sequence", sequenceName = "sell_sequence", allocationSize = 1)
    private Long id;

    // other fields...

    @OneToMany(cascade = { CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST },mappedBy = "sell")

    private List<Offer> offers = new ArrayList<>();

    // constructors, getters and setters...
}

@Entity
public class Buy implements Serializable {

    @Id
    @GeneratedValue(generator = "buy_sequence", strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name = "buy_sequence", sequenceName = "buy_sequence", allocationSize = 1)
    private Long id;

    // other fields...

    @OneToMany(cascade = { CascadeType.REMOVE },mappedBy = "buy")
    private List<Offer> offers;

    // constructors, getters and setters...
}

@Entity
public class Offer implements Serializable {

    @Id
    @GeneratedValue(generator = "offer_sequence", strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name = "offer_sequence", sequenceName = "offer_sequence", allocationSize = 1)
    private Long id;

    @Column(length = 500)
    String description;

    @ManyToOne(cascade = { CascadeType.PERSIST })
    @JoinColumn(name = "bidder_id", nullable = false)
    User bidder;

    @ManyToOne
    @JoinTable(name = "selloffer",
        joinColumns =  @JoinColumn(name = "offer_id", referencedColumnName = "id"),
        inverseJoinColumns =@JoinColumn(name = "sell_id", referencedColumnName = "id"))
    private Sell sell;

    @ManyToOne
    @JoinTable(name = "buyoffer",
        joinColumns = @JoinColumn(name = "offer_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "buy_id", referencedColumnName = "id"))
    private Buy buy;
}