java – JPA illegalStateException – CascadeType问题

前端之家收集整理的这篇文章主要介绍了java – JPA illegalStateException – CascadeType问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我希望有人可以为我阐明这一点.我真的不明白这个错误出了什么问题.我有一个实体用于在数据库中创建许多条目.当事务结束并完成同步时,我得到一个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
最佳答案
异常清楚地表明ProductBase引用未保存的Categories实例.

如果它是故意的并且您实际上想要保存新的类别实例,则可以为该关系配置级联,或者在保存ProductBase之前手动保存该实例.

否则,如果ProductBase应引用类别的现有持久实例,则需要从数据库加载该实例而不是创建新实例.

猜你在找的Java相关文章