EntityManager.merge(obj)删除而不是更新或给出null错误

时间:2016-10-14 11:15:36

标签: java hibernate jpa entitymanager

我的实体是这样的:

模板:

@Entity
@Table(name="TEMPLATE")
public class Template extends BaseEntityIdLong {


    private static final long serialVersionUID = 3208743090890588897L;

    @OneToMany(mappedBy = "template", orphanRemoval=true, cascade={CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE}, fetch=FetchType.EAGER)
    @JacksonXmlElementWrapper(useWrapping=false)
    private Set<TemplateControl> templatesSet;

TemplateControl:

@Entity
@Table(name="TEMPLATE_CONTROLS")
public class TemplateControl extends BaseEntityIdLong implements Comparable<TemplateControl>{


    private static final long serialVersionUID = 161243586581385870L;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="template_id", referencedColumnName="id", nullable=true)
    private Template template;  

    @ManyToOne
    @JoinColumn(name="template_control_id", nullable=true)
    private TemplateControl parentControl;

    @OneToMany(mappedBy = "parentControl", orphanRemoval=true, cascade={CascadeType.MERGE, CascadeType.PERSIST}, fetch=FetchType.EAGER)
    @Fetch(FetchMode.SUBSELECT)
    @BatchSize(size = 10)
    private Set<TemplateControl> children = new LinkedHashSet<TemplateControl>();


    /** TemplateControl attributes */
    @OneToMany(mappedBy = "templateControls", orphanRemoval=true, cascade={CascadeType.MERGE, CascadeType.PERSIST}, fetch=FetchType.EAGER)
    @Fetch(FetchMode.SUBSELECT)

    @BatchSize(size = 10)
    private Set<ControlAttribute> controlAttributes;

    @Transient
    private int columnIndex;

    @Transient 
    private GuiAttributes attributes;

ControlAttribute:

@Entity
@Table(name="CONTROL_ATTRIBUTE")
public class ControlAttribute extends BaseEntityIdLong {

    private static final long serialVersionUID = -1454089401068519858L;

    @ManyToOne//(fetch=FetchType.EAGER)
    @JoinColumn(name="template_controls_id", referencedColumnName="id", nullable=false)
    @NotNull
    private TemplateControl templateControls;

我有一个模板,其中包含一些templateControls(组件,如area),而且在区域中我有其他templateControl s(如buttons)我的问题是当我克隆时一个按钮,并将其添加到templateSet的集合中(通常我将parentControl设置为null,并使用模板设置模板属性)并从子项中删除原始文件。在EntityManager.merge(template),有时会删除templateControl和他的ControlAttributes,但应该更新它。有时会出现此错误:

org.postgresql.util.PSQLException: ERROR: null value in column "template_controls_id" violates not-null constraint
  Detail: Failing row contains (17226, null).

哪个来自controlAttribute导致他的templateControl为null但是我在debug中检查过他的templateControl不为null它有一个好的对象。有什么建议我错过了吗?

UPDATE1:

合并方法:

@PersistenceContext(unitName=PersistenceJPAConfig.BEAN_PERSISTENCE_UNIT_NAME)
private EntityManager entityManager;

public T update(T entity) throws DAOException {
    LOGGER.debug("[DAO] Update entity: " + entity);
    try{
        Preconditions.checkNotNull(entity);

        return (T) entityManager.merge(entity);
    }catch(Exception e){
        LOGGER.error("[DAO] Error while updating entity: " + entity, e);
        throw new DAOException(e.getMessage(), e);
    }
}

克隆方法确实可以正常工作我检查了它,同事也使用了,很长时间我不认为如果我发布它会有所帮助。

0 个答案:

没有答案