Created
April 7, 2011 15:31
-
-
Save andersonfreitas/908007 to your computer and use it in GitHub Desktop.
This file contains 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
@Entity | |
@FilterDef( name="tenantFilter", parameters=@ParamDef( name="tenantId", type="string" ) ) | |
@Filters( | |
@Filter( name="tenantFilter", condition="tenant_id = :tenantId" ) | |
) | |
@Filter( name="tenantFilter", condition="tenant_id = :tenantId" ) | |
) | |
public class Customer { | |
@Id | |
private Long id; | |
@Column( name="tenant_id", nullable=false, updateable=false ) | |
private String tenantId; | |
} | |
public void demonstrateUsageInDiscriminatorScenario() { | |
// One thing that must be done on all sessions is to enable the tenantbased Hibernate filter | |
// we defined before on the entity. This is usually best handled by some form of "request | |
// interceptor" (HttpServletRequestFilter, etc) to make sure it is done uniformly | |
// Creating an entity | |
Session session = openSession(); | |
session.enableFilter( "tenantFilter" ).setParameter( "tenantId", "sometenantidentifier" ); | |
session.beginTransaction(); | |
Customer customer = new Customer(); | |
... | |
customer.setTenantId( "sometenantidentifier" ); | |
session.persist( customer ); | |
session.getTransaction().commit(); | |
session.close(); | |
// Querying Customers | |
session = openSession(); | |
session.enableFilter( "tenantFilter" ).setParameter( "tenantId", "sometenantidentifier" ); | |
session.beginTransaction(); | |
Customer customer = (Customer) session.createQuery( "from Customer" ).uniqueResult(); | |
session.getTransaction().commit(); | |
session.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment