hibernate - How can I construct a SetJoin in JPA when there's no Set in my entity member field? -
hibernate - How can I construct a SetJoin in JPA when there's no Set in my entity member field? -
i’m using jpa 2.0, hibernate 4.1.0.final, , mysql 5.5.37. have next entities
@entity @table(name = "user_subscription", uniqueconstraints = { @uniqueconstraint(columnnames = { "user_id", “subscription_id" }) } ) public class usersubscription { @id @column(name = "id") @generatedvalue(generator = "uuid-strategy") private string id; @manytoone @joincolumn(name = "user_id", nullable = false, updatable = true) private user user; @manytoone @joincolumn(name = “subscription_id", nullable = false, updatable = true) private subscription subscription;
and
@entity @table(name = "subscription") public class subscription implements serializable { @id @column(name = "id") @generatedvalue(generator = "uuid-strategy") private string id; @onetoone(fetch = fetchtype.lazy) @joincolumn(name = "product_id") @notnull private product product;
without altering entities, how build jpa criteriabuilder query in user entities not have particular subscription entity “a”, have other subscription entities match same product entity “a”? have tried no avail …
public list<user> finduserswithsubscriptions(subscription subscription) { final list<user> results = new arraylist<user>(); final criteriabuilder builder = m_entitymanager.getcriteriabuilder(); final criteriaquery<usersubscription> criteria = builder.createquery(usersubscription.class); final root<usersubscription> root = criteria.from(usersubscription.class); join<usersubscription, subscription> subscriptionroot = root.join(usersubscription_.subscription); criteria.select(root).where(builder.equal(root.get(usersubscription_.subscription).get(subscription_.product),subscription.getproduct()), builder.notequal(root.get(usersubscription_.subscription), subscription));
i thought if build setjoin user -> subscription entities, “not.in”, i’m not sure how given constraints.
edit: sql produced vlad's post:
select user1_.id id97_, user1_.creator_id creator15_97_, user1_.dob dob97_, user1_.enabled enabled97_, user1_.expiration expiration97_, user1_.first_name first5_97_, user1_.grade_id grade16_97_, user1_.incorrect_logins incorrect6_97_, user1_.last_name last7_97_, user1_.middle_name middle8_97_, user1_.organization_id organiz17_97_, user1_.password password97_, user1_.reset_state reset10_97_, user1_.salutation salutation97_, user1_.temporary_password temporary12_97_, user1_.url url97_, user1_.user_demographic_info_id user18_97_, user1_.user_name user14_97_ sb_user_subscription subscription0_ inner bring together sb_user user1_ on subscription0_.user_id = user1_.id inner bring together cb_subscription subscription2_ on subscription0_.subscription_id = subscription2_.id inner bring together sb_product product3_ on subscription2_.product_id = product3_.id , product3_.id = ? , subscription2_.id <>?
check query:
final criteriabuilder builder = m_entitymanager.getcriteriabuilder(); final criteriaquery<user> criteria = builder.createquery(user.class); final root<usersubscription> root = criteria.from(usersubscription.class); join<usersubscription, user> userjoin = root.join(usersubscription_.user); join<usersubscription, subscription> subscriptionjoin = root.join(usersubscription_.subscription); join<subscription, product> productjoin = subscriptionjoin.join(subscription_.product); criteria .select(userjoin) .where(cb.and( builder.equal(productjoin, subscription.getproduct()), builder.notequal(subscriptionjoin, subscription) ); homecoming entitymanager.createquery(criteria).getresultlist();
the output query looks fine , should select users given subscription.product , different subscription product parent's one.
you seek in sql console, looks fine , validates initial requirement:
that not have particular subscription entity “a”, have other subscription entities match same product entity “a”
hibernate jpa jpa-2.0 criteria
Comments
Post a Comment