简体   繁体   English

JPA:反向映射字段上的查询子句-未设置参数

[英]JPA: query clause on reverse mapping field - Parameter is not set

I have make this simple domain in order to understand why the following query won't work. 我已经做了这个简单的领域,以了解为什么以下查询将不起作用。

@Entity
public class Owner {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToOne(cascade = CascadeType.ALL)
    private Reverse reverse;

    //getters / setters
}

@Entity
public class Reverse {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToOne(mappedBy = "reverse")
    private Owner owner;

    //getters / setters
}

This test fails: 该测试失败:

@Test
@Transactional
public void testQueryByEntity() throws Exception {
    Reverse reverse = new Reverse();
    Owner owner = new Owner();

    owner.setReverse(reverse);
    entityManager.persist(owner);

    entityManager.flush();
    entityManager.clear();

    Reverse dbReverse =
            em.createQuery("from Reverse r where r.owner = :owner", Reverse.class)
                    .setParameter("owner", owner)
                    .getSingleResult();

    System.out.println("Revrse id: " + dbReverse.getId());
}

with the following exception: 除了以下例外:

Caused by: org.h2.jdbc.JdbcSQLException: Parameter "#2" is not set; SQL statement:
select reverse0_.id as id1_ from T_ORDER reverse0_ where reverse0_.id=? limit ? [90012-156]

Same query from Owner side works: 所有者方面的相同查询有效:

Owner dbOwner = 
    em.createQuery("from Owner o where o.reverse = :reverse", Owner.class)
        .setParameter("reverse", reverse)
        .getSingleResult();

In order to make the query work I need to modify it in this way: 为了使查询工作,我需要以这种方式进行修改:

Reverse dbReverse = 
    em.createQuery("from Reverse r where r.owner.id = :ownerId", Reverse.class)
        .setParameter("ownerId", owner.getId())
        .getSingleResult();

So my question is: 所以我的问题是:

why if in where clause I have this reverse mapping condition, I need to query r.owner.id and not simply r.owner in order to make my query works? 为什么如果where子句我有这个反向映射条件,我需要查询r.owner.id而不是简单地r.owner为了使我的查询工作?

Thank you! 谢谢!

since Owner class is the actual owner of the relationship and hence reverse table's primary key is available inside Owner table as a foreign key. 由于Owner类是owner of the relationship的实际owner of the relationship因此在Owner表中可以将反向表的主键用作外键。

so if you refer reverse from owner table(owner.reverse) you can get it using the mapping reverse id which is available inside Owner table. 因此,如果您从所有者表(owner.reverse)引用反向,则可以使用所有者表中可用的映射反向ID来获取它。

but the reverse mapping is - reverse table will not have Owner table's id in its table and hence if you try to say reverse.owner it cant fetch it from its table. 但是反向映射是-反向表在其表中将没有所有者表的ID,因此,如果您尝试说reverse.owner,则无法从其表中获取它。

so you have to use reverse.owner.id to get the id from opposite table. 因此,您必须使用reverse.owner.id从相反的表中获取ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM