使用查询生成器从另一个 table 中查询 table 中的所有行 link
query all row in table that is link from another table using query builder
我有一个 table,其中只有几行变量。这一行 link 到另一行 table 有更多行。
说白了,1table叫做Connection。变量是:
name, groupname, etc.
组名应该 link 到第二个 table 称为 ConnectionGroup。变量是:
name, id.
我的想法是通过名称查询 ConnectionGroup table。 Connection和ConnectionGroup的抽象class是这样的:
public abstract class Connection_ {
public static volatile SingularAttribute<Connection, String>name;
public static volatile SingularAttribute<Connection, String>host;
public static volatile SetAttribute<Connection, ConnectionGroup>connectionGroups;
}
public abstract class ConnectionGroup_ {
public static volatile SingularAttribute<ConnectionGroup, String> name;
public static volatile SingularAttribute<ConnectionGroup, Long> id;
}
要查询这个,我假设我必须加入这 2 个 table,然后只查询它们。这是我试过的代码:
@PersistenceContext
private EntityManager em;
public List<Connection> retrieveAll( String groupFilter, int start, int length) {
ServiceUtil.requireAdmin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Connection> q = cb.createQuery(Connection.class);
Root<Connection> c = q.from(Connection.class);
Join<Connection, ConnectionGroup> join = c.join(Connection_.connectionGroups);
q.select(c);
c.fetch(Connection_.connectionGroups).fetch(ConnectionGroup_.id);
Predicate groupPredicate = cb.equal(
c.get(Connection_.connectionGroups), "%" + groupFilter + "%");
q.where(groupPredicate);
List<Connection> results = em.createQuery(q).setFirstResult(start)
.setMaxResults(length).getResultList();
for (Connection conn : results) {
logger.info( "getconnectionGroups =["+ conn.getConnectionGroups() + "]");
for (ConnectionGroup conngroup : conn.getConnectionGroups()) {
logger.info("connectiongroups = [" + conngroup.getName() + "]");
}
}
}
我尝试过的东西,改变这个:
Predicate groupPredicate = cb.equal(
c.get(Connection_.connectionGroups), "%" + groupFilter + "%");
对此:
Predicate groupPredicate = cb.equal(join.get(ConnectionGroup_.name),
"%" + groupFilter + "%");
改变这个:
Join<Connection, ConnectionGroup> join = c
.join(Connection_.connectionGroups);
为此:
Join<Connection, ConnectionGroup> join = c.join("connectionGroups");
当我尝试这些方法时,我总是收到异常 Cannot join to attribute of basic type
我也试过把代码改成这样:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Connection> q = cb.createQuery(Connection.class);
Root<Connection> c = q.from(Connection.class);
Join<Connection, ConnectionGroup> join = (Join<Connection, ConnectionGroup>) c.fetch(Connection_.connectionGroups);
q.select(c);
Predicate groupPredicate = cb.equal(join.get(ConnectionGroup_.name), "%" + groupFilter + "%");
q.where(groupPredicate);
List<Connection> results = em.createQuery(q).setFirstResult(start)
.setMaxResults(length).getResultList();
其中return一个例外:
query specified join fetching, but the owner of the fetched association was not present in the select list
我使用这些站点作为编写代码的参考:
JPA CriteriaBuilder using fetch joins and result objects
JPA 2 Criteria Fetch Path Navigation
如何查询 ConnectionGroup table 中的名称?我的方法错了吗?
如果您尝试通过字符串匹配使用名称过滤,CriteriaBuilder.like() 比 CriteriaBuilder.equal() 更合适。
要实现您要查询的内容,您可以使用 ff.日联QL查询:
SELECT DISTINCT conn FROM Connection conn JOIN conn.connectionGroups connGrp
WHERE connGrp.name LIKE :groupFilter
将 JP QL 转换为 CriteriaQuery,您将拥有:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Connection> q = cb.createQuery(Connection.class);
Root<Connection> conn = q.from(Connection.class);
Join<Connection, ConnectionGroup> connGrp = conn.join(Connection_.connectionGroups);
q.select(conn).distinct(true);
ParameterExpression<String> param = cb.parameter(String.class, "%"+ groupFilter + "%");
q.where(cb.like(connGrp.get(ConnectionGroup_name), param));
我有一个 table,其中只有几行变量。这一行 link 到另一行 table 有更多行。
说白了,1table叫做Connection。变量是:
name, groupname, etc.
组名应该 link 到第二个 table 称为 ConnectionGroup。变量是:
name, id.
我的想法是通过名称查询 ConnectionGroup table。 Connection和ConnectionGroup的抽象class是这样的:
public abstract class Connection_ {
public static volatile SingularAttribute<Connection, String>name;
public static volatile SingularAttribute<Connection, String>host;
public static volatile SetAttribute<Connection, ConnectionGroup>connectionGroups;
}
public abstract class ConnectionGroup_ {
public static volatile SingularAttribute<ConnectionGroup, String> name;
public static volatile SingularAttribute<ConnectionGroup, Long> id;
}
要查询这个,我假设我必须加入这 2 个 table,然后只查询它们。这是我试过的代码:
@PersistenceContext
private EntityManager em;
public List<Connection> retrieveAll( String groupFilter, int start, int length) {
ServiceUtil.requireAdmin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Connection> q = cb.createQuery(Connection.class);
Root<Connection> c = q.from(Connection.class);
Join<Connection, ConnectionGroup> join = c.join(Connection_.connectionGroups);
q.select(c);
c.fetch(Connection_.connectionGroups).fetch(ConnectionGroup_.id);
Predicate groupPredicate = cb.equal(
c.get(Connection_.connectionGroups), "%" + groupFilter + "%");
q.where(groupPredicate);
List<Connection> results = em.createQuery(q).setFirstResult(start)
.setMaxResults(length).getResultList();
for (Connection conn : results) {
logger.info( "getconnectionGroups =["+ conn.getConnectionGroups() + "]");
for (ConnectionGroup conngroup : conn.getConnectionGroups()) {
logger.info("connectiongroups = [" + conngroup.getName() + "]");
}
}
}
我尝试过的东西,改变这个:
Predicate groupPredicate = cb.equal(
c.get(Connection_.connectionGroups), "%" + groupFilter + "%");
对此:
Predicate groupPredicate = cb.equal(join.get(ConnectionGroup_.name),
"%" + groupFilter + "%");
改变这个:
Join<Connection, ConnectionGroup> join = c
.join(Connection_.connectionGroups);
为此:
Join<Connection, ConnectionGroup> join = c.join("connectionGroups");
当我尝试这些方法时,我总是收到异常 Cannot join to attribute of basic type
我也试过把代码改成这样:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Connection> q = cb.createQuery(Connection.class);
Root<Connection> c = q.from(Connection.class);
Join<Connection, ConnectionGroup> join = (Join<Connection, ConnectionGroup>) c.fetch(Connection_.connectionGroups);
q.select(c);
Predicate groupPredicate = cb.equal(join.get(ConnectionGroup_.name), "%" + groupFilter + "%");
q.where(groupPredicate);
List<Connection> results = em.createQuery(q).setFirstResult(start)
.setMaxResults(length).getResultList();
其中return一个例外:
query specified join fetching, but the owner of the fetched association was not present in the select list
我使用这些站点作为编写代码的参考:
JPA CriteriaBuilder using fetch joins and result objects
JPA 2 Criteria Fetch Path Navigation
如何查询 ConnectionGroup table 中的名称?我的方法错了吗?
如果您尝试通过字符串匹配使用名称过滤,CriteriaBuilder.like() 比 CriteriaBuilder.equal() 更合适。
要实现您要查询的内容,您可以使用 ff.日联QL查询:
SELECT DISTINCT conn FROM Connection conn JOIN conn.connectionGroups connGrp
WHERE connGrp.name LIKE :groupFilter
将 JP QL 转换为 CriteriaQuery,您将拥有:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Connection> q = cb.createQuery(Connection.class);
Root<Connection> conn = q.from(Connection.class);
Join<Connection, ConnectionGroup> connGrp = conn.join(Connection_.connectionGroups);
q.select(conn).distinct(true);
ParameterExpression<String> param = cb.parameter(String.class, "%"+ groupFilter + "%");
q.where(cb.like(connGrp.get(ConnectionGroup_name), param));