[英]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. 我正在尝试使用Eclipselink 2.5.2和MySQL制作一个简单的多租户示例来运行。
When trying to persist an entity asigned to a tenant id, mysql server throws an error: "Table 'jpatest.tenant1_userdata' doesn't exist". 当尝试保留分配给租户ID的实体时,MySQL服务器抛出错误:“表'jpatest.tenant1_userdata'不存在”。 (userdata being the entity, jpatest the database name, and tenant1 the tenant-id)
(userdata是实体,jpatest是数据库名称,tenant1是tenant-id)
The table indeed doesn't exist, the database jpatest do exist. 该表确实不存在,数据库jpatest确实存在。 I was expecting eclipselink to autogenerate the tables each time i try to persist with a new tenant id.
我期望eclipselink每次尝试使用新的租户ID持久化时自动生成表。
So the question would be: How can i force Eclipselink to create the tables? 因此,问题将是:如何强制Eclipselink创建表? 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 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. Eclipselink在多租户方案中将不支持DDL生成。
Refer to this link for more information, https://wiki.eclipse.org/EclipseLink/DesignDocs/Multi-Tenancy/TablePerTenant 请参阅此链接以获取更多信息, https://wiki.eclipse.org/EclipseLink/DesignDocs/Multi-Tenancy/TablePerTenant
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.