【问题标题】:NamedQuery an object and compare it to and integer idNamedQuery 一个对象并将其与整数 id 进行比较
【发布时间】:2017-09-21 03:33:04
【问题描述】:

我有这样的情况, 如果有人可以帮助我解决问题,那就太好了。

@MappedSuperclass
public abstract class AbstractEntity implements Serializable, Cloneable {
    private int id;
}


public class A extends AbstractEntity{ //Both A and B Entities will store in Table A in Database(in the same table)
    //some code
}

public class B extends A{
    //some fields
}

public class C extends AbstractEntity{  //Data will be Store in Table C in database and there's a column name b_id as integer to hold the id of Entity b
    B b;
}

现在我的 NamedQuery 中有这样一个查询

query="SELECT e FROM C e WHERE e.b= :vid"
//int vid;

问题是 JPA 抛出异常,因为 'b' 是 'B' 的类类型 var 而 'vid' 是 Integer var。

我不能说,

query="SELECT e FROM C e WHERE e.b.id= :vid"

因为 'b' 扩展了 'A' 而 'A' 扩展了 AbstractEntity,这就是我的想法,因为我尝试过并得到了一些错误'参数值 [601] 与预期类型不匹配' //601 是 id数据库中表“A”中的实体。

【问题讨论】:

  • AbstractEntity 中的私有成员 id 不会被继承到类 B
  • 我将其更改为受保护但它给了我'没有实体的默认构造函数'错误@alayor

标签: java jpa hql jpql named-query


【解决方案1】:

将映射的超类中的 getter 添加到 id 字段,以便您的类可以访问该字段。这样您就可以在查询中引用该字段。

@MappedSuperclass
public abstract class AbstractEntity implements Serializable, Cloneable {
    private int id;

    public int getId() { return id; }
}

【讨论】:

  • 我的 AbstractEntity 类中还有一个 getter,我也将其更改为 protected int id;仍然不起作用@roque-santos
猜你喜欢
  • 1970-01-01
  • 2019-07-26
  • 1970-01-01
  • 1970-01-01
  • 2018-08-04
  • 2018-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多