2

I have been having problems with a entity manager merge() method.

There is two entities 'FOO' and 'BAR' they have no relationship in the schema it self and has to stay that way, sadly I cant change it. There is a column in FOO called PROCESS_CD which is exactly the same as the CLASS_CD in BAR. CLASS_CD is the primary key of BAR but PROCESS_CD in FOO is not a foreign key.

I want to update FOO's value in the PROCESS_CD from X to Y but hibernate tries to change the value in BAR as well and thus the error occurs.

I don't know how to specify that it should only update in FOO but not in BAR

So here is my database schema

Database Schema

My foo class

@javax.persistence.Entity
@Table(name = "FOO")
public class FooImpl implements Foo {

    private Long callCode;
    private Journal journal;

    @Id
    @Column(name = "CALL_CD")
    public Long getCallCode() {
        return callCode;
    }

    public void setCallCode(Long aLong) {
        callCode = aLong;
    }


    @ManyToOne(fetch = FetchType.LAZY, targetEntity = BarImpl.class)
    @JoinColumn(name = "PROCESS_TYPE")
    public Journal getJournal() {
        return journal;
    }
}

My bar class

@javax.persistence.Entity
@Table(name = "BAR")
public class BarImpl implements Bar {

    private String Code;
    private String Description;

    public List<Interaction> interaction;

    @Id
    @Column(name = "CLASS_CD")
    public String getCode() {
        return Code;
    }

    public void setCode(String code) {
        Code = code;
    }

    @Column(name = "CLASS_LONG_DESCR")
    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        Description = description;
    }
}

This is my fooDao class

public class FooDao {

    public void saveFoo(Foo instance) {
        log.debug("update instance");
        try {
            if (getEntityManager().contains(instance)) {
                getEntityManager().merge(instance);
            } else {
                getEntityManager().persist(instance);
            }
            log.debug("update successful");
            return instance;
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public void startTransaction() throws TransactionException{
        EntityTransaction t =  getEntityManager().getTransaction();
        t.begin();
        TransactionManager.setTransaction(t);
    }
    public void commitTransaction()throws TransactionException {
        EntityTransaction t = TransactionManager.retrieveTransaction();
        if(t == null)
            throw new TransactionException("No transaction associated witht his thread");
        t.commit();
    }
    public void rollbackTransaction() throws TransactionException{
        EntityTransaction t = TransactionManager.retrieveTransaction();
        if(t == null)
            throw new TransactionException("No transaction associated witht his thread");
        t.rollback();
    }
}

and then lastly the methods I call to save

Here is the error I get

Caused by: org.hibernate.HibernateException: identifier of an instance of BarImpl was altered from X to Y

I hope you can help as I can't find anything on the internate

Thank you for your time

SandMan
  • 555
  • 2
  • 6
  • 15

1 Answers1

0

First of thank you for everyone that replied to my question, you were all very useful. :D

I found my problem and it resided with my actual implementation. Instead of me creating a new barImpl, I went and searched for one and if it is null it needs to create a new bar but if it founds one it just continues and enters the new values.

See I did not know that entity classes keeps an relationship to the actual record in the database. So when I changed the values of an entity that was found by a DAO it was actually trying to change the value in the database when I tried to save the foo entity.

Have a nice day

SandMan
  • 555
  • 2
  • 6
  • 15