0

我正在尝试创建一个 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

4

2 回答 2

1

我的猜测是这与缓存有关。

通常,租户属性将在 EntityManagerFactory 上设置,而不是在 EntityManager 上。这意味着每个租户都有不同的 EntityManagerFactory。您还可以在您的 persistence.xml 中定义租户属性,并为每个租户提供不同的持久性单元。

如果需要为每个 EntityManager 设置租户,则需要禁用共享缓存,或者将缓存模式设置为受保护。

http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Single-Table_Multi-Tenancy#Setting_Properties_and_Caching_Scope

于 2012-04-30T13:45:14.843 回答
1

作为参考,要禁用的属性是eclipselink.cache.shared.default

<property name="eclipselink.cache.shared.default" value="false"/>
于 2014-11-03T11:40:13.963 回答