Created
December 8, 2014 12:39
-
-
Save kuhnroyal/8d66ed0601009fea96b2 to your computer and use it in GitHub Desktop.
SelectiveMultitenantPolicy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.eclipse.persistence.descriptors.ClassDescriptor; | |
| import org.eclipse.persistence.descriptors.MultitenantPolicy; | |
| import org.eclipse.persistence.descriptors.SingleTableMultitenantPolicy; | |
| import org.eclipse.persistence.expressions.Expression; | |
| import org.eclipse.persistence.expressions.ExpressionBuilder; | |
| import org.eclipse.persistence.internal.helper.DatabaseField; | |
| import org.eclipse.persistence.internal.sessions.AbstractSession; | |
| public class SelectiveMultitenantPolicy extends SingleTableMultitenantPolicy { | |
| public SelectiveMultitenantPolicy(final ClassDescriptor desc) { | |
| super(desc); | |
| } | |
| @Override | |
| public void postInitialize(AbstractSession session) { | |
| if (includeTenantCriteria) { | |
| Expression expression = getDescriptor().getQueryManager().getAdditionalJoinExpression(); | |
| ExpressionBuilder builder = (expression == null) ? new ExpressionBuilder() : expression.getBuilder(); | |
| for (DatabaseField discriminatorField : tenantDiscriminatorFields.keySet()) { | |
| String property = tenantDiscriminatorFields.get(discriminatorField); | |
| // Add the tenant discriminator field context property as the parameter. | |
| // Do not initialize the database field with the property as it could be tenant.id | |
| // and we do not want to de-qualify it. | |
| DatabaseField newField = new DatabaseField(); | |
| newField.setName(property, session.getPlatform()); | |
| Expression tenantIdExpression = builder.and(builder.getField(discriminatorField).equal(builder.getProperty(newField)) | |
| .or(builder.getProperty(newField).equal(-1L))); | |
| if (expression == null) { | |
| expression = tenantIdExpression; | |
| } else { | |
| expression = expression.and(tenantIdExpression); | |
| } | |
| } | |
| getDescriptor().getQueryManager().setAdditionalJoinExpression(expression); | |
| } | |
| } | |
| @Override | |
| public MultitenantPolicy clone(ClassDescriptor descriptor) { | |
| SelectiveMultitenantPolicy clonedPolicy = new SelectiveMultitenantPolicy(descriptor); | |
| clonedPolicy.includeTenantCriteria = includeTenantCriteria; | |
| clonedPolicy.tenantDiscriminatorFields = tenantDiscriminatorFields; | |
| return clonedPolicy; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @kuhnroyal. I like the approach in this gist, which from my understanding would solve the problem described on stackoverflow: http://stackoverflow.com/questions/22562770/eclipselink-how-to-turn-off-multi-tenancy-even-if-i-annotate-entity-with-multi
Do you have an example how to plug in your MultitenantPolicy into eclipselink?