Dave
Dave

Reputation: 769

eclipseLink multitenancy bug: tenant id not added

eclipseLink multitenancy bug: tenant id not added. I tried both 2.5.1 and 2.6.1.

For example,

Student and Course: ManyToMany.

Student     Course                        Student_Course
-------     ------------------            -----------------
id, name    id, name, tenantId           studentId, courseId


 public class Student {

        @ManyToMany(...)
        List<Course> getCourses() {
            ...
        }
    } 

Course is multitenancy enabled, but Student is not.

Search student left join course: The generated SQL:

    SELECT DISTINCT t1.ID AS a1, t1.NAME AS a2, t1.VERSION AS a3, t0.ID AS a4, 
t0.tenantId AS a5, t0.NAME AS a6, t0.VERSION AS a7 FROM Student t1 LEFT OUTER 
JOIN (Student_Course t2 JOIN Course t0 ON (t0.ID = t2.courseId)) ON 
(t2.studentId = t1.ID) ORDER BY t1.ID DESC 

The restriction for Course.tenantId is not added into the query.

<entity class="Course" > 
    <multitenant>
        <tenant-discriminator-column name="tenantId" context-property="tenant.id"
            discriminator-type="INTEGER"/>      
    </multitenant>  
</entity> 



EntityManager em = ..
em.setProperty("tenant.id", 1);

CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Student> criteriaQuery = criteriaBuilder.createQuery(Student.class);
Root<Student> root = criteriaQuery.from(Student.class);
root.fetch("courses", JoinType.LEFT);

criteriaQuery.distinct(true);
TypedQuery<Student> query = em.createQuery(criteriaQuery);
List<Student> students = query.getResultList();

The courses of a retrieved student will contain all the courses of the student from all tenants. But only the courses from tenant 1 is wanted.

Is there any workaround? Thanks.

Upvotes: 1

Views: 236

Answers (1)

Andre Brito
Andre Brito

Reputation: 146

It seems there was a bug Bug 345962 - Join fetch query when using tenant discriminator column fails.

that has been solved in version 2.2.1 of javax.persistence which can be found on version 2.7.3 of org.eclipse.persistence

mvn javax.persistence

Upvotes: 3

Related Questions