1

I really would like to be able to delete a record from a single table that has a composite primary key. Here is my code for the composite key(getters/setters omitted):

@Embeddable
public class MISExceptionStoreUploadControlPK implements Serializable {
    /**
     * Serializable class - generated UID
     */
    private static final long serialVersionUID = -2589068978210044521L;

    @Column(name="STORE_NO", nullable=false)
    private String storeNo;

    @Column(name="EXTRACT_TIME", nullable=false)
    private String extractTime;

Here is my code for the Entity (getters/setters omitted):

@Entity
@Table(name="SDSUSER_OWNER.MIS_EXCEPTION_STORE_UPLD_CNTRL")
public class MISExceptionStoreUploadControl implements Serializable {
    /**
     * Serializable class - generated UID
     */
    private static final long serialVersionUID = 545018012527944915L;

    //@Id
    @EmbeddedId
    private MISExceptionStoreUploadControlPK misExceptionStoreUploadControlPK;

    @Column(name="UNDER_EXTRACT_DAYS", nullable=true)
    private Integer underExtractDays;

    @Column(name="UNDER_WEIGHT_RESERVE", nullable=false)
    private Integer underWeightReserve;

Here is the code for trying to delete a record:

// Retrieve session from Hibernate
        Session session = sessionFactory.getCurrentSession();

        MISExceptionStoreUploadControlPK recordToBeDeleted = (MISExceptionStoreUploadControlPK) session.load(MISExceptionStoreUploadControlPK.class, misExceptionStoreUploadControl.getMisExceptionStoreUploadControlPK());
        session.delete(recordToBeDeleted);

This is the error I get: nested exception is org.hibernate.MappingException: Unknown entity: com.classifieds.beans.volumetrics.MISExceptionStoreUploadControlPK

Editing the 'delete' code to use MISExceptionStoreUploadControl object doesn't work either. I get an error, which says that MISExceptionStoreUploadControlPK object was expected.

If it helps - I can confirm that a simple retrieval of all the records from the table is working.

I would be grateful for some advice on this one...THANKS!

3
  • I wouldn't advice using @Embeddable it means you can logically create two objects of the table hibernate created for you. That means the tables are not normalized. I would suggest to have a @OneToOne relationship rather. Commented Oct 21, 2014 at 10:51
  • Why do you want to remove PK object? PK object is not an entity and can not be removed explicitly. You can remove it only with entity associated with this PK object. Commented Oct 21, 2014 at 10:54
  • Dibya / Ivan, Thanks for getting back to me so quickly. Can you provide a little more asistance, please? Where do I put @OneToOne and how exactly do I delete a record from the Entity? Thanks Commented Oct 21, 2014 at 10:58

2 Answers 2

1

I am not straightly providing a solution to your answer rather I will tell you how you can delete.

First when you do a session.delete(object); One query(select) is fired to get the persistance state of the object than delete query is fired to delete the record.

You can delete records by the following ways.

  1. session.delete(object); - This will delete the record but you need to construct the object
  2. Using Query - This would be my choice as I can have as many conditions I want to have using "and"

    Query query = session.createQuery("delete from TABLE where COL1 = :value");
    query.setParameter("value", 10);
    
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Dibya - thanks again for taking a look at this issue. Yes, using Query would certainly work. I actually got my own code to work, with a minor correction. Posted below...
0

After some more attempts, I got the code I had to work. For everyone else, here is the corrected code:

// Retrieve session from Hibernate
        Session session = sessionFactory.getCurrentSession();

        MISExceptionStoreUploadControl recordToBeDeleted = (MISExceptionStoreUploadControl) session.load(MISExceptionStoreUploadControl.class, misExceptionStoreUploadControlPK);
        session.delete(recordToBeDeleted);

Hopefully it is useful!

Comments

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.