Hi guys!
I'm developing a multitenancy system and I'm using Single-Table Multi-Tenancy.
So when I persist a new multitenancy object the column that reference the tenant column is saved in blank and I don't know why.
I did a test but it worked well...
HashMap<String, Integer> properties = new HashMap<String, Integer>();
properties.put("tenant.empresa", 500);
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME,properties);
EntityManager em = factory.createEntityManager();
// create new
em.getTransaction().begin();
Produto p = new Produto();
p.setDescricao("test");
em.persist(p);
em.getTransaction().commit();
em.close();
when I execute a test class the object is saved correctly with the tenant_id 500... but when the applications is running this not occur... and I'm using a similar code to do this....
I'm using JSF, Spring Framework, and PostgreSQL the tenant identifier is in session and when I need I build a EntityManager like above with tenant identifier
persistence.xml
<persistence-unit name="TCC" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>br.com.mario.tcc.model.Funcionario</class>
<class>br.com.mario.tcc.model.Usuario</class>
<class>br.com.mario.tcc.model.Empresa</class>
<class>br.com.mario.tcc.model.Produto</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/multitenant"/>
<property name="javax.persistence.jdbc.user" value="postgres"/>
<property name="javax.persistence.jdbc.password" value="postgres"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<!-- configurações do eclipselink -->
<property name="eclipselink.target-database" value="PostgreSQL"/>
<property name="eclipselink.jdbc.uppercase-columns" value="true" />
<property name="eclipselink.jdbc.allow-native-sql-queries" value="true"/>
</properties>
</persistence-unit>
Java Classes
@Entity
@Multitenant
@TenantDiscriminatorColumn(name="empresa_id", contextProperty="tenant.empresa", discriminatorType=DiscriminatorType.INTEGER)
public class Produto implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2739839954798051504L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String descricao;
@ManyToOne
@JoinColumn(name="empresa_id", insertable=false, updatable=false)
private Empresa empresa;//tenant identifier
//getters and setters .....
so... what I do? what is it? why this is happening?
follows the image of the database after the operations