title: How to use Hibernate multi-tenancy from JPA
body: <p>I'm able to use hibernate multi-tenancy directly using hibernate but my requirement is to use hibernate from jpa.
  Using Hibernate SessionFactory from hibernate :</p>
  
  <pre><code> SessionFactory factory1=HibernateUtil.getSession();
        Session session1=factory1.withOptions().tenantIdentifier("db2").openSession();
        session1.beginTransaction();
  </code></pre>
  
  <p>With JPA</p>
  
  <pre><code>    EntityManagerFactory factory=JpaUtil.getEntityManagerFactory();
      EntityManager em=factory.createEntityManager();
      em.getTransaction().begin(); 
  </code></pre>
  
  <p>with this i'm getting following as expected EntityManager creating SessionFactory without <code>factory1.withOptions().tenantIdentifier("db2").openSession();</code></p>
  
  <pre><code>Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.HibernateException: SessionFactory configured for multi-tenancy, but no tenant identifier specified
  </code></pre>
  
  <p>How can i resolve this error ? or Is there some other way to use Hibernate Multi-tenancy with JPA?</p>
  
  <p>my persistence.xml file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0"
  xmlns="http://java.sun.com/xml/ns/persistence"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
  http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
  
  &lt;persistence-unit name="myJpa" transaction-type="RESOURCE_LOCAL"&gt;
     &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
      &lt;class&gt;com.nuc.jpa.MyEntity&lt;/class&gt;
      &lt;properties&gt;
          &lt;property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/db1" /&gt;
          &lt;property name="javax.persistence.jdbc.user" value="root" /&gt;
          &lt;property name="javax.persistence.jdbc.password" value="root123" /&gt;
          &lt;property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="hibernate.hbm2ddl.auto" value="update" /&gt;
          &lt;property name="hibernate.multiTenancy" value="SCHEMA" /&gt;
          &lt;property name="hibernate.multi_tenant_connection_provider" value="com.nuc.MultiTenantConnectionProvider" /&gt;
          &lt;property name="hibernate.tenant_identifier_resolver" value="com.nuc.MCurrentTenantIdentifierResolver"/&gt; 
          &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p></p>