在这种情况下,我想将一个数据插入到一个表中,该表有一个主键作为复合键,而复合键的每个部分都是多对一关系。现在,当我尝试插入一个元素时,我得到一个错误,该字段应该有一个默认值。请看下面。
@Entity
@Table(name = "products")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private long id;
}
@Entity
@Table(name = "product_categories")
public class ProductCategory {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private long id;
}
@Entity
@Table(name = "product_2_product_categories")
public class ProductToProductCategories {
@EmbeddedId
private ProductToProductCategoryId productToProductCategoryId;
public ProductToProductCategories(){}
public ProductToProductCategories(ProductToProductCategoryId productToProductCategoryId) {
this.productToProductCategoryId = productToProductCategoryId;
}
public ProductToProductCategoryId getProductToProductCategoryId() {
return productToProductCategoryId;
}
public void setProductToProductCategoryId(ProductToProductCategoryId productToProductCategoryId) {
this.productToProductCategoryId = productToProductCategoryId;
}
}
@Embeddable
public class ProductToProductCategoryId implements Serializable {
@ManyToOne
@JoinColumn(name = "productId", referencedColumnName = "id")
private Product productId;
@ManyToOne
@JoinColumn(name = "productCategoryId", referencedColumnName = "id")
private ProductCategory productCategoryId;
public ProductToProductCategoryId() {
}
public Product getProductId() {
return productId;
}
public void setProductId(Product productId) {
this.productId = productId;
}
public ProductCategory getProductCategoryId() {
return productCategoryId;
}
public void setProductCategoryId(ProductCategory productCategoryId) {
this.productCategoryId = productCategoryId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ProductToProductCategoryId)) return false;
ProductToProductCategoryId that = (ProductToProductCategoryId) o;
return Objects.equals(getProductId(), that.getProductId()) &&
Objects.equals(getProductCategoryId(), that.getProductCategoryId());
}
@Override
public int hashCode() {
return Objects.hash(getProductId(), getProductCategoryId());
}
}
@Repository
public interface Product2ProductCategoryJpaRepository extends JpaRepository<ProductToProductCategories, ProductToProductCategoryId> {
}
现在当我尝试在 product_2_product_categories
桌状
public Boolean saveOrUpdate(Long productId, Long productCategoryId) {
Product product = productJpaRepository.findById(productId).get();
ProductCategory productCategory = productCategoryJpaRepository.findById(productCategoryId).get();
ProductToProductCategoryId productToProductCategoryId = new ProductToProductCategoryId();
productToProductCategoryId.setProductCategoryId(productCategory);
productToProductCategoryId.setProductId(product);
product2ProductCategoryJpaRepository.save(new ProductToProductCategories(productToProductCategoryId));
return true;
}
我听到一个错误说
java.sql.sqlexception:com.mysql.cj.jdbc.exceptions.sqlerror.createsqlexception(sqlerror)中的字段“productid”没有默认值。java:129)~[mysql-connector-java-8.0.19。jar:8.0.19]
1条答案
按热度按时间hgb9j2n61#
创建一个默认值为null的二级构造函数,如果null与0不匹配,则null值将自动替换为自动生成的id