- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个包含两个表的数据库:“帐户”和“Collection 夹”。 Collection 夹是一个多对多表。它包含:
listowner (foreign key referencing the Account primary key)
favorite (also a foreign key referencing the Account primary key)
Collection 夹在我的程序中没有自己的类。我只有 Account.java,它包含两套。
private Set<Account> favorites;
private Set<Account> listOwner;
//the getters and setters for these sets
相关映射文件:
<set name="favorites" table="favorites" inverse="true" cascade="all">
<key column="listowner" />
<many-to-many column="favorite" class="Models.Account" />
</set>
<set name="listOwner" table="favorites" cascade="all">
<key column="favorite" />
<many-to-many column="listowner" class="Models.Account" />
</set>
现在,保存到数据库工作正常。我可以用列表所有者保存最喜欢的帐户,并在直接访问数据库时看到他出现。但我无法再次从数据库中获取这些信息。我想要一个帐户的所有 Collection 夹的列表。在 SQL 中,这将是:
SELECT favorite
FROM favorites
WHERE listowner = "Bob"
我目前的尝试:
public static List<Account> getFavorites(Account account)
{
List<Account> list = null;
Transaction tx = null;
try
{
tx = session.beginTransaction();
list = session.createQuery("from Account a where a.listOwner.accountName = :name").setParameter("name", account.getAccountName()).list();
tx.commit();
} catch (Exception e)
{
if (tx != null)
{
tx.rollback();
}
System.out.println("getFavorites failed");
e.printStackTrace();
} finally
{
return list;
}
}
根据调试器,它失败了
list = session.createQuery("from Account a where a.listOwner.accountName = :name").setParameter("name", account.getAccountName()).list();
我做错了什么?我没有遇到任何异常(exception)。
最佳答案
您的查询有误。 a.listOwner
类型为Set<Account>
。还有一个Set<Account>
没有accountName
属性(property)。能够对 a.listOwner
的元素添加限制,您需要显式连接:
select a from Account a
inner join a.listOwner owner
where owner.accountName = :name
也就是说,您的整个方法应该简单地替换为
return account.getFavorites();
关于java - 同一类中的HQL多对多查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19617654/
我有实体和 NamedQuery: @Entity @Table(name="MY_TABLE") @NamedQueries({ @NamedQuery(name="myQuery", quer
hql 中的 row_number() 与分区的等价物是什么 我在 hql 中有以下查询: select s.Companyname, p.Productname, sum(od.Unitprice
hql 函数 current_timestamp() 使用的是来自运行 java 代码的服务器的时间还是来自运行数据库的服务器的时间? 最佳答案 HQL 查询被翻译成 SQL 查询,SQL 查询由数据
我有一个 Hibernate HQL 问题。 我想将子查询编写为派生表(出于性能原因)。 可以在 HQL 中做到这一点吗? 例子: FROM Customer WHERE country.id in
我有三个表 A B 和 C。现在我想在 HQL 中执行这个 sql 查询: select * from A as a left join B as b on a.id = b.id left join
我是 Hive 的新手,想知道如何直接从 .hql 文件执行 hive 命令。 最佳答案 如 @rajshukla4696 所述,hive -f filename.hql 或 beeline -f f
出于各种原因,我正在尝试编写一个部分动态的 HQL 查询,而不求助于 Criteria API。我想知道是否有一种简单的方法可以使用 HQL 表达式来短路 where 限制。例如,这是工作正常的原始查
我正在自学 hibernate ,并且很困惑为什么我不能只编写简单的 SQL 查询。 我发现它比普通 SQL(我习惯的)使用起来更困惑 PLUS:我发现 NetBeans HQL 编辑器非常烦人,对我
非常感谢您帮助将以下 SQL 语句转换为有效的 HQL 语句。我尝试了几个小时但没有成功: SELECT * FROM master as m left outer join (select * fr
我有一个 HQL 查询: query = select item.itemNumber from items item where item.stock>0 and item.price it = q
我在以下代码段中遇到错误 using (var session = Database.OpenSession()) { var q = from x in session.Query()
我正在使用以下 C# 代码: public IList GetAllByExpression(Expression> expression, int startIndex, int count, Fu
我有 HQL,我试图在其中获取没有分类的工件(当 Activity 为 0 时) artifacts = Artifact.findAll("FROM Artifact WHERE id NOT IN
在 JPQL 中,我可以通过以下方式检索实体: query = entityManager.createQuery("select c from Category c"); List categori
我正在尝试使用 HQL 查询更新记录,但我收到了 CastException。如果有人可以帮助我,我将不胜感激。我已经在网上查了一段时间,但我找不到这方面的任何信息。如果您有关于此异常的更多信息,请告
我正在尝试在 JPA 中执行一个 select 语句,这会抛出上述错误。代码是: TypedQuery typedQuery = null; String query = ""; List list
我有问题要执行一个更新 HQL 查询。 我想编辑保存在配置文件实体中的值,但没有运气。 我的实体如下所示 @Entity @Table(name = "users", catalog = "testd
如何设置此 hql 查询的限制?当我在查询中添加 limit 关键字时,会引发错误。 @Query("from voucher v where v.voucherType.typeDescript
假设有两个表,A[a_id, b_id]和 B[b_id,c] . 我需要执行表单 "From A a ORDER BY a.b.c" 的 HQL 查询, 而 b在类 A 中可以为空. 但是,该查询仅
是否可以在 HQL 中运行与此类似的查询而无需指定所有列名。 select med, MAX(med.PrescriptionLines.Prescription.PrescriptionDate)
我是一名优秀的程序员,十分优秀!