Hibernate:使用 Ctriteria API 从两个链接表中获取数据
Hibernate: getting data from two linked tables using Ctriteria API
如何使用 Ctriteria [=17] 按值 'ispassed'(布尔值)从两个链接表(一对多:一个用户和多个结果)获取数据=]?
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
您可以使用 HQL:
from usertable u, resulttable r where u.id = r.id
where r.ispassed = 1
这将 return 一个 [User,result] 数组的列表。
将您的代码更改为:
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());
编辑:
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();
您可以像下面这样创建实体 class
@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()
}
并且在您的 hibernate.cfg.xml 文件中设置如下 属性
<property name="hibernate.query.substitutions">true 1, false 0</property>
执行下面的 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();
以上是获取用户列表的方法,现在您需要迭代用户列表并使用 getter 方法获取所有结果。
如何使用 Ctriteria [=17] 按值 'ispassed'(布尔值)从两个链接表(一对多:一个用户和多个结果)获取数据=]?
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
您可以使用 HQL:
from usertable u, resulttable r where u.id = r.id
where r.ispassed = 1
这将 return 一个 [User,result] 数组的列表。
将您的代码更改为:
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());
编辑:
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();
您可以像下面这样创建实体 class
@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()
}
并且在您的 hibernate.cfg.xml 文件中设置如下 属性
<property name="hibernate.query.substitutions">true 1, false 0</property>
执行下面的 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();
以上是获取用户列表的方法,现在您需要迭代用户列表并使用 getter 方法获取所有结果。