gpt4 book ai didi

java - 如何将 JPA 实体与持久性单元关联

转载 作者:行者123 更新时间:2023-11-30 07:41:14 25 4
gpt4 key购买 nike

我有两个 persistenceUnits 映射到两个不同的 Oracle 模式,这些模式相似但表名不同。当我尝试部署我的应用程序时,我收到一个 Hibernate 异常“Missing Table”,看起来 Hibernate 正在错误的 persistenceUnit 中寻找表。

.ear#POIPersistenceUnit\": org.hibernate.HibernateException: Missing table: POI_SERVICE Caused by: org.hibernate.HibernateException: Missing table: POI_SERVICE"}}

表“POI_SERVICE”属于“POIExtractorPersistenceUnit”persistenceUnit,而不是“POIPersistenceUnit”persistenceUnit

我尝试在 persistence.xml 文件中列出具有相应 persistenceUnits 的实体,如下所示,但没有成功,仍然遇到相同的错误。

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

<persistence-unit name="POIExtractorPersistenceUnit">

<jta-data-source>java:/XAPOIExtractorDS</jta-data-source>

<class>za.co.fnb.cii.poiextractor.persistence.model.IdentificationRequest</class>
<class>za.co.fnb.cii.poiextractor.persistence.model.IdentificationResponse</class>
<class>za.co.fnb.cii.poiextractor.persistence.model.POIService</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>

<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.flushMode" value="FLUSH_AUTO" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="true" />

</properties>

</persistence-unit>

<persistence-unit name="POIPersistenceUnit">

<jta-data-source>java:/XACompendiumDS</jta-data-source>

<class>za.co.fnb.cii.poiextractor.persistence.entities.IdentificationRequestIncoming</class>
<class>za.co.fnb.cii.poiextractor.persistence.entities.IdentificationResponseIncoming</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>

<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.flushMode" value="FLUSH_AUTO" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="false" />

</properties>

</persistence-unit>

我还尝试按如下方式注释实体 @PersistenceUnit(unitName = "POIExtractorPersistenceUnit") 但仍然没有成功,错误仍然存​​在。

/**
* Created by on 21-Dec-15.
*/
@Entity
@Table(name = "POI_SERVICE")
@PersistenceUnit(unitName = "POIExtractorPersistenceUnit")
@NamedQueries({
@NamedQuery(name="findServiceRecordByApplicationName",
query="SELECT pois FROM POIService pois WHERE pois.application = :applicationName")
})
public class POIService implements IEntity{

@Id
@GeneratedValue(generator="service_config_seq")
@SequenceGenerator(name="service_config_seq", sequenceName="service_config_seq", allocationSize=1)
private long id;

private String application;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "last_update",nullable = false)
private Date lastUpdate;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "last_date",nullable = true)
private Date lastDate;

@Column(name = "last_record",nullable = true)
private long lastRecord;

@Override
public long getId() {
return id;
}

public String getApplication() {
return application;
}

public void setApplication(String application) {
this.application = application;
}

public Date getLastUpdate() {
return lastUpdate;
}

public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}

public Date getLastDate() {
return lastDate;
}

public void setLastDate(Date lastDate) {
this.lastDate = lastDate;
}

public long getLastRecord() {
return lastRecord;
}

public void setLastRecord(long lastRecord) {
this.lastRecord = lastRecord;
}
}

更新:

堆栈跟踪

2016-01-08 12:24:52,587 INFO [java.sql.DatabaseMetaData] (ServerService Thread Pool -- 58) HHH000262: Table not found: POI_SERVICE 2016-01-08 12:24:52,590 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 58) MSC000001: Failed to start service jboss.persistenceunit."poiextractor-ear-1.0.0-SNAPSHOT.ear#POIPersistenceUnit": org.jboss.msc.service.StartException in service jboss.persistenceunit."poiextractor-ear-1.0.0-SNAPSHOT.ear#POIPersistenceUnit": org.hibernate.HibernateException: Missing table: POI_SERVICE at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:172) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final] at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:117) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final] at java.security.AccessController.doPrivileged(Native Method) [rt.jar:1.8.0_60] at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:474) [wildfly-security-manager-1.0.0.Final.jar:1.0.0.Final] at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:182) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_60] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_60] at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_60] at org.jboss.threads.JBossThread.run(JBossThread.java:122) [jboss-threads-2.1.1.Final.jar:2.1.1.Final] Caused by: org.hibernate.HibernateException: Missing table: POI_SERVICE at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1335) [hibernate-core-4.3.7.Final.jar:4.3.7.Final] at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:155) [hibernate-core-4.3.7.Final.jar:4.3.7.Final] at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:525) [hibernate-core-4.3.7.Final.jar:4.3.7.Final] at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1859) [hibernate-core-4.3.7.Final.jar:4.3.7.Final] at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:852) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final] at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:845) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final] at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398) [hibernate-core-4.3.7.Final.jar:4.3.7.Final] at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final] at org.jboss.as.jpa.hibernate4.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44) [jipijapa-hibernate4-3-1.0.1.Final.jar:] at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:154) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final] ... 8 more

2016-01-08 12:24:58,786 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (ServerService Thread Pool -- 57) HHH000261: Table found: POIOPS.POI_SERVICE 2016-01-08 12:24:58,788 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (ServerService Thread Pool -- 57) HHH000037: Columns: [last_record, application, last_update, id, last_date] 2016-01-08 12:25:00,774 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (ServerService Thread Pool -- 57) HHH000261: Table found: POIOPS.IDENTIFICATION_REQUEST 2016-01-08 12:25:00,775 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (ServerService Thread Pool -- 57) HHH000037: Columns: [sworkstationid, yafp2wsqimage, stransactiontime, created, stype, bretphoto, scardseqno, stranid, breturnname, smode, requesttype, ssiteid, identificationresponse_id, sidn, id, yafp1wsqimage, updated, ifpind1, ifpind2] 2016-01-08 12:25:05,385 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (ServerService Thread Pool -- 57) HHH000261: Table found: POIOPS.IDENTIFICATION_RESPONSE 2016-01-08 12:25:05,387 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (ServerService Thread Pool -- 57) HHH000037: Columns: [bnameret, created, sfpind2, scallcentreno, ifp2score, stranid, sidissuedate, ierror, ifp1score, sfp1result, sidn, sname, serror, sfp2result, sfpind1, yaphoto, ssmartcardind, id, ssurname, updated] 2016-01-08 12:25:09,367 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (ServerService Thread Pool -- 57) HHH000261: Table found: POIOPS.IDENTIFICATIONREQUEST 2016-01-08 12:25:09,369 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (ServerService Thread Pool -- 57) HHH000037: Columns: [sworkstationid, responseerror, yafp2wsqimage, stransactiontime, created, stype, bretphoto, scardseqno, stranid, breturnname, smode, requesttype, ssiteid, sidn, id, yafp1wsqimage, updated, ifpind1, ifpind2] 2016-01-08 12:25:15,851 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (ServerService Thread Pool -- 57) HHH000261: Table found: POIOPS.IDENTIFICATIONRESPONSE 2016-01-08 12:25:15,853 INFO [org.hibernate.tool.hbm2ddl.TableMetadata] (ServerService Thread Pool -- 57) HHH000037: Columns: [bnameret, created, sfpind2, scallcentreno, ifp2score, stranid, sidissuedate, ierror, ifp1score, sfp1result, sidn, sname, serror, sfp2result, sfpind1, yaphoto, ssmartcardind, id, ssurname, updated, request_id] 2016-01-08 12:25:15,994 ERROR [org.jboss.as.controller.management-operation] (management-handler-thread - 2) JBAS014613: Operation ("deploy") failed - address: ([("deployment" => "poiextractor-ear-1.0.0-SNAPSHOT.ear")]) - failure description: {"JBAS014671: Failed services" => {"jboss.persistenceunit.\"poiextractor-ear-1.0.0-SNAPSHOT.ear#POIPersistenceUnit\"" => "org.jboss.msc.service.StartException in service jboss.persistenceunit.\"poiextractor-ear-1.0.0-SNAPSHOT.ear#POIPersistenceUnit\": org.hibernate.HibernateException: Missing table: POI_SERVICE Caused by: org.hibernate.HibernateException: Missing table: POI_SERVICE"}} 2016-01-08 12:25:15,996 ERROR [org.jboss.as.server] (management-handler-thread - 2) JBAS015870: Deploy of deployment "poiextractor-ear-1.0.0-SNAPSHOT.ear" was rolled back with the following failure message: {"JBAS014671: Failed services" => {"jboss.persistenceunit.\"poiextractor-ear-1.0.0-SNAPSHOT.ear#POIPersistenceUnit\"" => "org.jboss.msc.service.StartException in service jboss.persistenceunit.\"poiextractor-ear-1.0.0-SNAPSHOT.ear#POIPersistenceUnit\": org.hibernate.HibernateException: Missing table: POI_SERVICE Caused by: org.hibernate.HibernateException: Missing table: POI_SERVICE"}} 2016-01-08 12:25:15,997 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-6) JBAS010418: Stopped Driver service with driver-name = poiextractor-ear-1.0.0-SNAPSHOT.ear_org.h2.Driver_1_4 2016-01-08 12:25:15,998 INFO [org.jboss.as.jpa] (ServerService Thread Pool -- 57) JBAS011410: Stopping Persistence Unit (phase 2 of 2) Service 'poiextractor-ear-1.0.0-SNAPSHOT.ear#POIExtractorPersistenceUnit' 2016-01-08 12:25:16,018 INFO [org.jboss.weld.deployer] (MSC service thread 1-9) JBAS016009: Stopping weld service for deployment poiextractor-ear-1.0.0-SNAPSHOT.ear 2016-01-08 12:25:16,043 INFO [org.jboss.as.jpa] (ServerService Thread Pool -- 57) JBAS011410: Stopping Persistence Unit (phase 1 of 2) Service 'poiextractor-ear-1.0.0-SNAPSHOT.ear#POIPersistenceUnit' 2016-01-08 12:25:16,043 INFO [org.jboss.as.jpa] (ServerService Thread Pool -- 59) JBAS011410: Stopping Persistence Unit (phase 1 of 2) Service 'poiextractor-ear-1.0.0-SNAPSHOT.ear#POIExtractorPersistenceUnit'

更新2:当我从 persistence.xml 中删除此属性时,一切正常。

<property name="hibernate.hbm2ddl.auto" value="validate" />

最佳答案

一个 JPA EntityManager 属于一个持久性单元,或 EntityManagerFactory,您不能将多个持久性单元与同一个 EntityManager 混合使用。

但你不必这样做。

您可以拥有单个持久性单元,并在映射实体时使用schema 属性。

如果您使用多个持久性单元,则需要多个 persistence.xml、多个 EntityManagerFactory,如果需要全局事务,可能还需要 JTA。

关于java - 如何将 JPA 实体与持久性单元关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34674618/

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