Surya
Surya

Reputation: 23

org.hibernate.TransientObjectException: The given object has a null identifier

I got the below Exception when update my 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) [:]

Model Class :

@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 implementation :

@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;
        }
    }

I got the above exception only when i use the update() method if i use the saveOrUpdate() there is no exception but in saveOrUpdate() method new record has created, its not update the record, Could you please tell me whats the wrong in that

Upvotes: 2

Views: 13118

Answers (1)

Alex Barnes
Alex Barnes

Reputation: 7218

The method in which you're trying to update your entity is annotated as @Transactional(readOnly = true). Is that deliberate? That seems wrong.

The problem is that you've passed an object to Hibernate that doesn't have a row in the database with a matching id.

In DefaultSaveOrUpdateEventListener.getUpdateId Hibernate attempts to the read the identifier from the object you're updating but finds that it's null.

Are you sure that the object you're trying to update was previously saved? Is the @Id null at the point that it's loaded? What is the value of the ID column for this entity in the database? Has anything

Upvotes: 2

Related Questions