我正在尝试创建一个 RESTful 服务。对于多租户,我正在应用 @AdditionalCriteria 注释。但是,当我使用 @OneToOne 注释加入实体时,会引发以下异常:
Exception [EclipseLink-6174] (Eclipse Persistence Services - 2.3.0.v20110604-
r9504): org.eclipse.persistence.exceptions.QueryException
Exception Description: No value was provided for the session property
[SECURITYID]. This exception is possible when using additional criteria or
tenant discriminator columns without specifying the associated contextual
property. These properties must be set through Entity Manager, Entity Manager
Factory or persistence unit properties. If using native EclipseLink, these
properties should be set directly on the session.
Query: ReadObjectQuery(name="readObject" referenceClass=Addresses
sql="SELECT id, city, country, postalcode, province, security_id, street
FROM addresses WHERE ((id = ?) AND (security_id = ?))")
我在 EntityManager 级别设置属性 [SECURITYID] 并且没有加入一切正常。但是,当我使用 @OneToOne 加入实体时,我发现该属性不存在。我没有足够的经验来确定这是由于我做错了什么还是这是一个错误。对我来说,它看起来像是一个不同的 EntityManager 用于获取加入的实体。但我猜是因为我缺乏知识。我还尝试在 EntityManagerFactory 级别设置属性,但无济于事。
这是我的设置。实体:
@Entity
@AdditionalCriteria("this.securityId=:SECURITYID")
@Table(name = "tasks", catalog = "catalog", schema = "schema")
@XmlRootElement
@NamedQueries(
{
@NamedQuery(name = "Tasks.findAll", query = "SELECT t FROM Tasks t")
})
public class Tasks implements Serializable
{
...
@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name="service_address_id")
private Addresses serviceAddress;
...
}
RESTFacade 类
@Stateless
@Path("tasks")
public class TasksFacadeREST extends AbstractFacade<Tasks>
{
@PersistenceContext(unitName = "UnitName")
private EntityManager em;
...
@java.lang.Override
protected EntityManager getEntityManager()
{
// Temp! REMOVE WHEN DONE
sessionId = "123456789";
Identifier.setIdentity(em,sessionId);
em.setProperty("SECURITYID",Identifier.securityId);
em.setProperty("USERID",Identifier.userId);
return em;
}
谢谢和Rgds,M