Micho Rizo
Micho Rizo

Reputation: 1092

Hibernate 5.x delete record based on partial composite id?

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?

Upvotes: 2

Views: 362

Answers (1)

Arthur Noseda
Arthur Noseda

Reputation: 2654

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.

Upvotes: 1

Related Questions