jpql查询构造,值1与预期类型不匹配

nx7onnlm  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(330)

你好,我有两个泰伯
1.人员表
2.电话号码表
我正试图实现这样的质疑

SELECT * FROM jobs.phonenumber where Person_ID='1';

但是在jpql中。
我试着做一些像:

String query="SELECT p1 FROM PhoneNumber p1 WHERE p1.person=:id;

以下是我的实体类示例:

@Entity
public class PhoneNumber {
    @Id
    @GeneratedValue
    @Column(name = "phoneId")
    private long id;
    @Column(name = "phoneNumber")
    private int number;
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "Person_ID")
    private Person person;

@Entity
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "personId")
    private long id;
    @Column(name = "personName")
    private String name;
    @Column(name = "personAge")
    private int age;
    @OneToMany(mappedBy = "person")
    private List<PhoneNumber> phones;

我有一个错误:参数值1与预期类型[model.person(n/a)]
我的方法是:

@Path("{id}/phones")
@GET
@Produces(MediaType.TEXT_PLAIN)
public List<PhoneNumber> getPhoneNumbers(@PathParam(value = "id") long id) {
    List<PhoneNumber> resultList = new ArrayList<>();
    EntityManager createEntityManager = emf.createEntityManager();
    String query="select p1 from PhoneNumber p1 where p1.person=:id";
    TypedQuery<PhoneNumber> createNamedQuery = createEntityManager.createQuery(query, PhoneNumber.class);
    createNamedQuery.setParameter("id", id);
    try {
        resultList = createNamedQuery.getResultList();
    } catch (Exception e) {
        System.out.println("Could not retrive phone list");
    }
    return resultList;
}

有人能帮我一下吗,我做错了什么?

im9ewurl

im9ewurl1#

使用以下查询:

select p1 from PhoneNumber p1 where p1.person.id=:id
58wvjzkj

58wvjzkj2#

你要么用 person.id 要查询:

String query="select p1 from PhoneNumber p1 where p1.person.id=:id";

或者先检索此人,然后将其用作参数:

Person person = createEntityManager.find(Person.class, id);

String query="select p1 from PhoneNumber p1 where p1.person=:id";
TypedQuery<PhoneNumber> createNamedQuery = createEntityManager.createQuery(query, PhoneNumber.class);
createNamedQuery.setParameter("id", person);

相关问题