{"id":252637,"date":"2015-03-08T19:09:03","date_gmt":"2015-03-08T15:09:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=252637"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=252637","title":{"rendered":"Hibernate, multi-tenancy \u0438 \u0430\u0432\u0442\u043e-\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0445\u0435\u043c\u044b \u0411\u0414"},"content":{"rendered":"<p> \t\t\t\u0425\u043e\u0442\u0438\u0442\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430\u043c\u0438 (\u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0430\u043c\u0438) \u0430\u0432\u0442\u043e-\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0445\u0435\u043c\u044b \u0411\u0414 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 Hibernate, \u043d\u043e \u0443 \u0432\u0430\u0441 \u0438\u043c\u0435\u0435\u0442\u0441\u044f multi-tenant \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430? \u0414\u043e\u0431\u0440\u043e \u043f\u043e\u0436\u0430\u043b\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434 \u043a\u0430\u0442.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h4>\u0412 \u0447\u0435\u043c, \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430?<\/h4>\n<p>  \u0415\u0441\u043b\u0438 \u0432 \u0432\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f Hibernate \u0438 \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0445\u0435\u043c\u044b \u0411\u0414 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 multi-tenant \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b \u0441\u043e \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0435\u0439 schema-per-tenant, \u0432\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043e\u043f\u0446\u0438\u044e \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0445\u0435\u043c\u044b:<\/p>\n<pre><code class=\"xml\">&lt;property name=&quot;hbm2ddl.auto&quot;&gt;update&lt;\/property&gt; <\/code><\/pre>\n<p>  \u0420\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043f\u043e\u0447\u0435\u043c\u0443. \u041f\u0440\u0438\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044c \u0438\u043c\u0435\u0435\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 multi-tenancy \u0432 Hibernate.<\/p>\n<h4>\u041a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f<\/h4>\n<p>  \u0411\u0414: Postgres 9.2<br \/>  ORM: Hibernate 4.3.7.Final + Envers<br \/>  Multi-tenancy \u0441\u0442\u0440\u0430\u0434\u0435\u0433\u0438\u044f: schema-per-tenant \u0441 \u043e\u0431\u0449\u0438\u043c JDBC data source.<\/p>\n<p>  \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 Hibernate:<\/p>\n<pre><code class=\"xml\">&lt;!--&lt;property name=&quot;hbm2ddl.auto&quot;&gt;update&lt;\/property&gt;--&gt; &lt;property name=&quot;connection.datasource&quot;&gt;java:\/portalDS&lt;\/property&gt; &lt;property name=&quot;hibernate.dialect&quot;&gt;by.tychina.PostgreSQL9MultiTenantDialect&lt;\/property&gt; &lt;property name=&quot;hibernate.multiTenancy&quot;&gt;SCHEMA&lt;\/property&gt; &lt;property name=&quot;hibernate.multi_tenant_connection_provider&quot;&gt;by.tychina.tenant.HibernateMultiTenantConnectionProvider&lt;\/property&gt; &lt;property name=&quot;hibernate.tenant_identifier_resolver&quot;&gt;by.tychina.tenant.HibernateTenantIdentifierResolver&lt;\/property&gt; &lt;property name=&quot;org.hibernate.envers.audit_strategy&quot;&gt;org.hibernate.envers.strategy.ValidityAuditStrategy&lt;\/property&gt; <\/code><\/pre>\n<p>  <\/p>\n<h4>\u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b<\/h4>\n<p>  \u0414\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 tenant \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043a\u043b\u0430\u0441\u0441 TenantId:<\/p>\n<pre><code class=\"java\">package by.tychina.tenant;  \/**  * @author Sergey Tychina  *\/ public class TenantId {      \/**      * Represents database schema name      *\/     private String tenantId;      public TenantId(String tenantId) {         this.tenantId = tenantId;     }      public String getTenantId() {         return tenantId;     } } <\/code><\/pre>\n<p>, \u0433\u0434\u0435 \u043f\u043e\u043b\u0435 tenantId \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0441\u0445\u0435\u043c\u044b \u0432 \u0411\u0414.<\/p>\n<p>  \u041a\u043b\u0430\u0441\u0441 Persistence, \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0438\u0439 \u0437\u0430 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e Hibernate \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e SessionFactory:<\/p>\n<pre><code class=\"java\">package by.tychina;  import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;  \/**  * @author Sergey Tychina  *\/ public class Persistence {      private final Configuration configuration;      private final SessionFactory sessionFactory;      private static Persistence instance = null;      private Persistence() {         configuration = new Configuration();         configuration.configure();         sessionFactory = configuration.buildSessionFactory();     }      public static synchronized Persistence getInstance() {         if (instance == null) {             instance = new Persistence();         }          return instance;     }      public Configuration getConfiguration() {         return configuration;     }      public SessionFactory getSessionFactory() {         return sessionFactory;     } } <\/code><\/pre>\n<p>  \u041a\u043b\u0430\u0441\u0441 ConnectionProvider, \u043d\u0435\u043e\u0445\u043e\u0434\u0438\u043c\u044b\u0439 \u0434\u043b\u044f \u043e\u043a\u0440\u044b\u0442\u0438\u044f \u043a\u043e\u043d\u043d\u0435\u043a\u0446\u0438\u0438 \u043a \u0411\u0414, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f data source:<\/p>\n<pre><code class=\"java\">package by.tychina;  import by.tychina.tenant.TenantId;  import javax.naming.InitialContext; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException;  \/**  * @author Sergey Tychina  *\/ public class ConnectionProvider {      private static final String DATASOURCE_JNDI_NAME = &quot;java:\/portalDS&quot;;      private static DataSource dataSource = null;      private static synchronized DataSource getDataSource() {         if (dataSource == null) {             try {                 dataSource = (DataSource) new InitialContext().lookup(DATASOURCE_JNDI_NAME);             } catch (Exception e) {                 throw new RuntimeException(e);             }         }          return dataSource;     }      public static Connection getConnection() {         try {             return getDataSource().getConnection();         } catch (SQLException e) {             throw new RuntimeException(&quot;Unable to get connection&quot;, e);         }     }      public static Connection getConnection(TenantId tenantId) {         Connection connection = getConnection();         try {             connection.createStatement().execute(&quot;SET SCHEMA '&quot; + tenantId.getTenantId() + &quot;'&quot;);         } catch (SQLException e) {             try {                 connection.close();             } catch (SQLException e1) {                 e1.printStackTrace();             }             throw new RuntimeException(&quot;Could not alter connection to '&quot; + tenantId.getTenantId() + &quot;' schema&quot;, e);         }          return connection;     } } <\/code><\/pre>\n<p>  Hibernate \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0411\u0414 \u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u043c tenant \u0438 \u0431\u0435\u0437 \u043d\u0435\u0433\u043e (\u043e\u043f\u0446\u0438\u044f hibernate.multi_tenant_connection_provider):<\/p>\n<pre><code class=\"java\">package by.tychina.tenant;  import by.tychina.ConnectionProvider; import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;  import java.sql.Connection; import java.sql.SQLException;  \/**  * @author Sergey Tychina  *\/ public class HibernateMultiTenantConnectionProvider implements MultiTenantConnectionProvider {      @Override     public Connection getAnyConnection() throws SQLException {         return ConnectionProvider.getConnection();     }      @Override     public void releaseAnyConnection(Connection connection) throws SQLException {         connection.close();     }      @Override     public Connection getConnection(String tenantIdentifier) throws SQLException {         return ConnectionProvider.getConnection(new TenantId(tenantIdentifier));     }      @Override     public void releaseConnection(String tenantIdentifier, Connection connection) throws SQLException {         releaseAnyConnection(connection);     }      @Override     public boolean supportsAggressiveRelease() {         return false;     }      @Override     public boolean isUnwrappableAs(Class unwrapType) {         return false;     }      @Override     public &lt;T&gt; T unwrap(Class&lt;T&gt; unwrapType) {         return null;     } } <\/code><\/pre>\n<p>  Hibernate \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0439 \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e tenant (\u043e\u043f\u0446\u0438\u044f hibernate.tenant_identifier_resolver):<\/p>\n<pre><code class=\"java\">package by.tychina.tenant;  import org.hibernate.context.spi.CurrentTenantIdentifierResolver;  \/**  * @author Sergey Tychina  *\/ public class HibernateTenantIdentifierResolver implements CurrentTenantIdentifierResolver {     @Override     public String resolveCurrentTenantIdentifier() {         \/\/ Put some logic here to get current transaction tenant id         \/\/ Use ThreadLocal for example to store TenantId when starting a transaction         TenantId transactionTenantId = null;          return transactionTenantId.getTenantId();     }      @Override     public boolean validateExistingCurrentSessions() {         return true;     } } <\/code><\/pre>\n<p>  <\/p>\n<h4>Postres \u0434\u0438\u0430\u043b\u0435\u043a\u0442 \u0434\u043b\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 multi-tenancy<\/h4>\n<p>  \u0412 \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0432 \u043c\u0430\u043f\u043f\u0438\u043d\u0433\u0430\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f sequence \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f id, \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 PostgreSQL9Dialect, \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0439 Hibernate, \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c update \u0441\u043a\u0440\u0438\u043f\u0442\u044b \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f sequence. \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043a\u0440\u043e\u0435\u0442\u0441\u044f \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043c\u0435\u0442\u043e\u0434\u0435 \u043a\u043b\u0430\u0441\u0441\u0430 org.hibernate.dialect.PostgreSQL81Dialect:<\/p>\n<pre><code class=\"java\">public String getQuerySequencesString() {     return &quot;select relname from pg_class where relkind='S'&quot;; } <\/code><\/pre>\n<p>  \u0414\u0430\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f Hibernate \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 sequence, \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u043a\u0430\u043a\u0438\u0435 update \u0441\u043a\u0440\u0438\u043f\u0442\u044b \u043d\u0430\u0434\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0439 SQL \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u0435\u0440\u043d\u0435\u0442 sequence \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0441\u0445\u0435\u043c, \u0430 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0442\u043e\u0439, \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u044b \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c update \u0441\u043a\u0440\u0438\u043f\u0442\u044b. \u0420\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u0434\u0430\u043d\u043d\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u043d\u0430\u043c\u0438 \u0434\u0438\u0430\u043b\u0435\u043a\u0442:<\/p>\n<pre><code class=\"java\">package by.tychina;  import org.hibernate.dialect.PostgreSQL9Dialect;  \/**  * @author Sergey Tychina  *\/ public class PostgreSQL9MultiTenantDialect extends PostgreSQL9Dialect {      \/**      * Query to get sequences for current schema ONLY. Original method returns query to get all sequences (in all schemas)      * that breaks multi-tenancy.      *\/     @Override     public String getQuerySequencesString() {         return &quot;SELECT c.relname FROM pg_catalog.pg_class c &quot; +                 &quot;LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace &quot; +                 &quot;WHERE c.relkind IN ('S','') &quot; +                 &quot;AND n.nspname &lt;&gt; 'pg_catalog' &quot; +                 &quot;AND n.nspname &lt;&gt; 'information_schema' &quot; +                 &quot;AND n.nspname !~ '^pg_toast' &quot; +                 &quot;AND pg_catalog.pg_table_is_visible(c.oid)&quot;;     } } <\/code><\/pre>\n<p>  \u0422\u0430\u043a\u043e\u0439 SQL \u0437\u0430\u043f\u0440\u043e\u0441 \u0431\u044b\u043b \u043f\u043e\u043b\u0443\u0447\u0435\u043d \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 psql \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c -E \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u043c \u043a\u043e\u043c\u043c\u0430\u043d\u0434\u044b \\ds:<\/p>\n<pre><code class=\"sql\">demo_portal=&gt; \\ds ********* QUERY ********** SELECT n.nspname as &quot;Schema&quot;,   c.relname as &quot;Name&quot;,   CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' END as &quot;Type&quot;,   pg_catalog.pg_get_userbyid(c.relowner) as &quot;Owner&quot; FROM pg_catalog.pg_class c      LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN ('S','')       AND n.nspname &lt;&gt; 'pg_catalog'       AND n.nspname &lt;&gt; 'information_schema'       AND n.nspname !~ '^pg_toast'   AND pg_catalog.pg_table_is_visible(c.oid) ORDER BY 1,2; ************************** <\/code><\/pre>\n<p>  \u0412 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 Hibernate \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0434\u0438\u0430\u043b\u0435\u043a\u0442:<\/p>\n<pre><code class=\"xml\">&lt;property name=&quot;hibernate.dialect&quot;&gt;by.tychina.PostgreSQL9MultiTenantDialect&lt;\/property&gt; <\/code><\/pre>\n<p>  <\/p>\n<h4>\u0410\u0432\u0442\u043e-\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0445\u0435\u043c\u044b<\/h4>\n<p>  \u0412 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 Hibernate \u0437\u0430\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0430 \u043e\u043f\u0446\u0438\u044f hbm2ddl.auto, \u0442\u0430\u043a \u043a\u0430\u043a Hibernate \u043d\u0435 \u0443\u043c\u0435\u0435\u0442 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u0441\u0445\u0435\u043c\u0443 \u0411\u0414 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u0439 multi-tenancy \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438. \u0414\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0445\u0435\u043c\u044b \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0441\u0432\u043e\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e (\u043f\u043e\u0447\u0442\u0438 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0445 \u043a\u043e\u0434\u043e\u0432 Hibernate):<\/p>\n<pre><code class=\"java\">package by.tychina.schema;  import by.tychina.ConnectionProvider; import by.tychina.Persistence; import by.tychina.tenant.TenantId; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.dialect.Dialect; import org.hibernate.engine.jdbc.internal.FormatStyle; import org.hibernate.engine.jdbc.internal.Formatter; import org.hibernate.tool.hbm2ddl.DatabaseMetadata; import org.hibernate.tool.hbm2ddl.SchemaUpdateScript;  import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import java.util.List;  \/**  * @author Sergey Tychina  *\/ public class MultiTenantHibernateSchemaUpdate {      public static void execute(TenantId tenantId) {         Connection connection = ConnectionProvider.getConnection(tenantId);          Configuration configuration = Persistence.getInstance().getConfiguration();         String originalSchema = configuration.getProperty(Environment.DEFAULT_SCHEMA);          Statement stmt = null;         try {             Dialect dialect = Dialect.getDialect(configuration.getProperties());             Formatter formatter = FormatStyle.DDL.getFormatter();              configuration.setProperty(Environment.DEFAULT_SCHEMA, tenantId.getTenantId());              DatabaseMetadata meta = new DatabaseMetadata(connection, dialect, configuration);             stmt = connection.createStatement();              List&lt;SchemaUpdateScript&gt; scripts = configuration.generateSchemaUpdateScriptList(dialect, meta);             for (SchemaUpdateScript script : scripts) {                 String formatted = formatter.format(script.getScript());                 stmt.executeUpdate(formatted);             }         } catch (SQLException e) {             throw new RuntimeException(e);         } finally {             if (originalSchema != null) {                 configuration.setProperty(Environment.DEFAULT_SCHEMA, originalSchema);             } else {                 configuration.getProperties().remove(Environment.DEFAULT_SCHEMA);             }              try {                 if (stmt != null) {                     stmt.close();                 }                 connection.close();             } catch (SQLException e) {                 e.printStackTrace();             }         }     } } <\/code><\/pre>\n<p>  \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u0440\u0430\u0431\u043e\u0442\u044b:  <\/p>\n<ul>\n<li> \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u0441\u0445\u0435\u043c\u0443 \u0432 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 Hibernate, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f<br \/> \n<pre><code class=\"java\">configuration.setProperty(Environment.DEFAULT_SCHEMA, tenantId.getTenantId()); <\/code><\/pre>\n<\/li>\n<li>\u0421\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043a\u0440\u0438\u043f\u0442\u044b \u0434\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0445\u0435\u043c\u044b, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f<br \/> \n<pre><code class=\"java\">List&lt;SchemaUpdateScript&gt; scripts = configuration.generateSchemaUpdateScriptList(dialect, meta); <\/code><\/pre>\n<\/li>\n<li>\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0441\u043a\u0440\u0438\u043f\u0442\u044b<br \/> \n<pre><code class=\"java\">for (SchemaUpdateScript script : scripts) {     String formatted = formatter.format(script.getScript());     stmt.executeUpdate(formatted); } <\/code><\/pre>\n<\/li>\n<li>\u0412\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0435 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0441\u0445\u0435\u043c\u044b \u0432 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438<br \/> \n<pre><code class=\"java\">if (originalSchema != null) {     configuration.setProperty(Environment.DEFAULT_SCHEMA, originalSchema); } else {     configuration.getProperties().remove(Environment.DEFAULT_SCHEMA); } <\/code><\/pre>\n<\/li>\n<\/ul>\n<p>  \u041f\u0440\u0438 \u0441\u0442\u0430\u0440\u0442\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 Hibernate, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c <\/p>\n<pre><code class=\"java\">MultiTenantHibernateSchemaUpdate.execute(tenantId); <\/code><\/pre>\n<p>  \u0438 \u0441\u0445\u0435\u043c\u0430 \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0430.<\/p>\n<p>  \u0421\u043f\u0430\u0441\u0438\u0431\u043e. \t\t\t<\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/252353\/\"> http:\/\/habrahabr.ru\/post\/252353\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p> \t\t\t\u0425\u043e\u0442\u0438\u0442\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430\u043c\u0438 (\u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0430\u043c\u0438) \u0430\u0432\u0442\u043e-\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0445\u0435\u043c\u044b \u0411\u0414 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 Hibernate, \u043d\u043e \u0443 \u0432\u0430\u0441 \u0438\u043c\u0435\u0435\u0442\u0441\u044f multi-tenant \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430? \u0414\u043e\u0431\u0440\u043e \u043f\u043e\u0436\u0430\u043b\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434 \u043a\u0430\u0442.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-252637","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/252637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=252637"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/252637\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=252637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=252637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=252637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}