简体   繁体   中英

JPA Eclipselink Multitenant, table doesn't exist error

i'm trying to make a simple multitenant example to run, using Eclipselink 2.5.2, and MySQL.

When trying to persist an entity asigned to a tenant id, mysql server throws an error: "Table 'jpatest.tenant1_userdata' doesn't exist". (userdata being the entity, jpatest the database name, and tenant1 the tenant-id)

The table indeed doesn't exist, the database jpatest do exist. I was expecting eclipselink to autogenerate the tables each time i try to persist with a new tenant id.

So the question would be: How can i force Eclipselink to create the tables? If that is not possible; How can i create tables at runtime?

Here's the code:

Entity:

@Entity
@Table(name = "userdata")
@Multitenant(value = MultitenantType.TABLE_PER_TENANT)
@TenantTableDiscriminator(type = TenantTableDiscriminatorType.PREFIX, contextProperty = "tenant-id")
public class UserData implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private static final long serialVersionUID = 1L;
    private String name;

.
.
.

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="MultiTeanantTest" transaction-type="RESOURCE_LOCAL">
        <class>UserData</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpatest" />

            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
            <property name="javax.persistence.schema-generation.create-database-schemas" value="true"/>
            <property name="eclipselink.ddl-generation" value="create-tables"/>
        </properties>
    </persistence-unit>
</persistence>

Main class:

public class Main {

    public static void main(String[] args) {

        UserData ud = new UserData();
        ud.setNombre("John);
        Map properties = new HashMap<>();
        properties.put("tenant-id", "tenant1");

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("MultiTeanantTest", properties );
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();
        em.persist(ud);
        em.getTransaction().commit();

    }

}

Hope someone can give me a tip in what i'm doing wrong.

DDL generation will not be supported in a Multitenant Scenario by Eclipselink.

Refer to this link for more information, https://wiki.eclipse.org/EclipseLink/DesignDocs/Multi-Tenancy/TablePerTenant

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM