具有以下实体:
@Entity
@Table(name = "NAMES")
public class Name {
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private Long id;
@OneToOne(mappedBy = "name", optional = true)
private Event event;
}
@Entity
@Table(name = "EVENTS")
public class Event{
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private Long id;
@OneToOne(optional = false)
private Name name;
}
以及以下 JPQL 查询:
SELECT n FROM Name n JOIN n.event e WHERE e=:uref
查询通过以下方式执行:
String queryString = "SELECT n FROM Name n JOIN n.event e WHERE e=:uref";
List<Name> result = entityManager.createQuery(queryString, Name.class)
.setParameter("uref", userRef).getResultList();
我收到以下错误:
Hibernate:
select
name0_.id as id1_
from
NAMES name0_
inner join
EVENTS event1_
on name0_.id=event1_.name_id
where
event1_.id=?
1-Aug-2012 3:18:31 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 90012, SQLState: 90012
1-Aug-2012 3:18:31 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Parameter "#1" is not set; SQL statement:
select name0_.id as id1_ from NAMES name0_ inner join EVENTS event1_ on name0_.id=event1_.name_id where event1_.id=? [90012-168]
为什么加入不起作用?
奇怪的是,这个非常相似的查询有效:
SELECT e.name FROM Event e WHERE e=:uref
Hibernate:
select
name1_.id as id1_
from
EVENTS event0_
inner join
NAMES name1_
on event0_.name_id=name1_.id
where
event0_.id=?
这是用 Hibernate 4.1.5.SP1 和 4.1.0.Final 尝试过的
注意:第一个查询在 OpenJPA 中有效