// Path: spring-best-practice-hibernate-search-multi-tenancy/src/main/java/practice/entity/Item.java
// @Entity
// @Indexed
// public class Item implements Serializable {
// 
// 	@Id
// 	@GeneratedValue(strategy = GenerationType.AUTO)
// 	private long id;
// 
// 	private String name;
// 
// 	public long getId() {
// 		return id;
// 	}
// 
// 	public void setId(long id) {
// 		this.id = id;
// 	}
// 
// 	public String getName() {
// 		return name;
// 	}
// 
// 	public void setName(String name) {
// 		this.name = name;
// 	}
// 
// 	@Override
// 	public String toString() {
// 		return name;
// 	}
// }


import org.hibernate.MultiTenancyStrategy;
import org.hibernate.cfg.Environment;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import practice.entity.Item;
import javax.sql.DataSource;
import java.util.LinkedHashMap;
import java.util.Map;
package practice.configure;



@Configuration
@EnableConfigurationProperties(JpaProperties.class)
public class MultiTenancyJpaConfiguration {

	@Autowired
	private DataSource dataSource;

	@Autowired
	private JpaProperties jpaProperties;

	@Autowired
	private MultiTenantConnectionProvider multiTenantConnectionProvider;

	@Autowired
	private CurrentTenantIdentifierResolver currentTenantIdentifierResolver;

	@Bean
	public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder factoryBuilder) {
		Map<String, Object> vendorProperties = new LinkedHashMap<>();
		vendorProperties.putAll(jpaProperties.getHibernateProperties(dataSource));

		vendorProperties.put(Environment.MULTI_TENANT, MultiTenancyStrategy.DATABASE);
		vendorProperties.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProvider);
		vendorProperties.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifierResolver);

		return factoryBuilder.dataSource(dataSource)
// Path: spring-best-practice-hibernate-search-multi-tenancy/src/main/java/practice/entity/Item.java
// @Entity
// @Indexed
// public class Item implements Serializable {
// 
// 	@Id
// 	@GeneratedValue(strategy = GenerationType.AUTO)
// 	private long id;
// 
// 	private String name;
// 
// 	public long getId() {
// 		return id;
// 	}
// 
// 	public void setId(long id) {
// 		this.id = id;
// 	}
// 
// 	public String getName() {
// 		return name;
// 	}
// 
// 	public void setName(String name) {
// 		this.name = name;
// 	}
// 
// 	@Override
// 	public String toString() {
// 		return name;
// 	}
// }


// Path: spring-best-practice-hibernate-search-multi-tenancy/src/main/java/practice/configure/MultiTenancyJpaConfiguration.java
import org.hibernate.MultiTenancyStrategy;
import org.hibernate.cfg.Environment;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import practice.entity.Item;
import javax.sql.DataSource;
import java.util.LinkedHashMap;
import java.util.Map;

package practice.configure;



@Configuration
@EnableConfigurationProperties(JpaProperties.class)
public class MultiTenancyJpaConfiguration {

	@Autowired
	private DataSource dataSource;

	@Autowired
	private JpaProperties jpaProperties;

	@Autowired
	private MultiTenantConnectionProvider multiTenantConnectionProvider;

	@Autowired
	private CurrentTenantIdentifierResolver currentTenantIdentifierResolver;

	@Bean
	public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder factoryBuilder) {
		Map<String, Object> vendorProperties = new LinkedHashMap<>();
		vendorProperties.putAll(jpaProperties.getHibernateProperties(dataSource));

		vendorProperties.put(Environment.MULTI_TENANT, MultiTenancyStrategy.DATABASE);
		vendorProperties.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProvider);
		vendorProperties.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifierResolver);

		return factoryBuilder.dataSource(dataSource)
				.packages(Item.class.getPackage().getName())