在Hibernate的ManyToMany两个实体中都使用级联是一种好习惯吗?

时间:2018-08-25 21:24:29

标签: java hibernate jpa

我有2个分类,客户和股票(代码在下面)。它们通过ManyToMany关系相互连接。两者都有cascade = {CascadeType.PERSIST, CascadeType.MERGE}

我已经测试过它可以正常工作。我只想知道这是好习惯吗 这样层叠?

客户端类:

import com.sun.javafx.beans.IDProperty;

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

@Entity
@Table(name = "client_table")
public class Client {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "client_seq_generator")
    @SequenceGenerator(name = "client_seq_generator", sequenceName = "client_seq", allocationSize = 3)
    private int id;

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

    @OneToMany(
            mappedBy = "client",
            cascade = CascadeType.ALL,
            orphanRemoval = true
    )
    private List<Contribution> contributions = new ArrayList<Contribution>();

    @ManyToMany(
            cascade = {CascadeType.PERSIST, CascadeType.MERGE}
    )
    @JoinTable(
            name="client_stock_table",
            joinColumns = @JoinColumn(name = "client_id"),
            inverseJoinColumns = @JoinColumn(name = "stock_id")
    )
    private Set<Stock> stocks = new HashSet<Stock>();

    public Client() {}

    public Client(String name)
    {
        this.name = name;
    }



    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Contribution getContribution(int id) {
        return contributions.get(id);
    }

    public void addContributions(Contribution contribution) {
        contribution.setClient(this);
        this.contributions.add(contribution);
    }

    public void removeContributions(Contribution contribution) {
        if(contribution != null && contributions.contains(contribution))
        {
            contribution.setClient(null);
            this.contributions.remove(contribution);
        }

    }

    public Set<Stock> getStocks() {
        return stocks;
    }

    public void addStock(Stock stock)
    {
        if(stock != null)
        {
            stocks.add(stock);
            stock.getClients().add(this);
        }
    }

    public void removeStock(Stock stock)
    {
        if(stock != null)
        {
            stocks.remove(stock);
            stock.getClients().remove(this);
        }
    }

}

股票类别:

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;


@Entity
@Table(name = "stock_table")
public class Stock {
    @Id
    @GeneratedValue(strategy= GenerationType.SEQUENCE, generator = "stock_seq_generator")
    @SequenceGenerator(name = "stock_seq_generator", sequenceName = "stock_seq", allocationSize = 1)
    private int id;

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

    @ManyToMany(
            mappedBy = "stocks",
            cascade = {CascadeType.PERSIST, CascadeType.MERGE}
    )
    private Set<Client> clients = new HashSet<Client>();

    public Stock() {}

    public Stock(String name)
    {
        this.name = name;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<Client> getClients() {
        return clients;
    }

    public void addClient(Client client)
    {
        if(client != null)
        {
            clients.add(client);
            client.getStocks().add(this);
        }
    }

    public void removeClient(Client client)
    {
        if(client != null)
        {
            clients.remove(client);
            client.getStocks().remove(this);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

我认为这不是一种方便的方法。

  1. Client(可能还有Stock)是参考表。可以存在引用Client表作为参考表的其他表。

  2. 您不能使用Hibernate合法地使用联接client_stock_table表。我的意思是没有ClientStock

更好

@ManyToManyClient中删除Stock关联,并通过ClientStocks@ManyToOne关联使用其他持久性类@OneToMany

相关问题