How to access multiple tenants in eclipselink?

白昼怎懂夜的黑 提交于 2019-12-01 00:49:34

EclipseLink has an open feature request to allow a better way of allowing an admin server to access multi tenant data: https://bugs.eclipse.org/bugs/show_bug.cgi?id=355458 - vote for it if it is important to you.

The way around it would be to create a separate persistence unit for your admin console. One way to go about this would be to move the multitenant metadata to an EclipseLink orm.xml file, and use it in your tenant persitence units, while the admin persistence unit just use the entity classes. You might want a field in the entities that can be mapped to the tenant column that the admin console could use and query against, but be read-only or inaccessible to the tenant specific persistence units.

I found an alternative approach that I will try out: Instead of using the @Multitenant annotation to filter out those entities that belong to my tenant I will use @AdditionalCriteria. My example from the question above becomes

@Entity
@AdditionalCriteria(":ADMINACCESS = 1 or this.tenant=:TENANT")
public class TenantEntity {

    private String tenant;
    ...

Here I need to take care of the tenant column myself. I can create two entity managers. One for tenant access:

private static EntityManager newEntityManager(String tenant) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("TENANT", tenant);
    map.put("ADMINACCESS", 0);
    return emf.createEntityManager(map);
}

And another one for admin access:

private static EntityManager newEntityManager() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("TENANT", "");
    map.put("ADMINACCESS", 1);
    return emf.createEntityManager(map);
}

See details for @AdditionalCriteria here. Any comments?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!