2

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

4
  • 1
    Your exception handling is bad. Please throw it (and don't return silly booleans) or at least print its stacktrace instead of only printing the toString() representation of the e instance. The concrete exception and its stacktrace namely contains the answer to your concrete problem. Commented Jun 22, 2012 at 13:16
  • thank you i will try instead of Boolean return nothing and will use a printstackTrace Commented Jun 22, 2012 at 13:30
  • yeah i have tried i got the Exception in below condition getSession().update(tipoModel); Commented Jun 22, 2012 at 13:36
  • Can you please post the code for your entire model class? It appears that much is missing. Also, are there any indexes on the table? Commented Jun 22, 2012 at 19:39

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alex, i found my mistake

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.