[英]Hibernate: getting data from two linked tables using Ctriteria API
How can I get data from two linked tables (one-to-many: one User and many Results) by value ' ispassed ' (boolean) using Ctriteria API? 如何使用Ctriteria API通过值“ ispasssed ”(布尔值)从两个链接表(一对多:一个用户和多个结果)中获取数据?
private List<?> winners;
try {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session hSession = factory.openSession();
Transaction tx = null;
try {
tx = hSession.beginTransaction();
winners = hSession.createSQLQuery("select * from usertable u, resulttable r where u.id = r.id where r.ispassed = true").list();
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
} finally {
hSession.close();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(winners.size()); // an exception
You can use HQL: 你可以使用HQL:
from usertable u, resulttable r where u.id = r.id
where r.ispassed = 1
This will return a list of [User,result] arrays. 这将返回[User,result]数组的列表。
Change you code like: 改变你的代码如:
private List<?> winners;
try {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session hSession = factory.openSession();
Transaction tx = null;
try {
tx = hSession.beginTransaction();
winners = hSession.createSQLQuery("from usertable u, resulttable r where u.id = r.id and r.ispassed = true").list();
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
} finally {
hSession.close();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(winners.size());
EDIT: 编辑:
CriteriaBuilder b = em.getCriteriaBuilder();
CriteriaQuery<Tuple> c = b.createTupleQuery();
Root<EntityX> entityXRoot= c.from(EntityX.class);
Root<EntityY> entityYRoot = c.from(EntityY.class);
List<Predicate> predicates = new ArrayList<>();
//Here you need to add the predicates you need
List<Predicate> andPredicates = new ArrayList<>();
andPredicates.add(b.equal(entityXRoot.get("id"), entityYRoot.get("id")));
andPredicates.add(b.and(predicates.toArray(new Predicate[0])));
c.multiselect(entityXRoot, entityYRoot);
c.where(andPredicates.toArray(new Predicate[0]));
TypedQuery<Tuple> q = em.createQuery(criteria);
List<Tuple> result = q.getResultList();
You can create your entity class like below 您可以像下面一样创建实体类
@Entity
@Table(name="RESULTS")
public class Results implements Serializable {
@Id
@GeneratedValue()
@Column(name="ID")
private Long id;
@ManyToOne
@JoinColumn(name="USER_ID")
private User userId;
@Column(name = "IS_PASSED")
private Boolean ispassed;
other property
... getter() setter()
}
@Entity
@Table(name="USER")
public class User implements Serializable {
@Id
@GeneratedValue()
@Column(name="ID")
private Long id;
@OneToMany(mappedBy = "userId",cascade=CascadeType.ALL)
private Set<Results> resultsSet;
other property
... getter() setter()
}
And in your hibernate.cfg.xml file if set below property 并在你的hibernate.cfg.xml文件中设置如下属性
<property name="hibernate.query.substitutions">true 1, false 0</property>
Execute below HQL query 在HQL查询下面执行
String sql = "from User as user "
+ "inner join user.resultsSet"
+ "where resultsSet.ispassed= true";
Query query = getCurrentSession().createQuery(sql);
List<User> UserList = query.list();
above is how you can get List of User, now you need to Iterate User list and use getter method get all results. 以上是如何获得用户列表,现在你需要迭代用户列表并使用getter方法获取所有结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.