У меня есть 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);
}
}
}