JPA Eclipselink Multitenant,表不存在错误

时间:2015-10-09 15:16:09

标签: java mysql jpa eclipselink multi-tenant

我正在尝试使用Eclipselink 2.5.2和MySQL来创建一个简单的多租户示例。

当尝试将实体持久化为租户ID时,mysql服务器会抛出错误:“表'jpatest.tenant1_userdata'不存在”。 (userdata是实体,jpatest数据库名称,tenant1是tenant-id)

表确实不存在,数据库jpatest确实存在。每次我尝试使用新的租户ID时,我都希望eclipselink自动生成表格。

所以问题是: 我如何强制Eclipselink创建表格? 如果那是不可能的;如何在运行时创建表?

以下是代码:

实体:

@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>

主要课程:

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();

    }

}

希望有人可以给我一个关于我做错的提示。

1 个答案:

答案 0 :(得分:0)

Eclipselink在多租户情景中不支持DDL生成。

有关详细信息,请参阅此链接https://wiki.eclipse.org/EclipseLink/DesignDocs/Multi-Tenancy/TablePerTenant