我希望有人可以为我阐明这一点。我真的不明白这个错误出了什么问题。我有一个实体用于在数据库中创建许多条目。当事务结束并完成同步时,我得到一个illegalStateException。这一切都在从提供所有数据的Web服务填充数据库的过程中。现在,我以一种不会导致任何外键约束错误的方式解析数据,但与实体的关系阻止了我。
Caused by: java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: entities.cmic.ajrs.com.Categories[pkId=null].
我试图坚持的实体叫做ProductBase。 ProductBase有一个名为pkBrand的外键到Brand_Data表,并带有相应的实体BrandData。
我可以为这些关系将CascadeType设置为ALL,但我真的不想在此时创建该实体。
以下是实体(减去一些吸气剂和制定者)
@Entity
@Table(name = "product_base")
@NamedQueries({
@NamedQuery(name = "ProductBase.findAll", query = "SELECT p FROM ProductBase p"),
@NamedQuery(name = "ProductBase.findByPkId", query = "SELECT p FROM ProductBase p WHERE p.pkId = :pkId"),
@NamedQuery(name = "ProductBase.findByColorsAvail", query = "SELECT p FROM ProductBase p WHERE p.colorsAvail = :colorsAvail"),
@NamedQuery(name = "ProductBase.findBySeriesName", query = "SELECT p FROM ProductBase p WHERE p.seriesName = :seriesName"),
@NamedQuery(name = "ProductBase.findByStatusCodes", query = "SELECT p FROM ProductBase p WHERE p.statusCodes = :statusCodes"),
@NamedQuery(name = "ProductBase.findByTs", query = "SELECT p FROM ProductBase p WHERE p.ts = :ts")})
public class ProductBase implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "pk_id")
private Integer pkId;
@Column(name = "colors_avail")
private String colorsAvail;
@Column(name = "series_name")
private String seriesName;
@Column(name = "status_codes")
private String statusCodes;
@Basic(optional = false)
@Column(name = "ts")
@Temporal(TemporalType.TIMESTAMP)
private Date ts;
@OneToMany(mappedBy = "productBase", fetch = FetchType.LAZY)
private Collection<ProductSorts> productSortsCollection;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "productBase", fetch = FetchType.LAZY)
private MapTarget mapTarget;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "productBase", fetch = FetchType.LAZY)
private KeyFeatures keyFeatures;
@JoinColumn(name = "pk_category", referencedColumnName = "pk_id")
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private Categories categories;
@JoinColumn(name = "pk_brand", referencedColumnName = "pk_id")
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private BrandData brandData;
@OneToMany(mappedBy = "productBase", fetch = FetchType.LAZY)
private Collection<PromotionsByModel> promotionsByModelCollection;
@OneToMany(mappedBy = "productBase", fetch = FetchType.LAZY)
private Collection<ProductEnhancedFeatures> productEnhancementFeaturesCollection;
@OneToMany(mappedBy = "productBase", fetch = FetchType.LAZY)
private Collection<SkuBasic> skuBasicCollection;
@OneToMany(mappedBy = "productBase", fetch = FetchType.LAZY)
private Collection<ProductMeasurements> productMeasurementsCollection;
public ProductBase() {
}
@Entity
@Table(name = "brand_data")
@NamedQueries({
@NamedQuery(name = "BrandData.findAll", query = "SELECT b FROM BrandData b"),
@NamedQuery(name = "BrandData.findByPkId", query = "SELECT b FROM BrandData b WHERE b.pkId = :pkId"),
@NamedQuery(name = "BrandData.findByCommonBrandId", query = "SELECT b FROM BrandData b WHERE b.commonBrandId = :commonBrandId"),
@NamedQuery(name = "BrandData.findByCommonBrandName", query = "SELECT b FROM BrandData b WHERE b.commonBrandName = :commonBrandName"),
@NamedQuery(name = "BrandData.findByTs", query = "SELECT b FROM BrandData b WHERE b.ts = :ts")})
public class BrandData implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "pk_id")
private Integer pkId;
@Column(name = "common_brand_id")
private String commonBrandId;
@Column(name = "common_brand_name")
private String commonBrandName;
@Basic(optional = false)
@Column(name = "ts")
@Temporal(TemporalType.TIMESTAMP)
private Date ts;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "brandData", fetch = FetchType.LAZY)
private Collection<ProductBase> productBaseCollection;
public BrandData() {
}
答案 0 :(得分:4)
异常明确表示ProductBase
引用了未保存的Categories
实例。
如果是故意的并且您确实想要保存Categories
的新实例,则可以为该关系配置级联,或者在保存ProductBase
之前手动保存该实例。
否则,如果ProductBase
应该引用Categories
的现有持久化实例,则需要从数据库加载该实例而不是创建新实例。