gpt4 book ai didi

java - jpa persist() 函数在 TABLE_PER_TENANT (multi_schema) Multi-Tenancy 策略中给出错误

转载 作者:行者123 更新时间:2023-11-29 19:18:23 24 4
gpt4 key购买 nike

我正在使用 java、jpa(eclipselink)、mysql 开发一个采用“共享数据库/单独模式”方法的 Multi-Tenancy Web 应用程序。我的持久性文件如下所示:

    <persistence-unit name="GroupBuilderPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.cache.shared.default" value="false"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/?"/>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
<--- Here goes other properties definition -->
</persistence-unit>

现在这是我的 EntityMangerFactory 和 EntityManager:

emfForTenant = Persistence.createEntityManagerFactory("GroupBuilderPU");
EntityManager em = emfForTenant.createEntityManager();
em.setProperty("eclipselink.tenant-id", schemaNameAsTenantId);

我有一个实体 MaterialUnit:

@Entity
@Multitenant(MultitenantType.TABLE_PER_TENANT)
@TenantTableDiscriminator(type = TenantTableDiscriminatorType.SCHEMA, contextProperty = PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT)
public class MaterialUnit implements Serializable {
//private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@NotNull
@Size(min = 1, max = 128)
@Column(unique=true)
private String unitName;

现在操作出现错误:

MaterialUnit mu = new MaterialUnit();
mu.setUnitName("New Unit");
em.persist(mu);

错误是:

Internal Exception: java.sql.SQLException: No database selected
Error Code: 1046
Call: UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?
bind => [50, SEQ_GEN]
Query: ValueReadQuery(name="SEQUENCE" sql="SELECT SEQ_COUNT FROM SEQUENCE WHERE SEQ_NAME = ?")
Severe: Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: No database selected
Error Code: 1046
Call: UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?
bind => [50, SEQ_GEN]
Query: ValueReadQuery(name="SEQUENCE" sql="SELECT SEQ_COUNT FROM SEQUENCE WHERE SEQ_NAME = ?")
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:331)

那么这个持久化操作将如何进行呢?任何帮助或建议将不胜感激:)

最佳答案

错误表示没有选择数据库..

我认为你的问题出在这一行:

<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/?"

当它执行时,它连接到mysql,但不知道你需要什么模式...如果你想要有多个模式(持久单元),我建议根据需要创建尽可能多的持久单元,如下所示:

<persistence-unit name="PersistenceUnitONE" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.cache.shared.default" value="false"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/schema1"/>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
</properties>
</persistence-unit>

<persistence-unit name="PersistenceUnitTWO" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.cache.shared.default" value="false"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/schema2"/>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
</properties>
</persistence-unit>

然后通过更改此行来调用您需要的单元:

emfForTenant = Persistence.createEntityManagerFactory("PersistenceUnitONE");
emfForTenant = Persistence.createEntityManagerFactory("PersistenceUnitTWO");

关于java - jpa persist() 函数在 TABLE_PER_TENANT (multi_schema) Multi-Tenancy 策略中给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42579651/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com