-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Error when updating a compound key
PostPosted: Mon May 19, 2008 2:47 am 
Newbie

Joined: Mon May 19, 2008 2:30 am
Posts: 2
Hibernate version: 3.2.5
Database name and version: PostgreSQL, version: 8.3.0

Hi there,

I've been dealing with this for several hours and I would really appreciate any help that you could provide me.
I've 2 entities, one called "Empleados" (Employees) and another one called "Ausencias" (Absences).
I have created their respective entities as follows:
Code:
@Entity
@Table(name = "empleados")
@NamedQueries({@NamedQuery(name = "Empleados.findByIdEmpleado", query = "SELECT e FROM Empleados e WHERE e.idEmpleado = :idEmpleado"), @NamedQuery(name = "Empleados.findByNombre", query = "SELECT e FROM Empleados e WHERE e.nombre = :nombre"), @NamedQuery(name = "Empleados.findByApellido", query = "SELECT e FROM Empleados e WHERE e.apellido = :apellido"), @NamedQuery(name = "Empleados.findByFechaNac", query = "SELECT e FROM Empleados e WHERE e.fechaNac = :fechaNac"), @NamedQuery(name = "Empleados.findBySueldo", query = "SELECT e FROM Empleados e WHERE e.sueldo = :sueldo"), @NamedQuery(name = "Empleados.findByIdJefe", query = "SELECT e FROM Empleados e WHERE e.idJefe = :idJefe")})
public class Empleados implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "id_empleado", nullable = false)
    private Long idEmpleado;
    @Column(name = "nombre")
    private String nombre;
    @Column(name = "apellido", nullable = false)
    private String apellido;
    @Column(name = "fecha_nac")
    @Temporal(TemporalType.DATE)
    private Date fechaNac;
    @Column(name = "sueldo")
    private BigInteger sueldo;
    @Column(name = "id_jefe")
    private BigInteger idJefe;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "empleados")
    private Collection<Ausencias> ausenciasCollection;
    @JoinColumn(name = "id_categoria", referencedColumnName = "id_categoria")
    @ManyToOne
    private Categorias idCategoria;
    @JoinColumn(name = "id_sector", referencedColumnName = "id_sector")
    @ManyToOne
    private Sector idSector;

    public Empleados() {
    }

    public Empleados(Long idEmpleado) {
        this.idEmpleado = idEmpleado;
    }

Code:
@Entity
@Table(name = "ausencias")
@NamedQueries({@NamedQuery(name = "Ausencias.findByIdEmpleado", query = "SELECT a FROM Ausencias a WHERE a.ausenciasPK.idEmpleado = :idEmpleado"), @NamedQuery(name = "Ausencias.findByFecha", query = "SELECT a FROM Ausencias a WHERE a.ausenciasPK.fecha = :fecha")})
public class Ausencias implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected AusenciasPK ausenciasPK;
    @JoinColumn(name = "id_empleado", referencedColumnName = "id_empleado", insertable = false, updatable = false)
    @ManyToOne
    private Empleados empleados;

    public Ausencias() {
    }

    public Ausencias(AusenciasPK ausenciasPK) {
        this.ausenciasPK = ausenciasPK;
    }

Code:
@Embeddable
public class AusenciasPK implements Serializable {
    @Column(name = "id_empleado", nullable = false)
    private Integer idEmpleado;
    @Column(name = "fecha", nullable = false)
    @Temporal(TemporalType.DATE)
    private Date fecha;

    public AusenciasPK() {
    }

    public AusenciasPK(Integer idEmpleado, Date fecha) {
        this.idEmpleado = idEmpleado;
        this.fecha = fecha;
    }


Then, after creating an EntityManager object and quering the database a few times, I'll like to update an employee's abscense date.
This is how I'm trying to do it:
Code:
    public void updateAusencia(Integer id, java.sql.Date old, java.sql.Date newDate) {
                       
        try {
            manager.getTransaction().begin();

            AusenciasPK a = new AusenciasPK(id, old);
            AusenciasPK a2 = new AusenciasPK(id, newDate);

            Ausencias ausencia = manager.getReference(Ausencias.class, a);                       
            ausencia.setAusenciasPK(a2);                       
           
            manager.getTransaction().commit();

        } catch (HibernateException hbe) {
            manager.getTransaction().rollback(); 
            hbe.printStackTrace();
        } 
    }


And, I get the following exception:

Full stack trace of any exception that occurs:
Code:
Exception in thread "AWT-EventQueue-1" javax.persistence.PersistenceException: org.hibernate.HibernateException: identifier of an instance of edu.up.RRHH.model.dao.Ausencias was altered from edu.up.RRHH.model.dao.AusenciasPK[idEmpleado=126, fecha=1987-01-27] to edu.up.RRHH.model.dao.AusenciasPK[idEmpleado=126, fecha=1987-03-29]
        at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:630)
        at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:300)
        at edu.up.RRHH.model.ConsultaHiber.updateAusencia(ConsultaHiber.java:423)
        at edu.up.RRHH.model.table.MyTableModelAusencias.setValueAt(MyTableModelAusencias.java:128)
        at javax.swing.JTable.setValueAt(JTable.java:2662)
        at javax.swing.JTable.editingStopped(JTable.java:4661)
        at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.java:125)
        at javax.swing.DefaultCellEditor$EditorDelegate.stopCellEditing(DefaultCellEditor.java:330)
        at javax.swing.DefaultCellEditor.stopCellEditing(DefaultCellEditor.java:215)
        at javax.swing.JTable$GenericEditor.stopCellEditing(JTable.java:5415)
        at javax.swing.DefaultCellEditor$EditorDelegate.actionPerformed(DefaultCellEditor.java:347)
        at javax.swing.JTextField.fireActionPerformed(JTextField.java:492)
        at javax.swing.JTextField.postActionEvent(JTextField.java:705)
        at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:820)
        at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1636)
        at javax.swing.JComponent.processKeyBinding(JComponent.java:2849)
        at javax.swing.JComponent.processKeyBindings(JComponent.java:2884)
        at javax.swing.JComponent.processKeyEvent(JComponent.java:2812)
        at java.awt.Component.processEvent(Component.java:5815)
        at java.awt.Container.processEvent(Container.java:2058)
        at java.awt.Component.dispatchEventImpl(Component.java:4410)
        at java.awt.Container.dispatchEventImpl(Container.java:2116)
        at java.awt.Component.dispatchEvent(Component.java:4240)
        at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
        at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:697)
        at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:962)
        at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:834)
        at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:661)
        at java.awt.Component.dispatchEventImpl(Component.java:4282)
        at java.awt.Container.dispatchEventImpl(Container.java:2116)
        at java.awt.Component.dispatchEvent(Component.java:4240)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Caused by: org.hibernate.HibernateException: identifier of an instance of edu.up.RRHH.model.dao.Ausencias was altered from edu.up.RRHH.model.dao.AusenciasPK[idEmpleado=126, fecha=1987-01-27] to edu.up.RRHH.model.dao.AusenciasPK[idEmpleado=126, fecha=1987-03-29]
        at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:58)
        at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:164)
        at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:120)
        at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
        at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
        at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
        at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:297)
        ... 36 more


I've read that it may be caused by difference of types between the database and the session (eg.: column id_empleado in database is bigint and in entity is Long). However, after changing the values several times nothing has changed and the exception is still thrown.
Please, any help will be MUCH appreciated.

Thanks,

Pablo


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.