2

I have an embedded composite key that consist of two column

@Embeddable
public static class PK implements Serializable {

    private static final long serialVersionUID = 4049628068378058196L;

    @Column(name="colA", length=32, nullable=false)
    private String colA;

    @Column(name = "colB", length=32, nullable=false)
    private String colB;

   //constructors/getters/setters
}

Is there any way I can delete a persistent object using only colB value such as

 Serializable id = new String(myColBValue);

 Object persistentInstance = session.get(MyObject.class, id);
 if (persistentInstance != null) {
            session.delete(persistentInstance);    
 }

or do I have to resort to using HQL statement?

MWiesner
  • 8,868
  • 11
  • 36
  • 70
Micho Rizo
  • 1,000
  • 3
  • 12
  • 27

1 Answers1

1

If you need 2 columns to identify MyObject, then providing just one won't be enough to lookup a unique result every time. Neither Session nor EntityManager provide the method you're looking for. You'll have to go with Criteria or Query.

Arthur Noseda
  • 2,534
  • 19
  • 28
  • okay, but how to delete using hql now? PK is a private entity in my entity object. Seems it can't resolve the column. Hibernate 5 is really more trouble than it's work with lack of documentation. session.createQuery("delete from MyObject where PK.colB= :colb") .setParameter("colb", "foobar") .executeUpdate(); reults in a "could not execute statement" error – Micho Rizo Aug 03 '16 at 16:49