EclipseLink

References

  1. http://www.eclipse.org/eclipselink/documentation/2.5/solutions/multitenancy001.htm

MultitenantType.SINGLE_TABLE

import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.eclipse.persistence.annotations.Multitenant;
import org.eclipse.persistence.annotations.MultitenantType;
import org.eclipse.persistence.annotations.TenantDiscriminatorColumn;

@Entity
@Multitenant(MultitenantType.SINGLE_TABLE)
@TenantDiscriminatorColumn(name = "USER_ID")
@Cacheable(false)
public class StockcardEntity extends BaseEntity {

    @ManyToOne
    @JoinColumn(name = "USER_ID", insertable = false, updatable = false)
    private UserEntity user;

}

...

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class StockcardServiceBean extends CrudServiceBean<StockcardEntity, Long> implements StockcardService {

    @PersistenceContext
    private EntityManager em;

    @Inject
    private UserService userService;

    @Override
    protected EntityManager getEntityManager() {
        UserEntity user = userService.getCurrentUser();
        em.setProperty("eclipselink.tenant-id", user.getId());
        return em;
    }

    public StockcardServiceBean() {
        super(StockcardEntity.class);
    }

}

MultitenantType.TABLE_PER_TENANT

MultitenantType.VPD