M.Minbashi
M.Minbashi

Reputation: 352

Hibernate inheritance update problem when generate HT table

I have a problem about Hibernate 4.3.11 , I have two pojos : X and Y that Y extends X , Actually Y is a subjoin class in X.hbm . When i excute this update on Y pojo , All of the Y records have updated ,but I want to update Y where id is 123

This my HQL :

string hql = " update Y set Y.name = :name where e.id = :id "  
Query query = getSession().createQuery(hql);  
query.setParameter("name",name);  
query.setParameter("id",123);  
query.executeUpdate();  

When it excutes query it generates this SQL command :

update Y set Y.name = ? where (X_Id) IN(select X_Id from HT_Y)

Moreover I don't want to use createSQLQuery(...) , Do you have any solution to help me?

Upvotes: 2

Views: 101

Answers (1)

Pallav Jha
Pallav Jha

Reputation: 3619

Shouldn't this query work?

String hql = "update Y set Y.name = :name where Y.id = :id"

Also, instead of createQuery, CriteriaBuilder can be used

CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaUpdate<Y> criteria = builder.createCriteriaUpdate(Y.class);
Root<Y> root = criteria.from(Y.class);
criteria.set(root.get("name"), name);
criteria.where(builder.equal(root.get("id"), 123));
session.createQuery(criteria).executeUpdate();

Upvotes: 2

Related Questions