使用以下实体:
@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