org.hibernate.TransientObjectException:给定对象具有空标识符

时间:2012-06-22 13:11:54

标签: spring hibernate

在更新我的Modelclass

时,我得到了以下异常
18:27:15,203 ERROR [com.sinergia.ea.daoimpl.TypeOfArtifactDaoImpl]  ERROR Exception in updateTypeOfArtifact() : o
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.getUpdateId(DefaultSaveOrUpdateEventListener.
        at org.hibernate.event.def.DefaultUpdateEventListener.getUpdateId(DefaultUpdateEventListener.java:46) [:3
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventList
        at org.hibernate.event.def.DefaultUpdateEventListener.performSaveOrUpdate(DefaultUpdateEventListener.java
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListen
        at org.hibernate.impl.SessionImpl.fireUpdate(SessionImpl.java:564) [:3.2.6.ga]
        at org.hibernate.impl.SessionImpl.update(SessionImpl.java:552) [:3.2.6.ga]
        at org.hibernate.impl.SessionImpl.update(SessionImpl.java:544) [:3.2.6.ga]
        at com.sinergia.ea.daoimpl.TypeOfArtifactDaoImpl.updateTypeOfArtifact(TypeOfArtifactDaoImpl.java:67) [:]

模特课程:

@Entity
@Table(name="TYPE_OF_ARTIFACT")
public class TypeOfArtifactModel implements java.io.Serializable , Identifiable{

    /**
     * 
     */
    private static final long serialVersionUID = 2662289176706818360L;



    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "TYPE_OF_ARTIFACT_SEQ")
    @SequenceGenerator(name = "TYPE_OF_ARTIFACT_SEQ", sequenceName = "TYPE_OF_ARTIFACT_SEQ")
    @Column(name="ID",unique=true, nullable=false)
    private Integer id;

    @Column(name="DESCRIPTION", nullable=true, length=400)
    private String description;

    @Column(name="NAME", nullable=false, length=50)
    private String name;

    @OneToMany(fetch = FetchType.LAZY, targetEntity = AdditionalInfoModel.class, mappedBy = "typeOfArtifactID")
    private Set<AdditionalInfoModel> additionalInfos = new HashSet<AdditionalInfoModel>(0);


    @OneToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "TYPE_ARTIFACT_OPERATE_RELATION", joinColumns = { @JoinColumn(name = "TYPE_OF_ARTIFACT_ID") }, inverseJoinColumns = { @JoinColumn(name = "OPERATE_ARTIFACT_ID") })
    private Set<TypeOfArtifactModel> checkedItems = new HashSet<TypeOfArtifactModel>(0);

    @Column(name="FLAG",length=1)
    boolean editable;

    public TypeOfArtifactModel() {
    }

DaoImppl实施:

@Override
    @Transactional(readOnly = true)
    public Boolean updateTypeOfArtifact(@NotNull final TypeOfArtifactModel tipoModel,final Set<AdditionalInfoModel> additionalInfos,final Set<TypeOfArtifactModel> checkedItems) {
        try {
            System.out.println("Dao Impl Name :"+tipoModel.getName());
            System.out.println("Dao Impl Description :"+tipoModel.getDescription());
            System.out.println("Dao Impl CheckedItems :"+tipoModel.getCheckedItems());          

            if(additionalInfos !=null && !(additionalInfos.isEmpty())){             
                for(AdditionalInfoModel item : additionalInfos){
                    getSession().update(item);
                }
                tipoModel.setAdditionalInfos(additionalInfos);
            }
            getSession().update(tipoModel);
            return Boolean.TRUE;

        } catch (Exception e) {
            log.error(" ERROR Exception in updateTypeOfArtifact() ", e);
            return Boolean.FALSE;
        }
    }

我只有在使用update()方法时才得到上述异常,如果我使用saveOrUpdate()没有异常但是在saveOrUpdate()方法中创建了新记录,它没有更新记录,请你告诉我那是什么意思

1 个答案:

答案 0 :(得分:2)

您尝试更新实体的方法注释为@Transactional(readOnly = true)。这是故意的吗?这似乎不对。

问题是你已经将一个对象传递给Hibernate,该对象在数据库中没有一行具有匹配的id。

DefaultSaveOrUpdateEventListener.getUpdateId中,Hibernate尝试从您正在更新的对象中读取标识符,但发现它为空。

您确定您之前尝试更新的对象已保存吗? @Id是否在加载时为空?数据库中此实体的ID列的值是多少?有什么