gpt4 book ai didi

java - 如何避免将不存在的外键插入@AssociationOverrides的联接表中

转载 作者:行者123 更新时间:2023-11-30 01:47:54 26 4
gpt4 key购买 nike

我正在为spring boot jpa项目设置数据库。
我的数据库中有Product,Team,Assignment(以及更多)表

数据库必须满足以下规则


一个团队可以属于多种产品
一个产品可以有多个团队


那就是为什么我选择了很多方法

下一个约束是->产品和团队的组合应该是唯一的->这意味着我们有一个团队和产品映射,它告诉我们一个团队可以成为某些指定产品列表的一部分。

在启动过程中,我从excel文件中填写产品和团队映射表

映射从链接到用户表的工作分配表中引用

产品和团队表映射具有更多信息,例如isobsolete,以指定将来不再使用此映射。

这是我的实体类

@Table(name="product")
public class Product {


@Id
@Column(name="id" )
@SequenceGenerator(name="inc", initialValue=1, allocationSize=100)
@GeneratedValue(strategy=GenerationType.IDENTITY,generator="inc")
private int id;


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

@OneToMany(mappedBy="product")
@JsonIgnore
private Set<Assignment> assignments = new HashSet<>();



@OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.product", cascade=CascadeType.ALL)
@NotFound(action = NotFoundAction.EXCEPTION)
private Set<ProductTeam> teams = new HashSet<ProductTeam>(0);


//getters and setters


}



@Entity
@Table(name="team")
public class Team {


@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;

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

@OneToMany(mappedBy="team")
private Set<Assignment> asssignments = new HashSet<>();

//@ManyToMany(mappedBy = "teams")
@OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.team")
@NotFound(action = NotFoundAction.EXCEPTION)
private Set<ProductTeam> products = new HashSet<ProductTeam>(0);

//getters and setters
}






@Embeddable
public class ProductTeamID implements java.io.Serializable{


private static final long serialVersionUID = 1L;

@ManyToOne
private Product product;

@ManyToOne
private Team team;


public Product getProduct() {
return product;
}

public void setProduct(Product product) {
this.product = product;
}

public Team getTeam() {
return team;
}

public void setTeam(Team team) {
this.team = team;
}

public boolean equals(Object o) {

if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

ProductTeamID that = (ProductTeamID) o;

if (product != null ? !product.equals(that.product) : that.product != null) return false;
if (team != null ? !team.equals(that.team) : that.team != null)
return false;

return true;
}

public int hashCode() {
int result;
result = (product != null ? product.hashCode() : 0);
result = 31*13 * result + (team != null ? team.hashCode() : 0);
return result;
}

}



@Entity
@Table(
name = "product_team",
uniqueConstraints = {
@UniqueConstraint(
columnNames = {"product_id","team_id"}
)
}
)
@AssociationOverrides({
@AssociationOverride(
name = "pk.product",
joinColumns = {
@JoinColumn(name = "product_id", referencedColumnName = "id")
}
),
@AssociationOverride(
name = "pk.team",
joinColumns = @JoinColumn(name = "team_id",referencedColumnName = "id")
)
}

)
public class ProductTeam implements java.io.Serializable {

private static final long serialVersionUID = 1L;

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

//@EmbeddedId
private ProductTeamID pk = new ProductTeamID();

@Column(name = "obsolete", nullable = false)
private boolean obsolete;

@Temporal(TemporalType.DATE)
@Column(name = "wef", nullable = true, length = 10)
private Date wef;

public ProductTeam() {
}

public int getId() {
return id;
}

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

@Transient
public Product getProduct() {
return getPk().getProduct();
}

public void setProduct(Product product) {
getPk().setProduct(product);
}

@Transient
public Team getTeam() {
return getPk().getTeam();
}

public void setTeam(Team team) {
getPk().setTeam(team);
}


public ProductTeamID getPk() {
return pk;
}

public void setPk(ProductTeamID pk) {
this.pk = pk;
}

public boolean isObsolete() {
return obsolete;
}

public void setObsolete(boolean obsolete) {
this.obsolete = obsolete;
}

public Date getWef() {
return wef;
}

public void setWef(Date wef) {
this.wef = wef;
}



public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;

ProductTeam that = (ProductTeam) o;

if(that.getTeam()==null||that.getProduct()==null)
return false;
else
return Objects.equals(pk.getTeam().getName(), that.getTeam().getName()) &&
Objects.equals(pk.getProduct().getName(), that.pk.getProduct().getName()) ;
}

public int hashCode() {
return (getPk() != null ? getPk().hashCode() : 0);
}

}




@Entity
@Table(name="assignment")
public class Assignment {

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

@Column(name="allocation")
private double allocation;

@Column(name="actualallocation")
private double actualallocation;

@Column(name="entrydate")
@Temporal(TemporalType.DATE)
private Date entrydate;

@Column(name="exitdate")
@Temporal(TemporalType.DATE)
private Date exitdate;

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

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="user_id")
private User user;


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


@Column(name="islead")
private boolean islead;


@Column(name="coreorplug")
private CoreOrPlug coreorplug;


@OneToOne
@JoinColumn(name="team_product_id")
private ProductTeam productTeam;

//getters and setters
}




问题是我可以在mysql终端的product_team表中插入不存在的productid和teamid

插入product_team值(0,true,NULL,99939,403336);

如何防止在product_team表中插入不存在的产品和团队?

最佳答案

我发现了问题。默认情况下,Spring Boot使用MyIsam引擎,该引擎不会创建外键关联。我将休眠方言更改为org.hibernate.dialect.MySQL5InnoDBDialect,现在存在外键关联,问题不再存在。

关于java - 如何避免将不存在的外键插入@AssociationOverrides的联接表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57272943/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com