【问题标题】:Hibernate query composite keyHibernate 查询复合键
【发布时间】:2012-03-18 04:08:27
【问题描述】:

我正在尝试使用 @embeddable 执行复合键查询。

这是我到目前为止所拥有的。

@Embeddable
public class IfasvVendorPK implements Serializable{

@Column(length = 4, nullable = false)
protected String peId;
@Column(length = 8, nullable = false)
protected String peAddrCd;

实体

@Entity
public class IfasvVendor implements Serializable {

@EmbeddedId
private IfasvVendorPK ifasvVendorPK;

查询

列表包含两个 pk。不确定我是否应该为此使用列表。

            Query query = session.createQuery("from IfasvVendor t0 where     t0.ifasvVendorPK.peId=:id");
            query.setParameter("id", list);
            query.list();

我也不确定一旦我的查询工作如何获取对象。

【问题讨论】:

  • 这不是复合键查询,您是通过复合键中的属性进行搜索。请确认这是您要完成的任务。无论如何,假设您要传递属性列表,您当前的查询是不正确的。改用这个 - from IfasvVendor t0 where t0.ifasvVendorPK.peId in :idListquery.setParameter("idList", list)

标签: java mysql hibernate postgresql


【解决方案1】:

我相信以下应该有效:

Query query = session.createQuery("from IfasvVendor t0 where t0.ifasvVendorPK.peId in (:id)");
query.setParameterList("id", list);

query.list();

您必须在查询中将命名参数括在括号中并使用 setParameterList。请参阅 setParameterList here 的 javadoc。

查询结果将在query.list()返回的列表中。这会返回一个未经检查的列表,您可能希望将其转换为 List<IfasvVendor>

顺便说一句。这不是复合键查询。见@Perception 评论...

【讨论】:

  • 我相信你和感知是正确的。我还没有机会回去尝试修复,但它看起来很有希望。
猜你喜欢
  • 2013-11-16
  • 2019-02-25
  • 2015-03-27
  • 2015-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-25
相关资源
最近更新 更多