Bug 496325 - ElicpseLink multitenancy bug: tenant id not added in query
Summary: ElicpseLink multitenancy bug: tenant id not added in query
Status: NEW
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 blocker (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-06-17 15:55 EDT by Dave Tiger CLA
Modified: 2022-06-09 10:07 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dave Tiger CLA 2016-06-17 15:55:17 EDT
eclipseLink multitenancy bug: tenant id not added. I tried 2.5.1, 2.6.1 and 2.6.3.

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.
Comment 1 Chris Delahunt CLA 2016-10-15 16:25:51 EDT
I'm not sure this is a bug, though I would have expected some warnings or errors if not since the Multitenant Course entity should be treated as isolated, while I assume Student is shared as described here
http://www.eclipse.org/eclipselink/documentation/2.4/concepts/cache001.htm

What happens if you marked the Student as protected?
Comment 2 Eclipse Webmaster CLA 2022-06-09 10:07:57 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink