当有多个类(AClass,下面的BClass)使用相同的@Embeddable类(CClass)时,它又由DClass的元素集合或任何通用类(如String)组成。
我们如何在JPA中支持这样的结构?我跑的时候收到错误。 有一个简单的方法,在我的系统中有许多类似于AClass和Bclass的类。
@Entity
public class AClass {
@Id
String aid;
CClass a;
String member1;
}
@Entity
public class BClass {
@Id
String bid;
CClass a;
int member2;
}
@Embeddable
public class CClass {
@ElementCollection
List<DClass> member5;
}
@Embeddable
public class DClass {
int member4;
int member3;
}
我得到以下错误,只创建了2个表
> Exception [EclipseLink-4002] (Eclipse Persistence Services -
> 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException Internal
> Exception: java.sql.SQLSyntaxErrorException: Column name 'MEMBER3'
> appears more than once in the CREATE TABLE statement. Error Code: -1
> Call: CREATE TABLE AClass_MEMBER5 (MEMBER3 INTEGER, MEMBER4 INTEGER,
> AClass_AID VARCHAR(255), MEMBER3 INTEGER, MEMBER4 INTEGER, AClass_AID
> VARCHAR(255)) Query: DataModifyQuery(sql="CREATE TABLE AClass_MEMBER5
> (MEMBER3 INTEGER, MEMBER4 INTEGER, AClass_AID VARCHAR(255), MEMBER3
> INTEGER, MEMBER4 INTEGER, AClass_AID VARCHAR(255))") at
> org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:340)
> at
> org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:679)
> at
> org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:558)
> at
> org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:1995)
> at
> org.eclipse.persistence.sessions.server.ServerSession.executeCall(ServerSession.java:570)
> at
> org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:242)
> at
> org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:228)
> at
> org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeNoSelectCall(DatasourceCallQueryMechanism.java:271)
> at
> org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeNoSelect(DatasourceCallQueryMechanism.java:251)
> at
> org.eclipse.persistence.queries.DataModifyQuery.executeDatabaseQuery(DataModifyQuery.java:85)
> at
> org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:899)
> at
> org.eclipse.persistence.internal.sessions.AbstractSession.internalExecuteQuery(AbstractSession.java:3207)
> at
> org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1797)
> at
> org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1779)
> at
> org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1730)
> at
> org.eclipse.persistence.internal.sessions.AbstractSession.priviledgedExecuteNonSelectingCall(AbstractSession.java:5000)
> at
> org.eclipse.persistence.tools.schemaframework.DatabaseObjectDefinition.createOnDatabase(DatabaseObjectDefinition.java:141)
> at
> org.eclipse.persistence.tools.schemaframework.SchemaManager.createObject(SchemaManager.java:223)
> at
> org.eclipse.persistence.tools.schemaframework.TableCreator.createTables(TableCreator.java:165)
> at
> org.eclipse.persistence.tools.schemaframework.TableCreator.createTables(TableCreator.java:142)
> at
> org.eclipse.persistence.tools.schemaframework.TableCreator.createTables(TableCreator.java:134)
> at
> org.eclipse.persistence.tools.schemaframework.SchemaManager.createDefaultTables(SchemaManager.java:1032)
> at
> org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.generateDefaultTables(EntityManagerFactoryProvider.java:102)
> at
> org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.writeDDLToDatabase(EntityManagerSetupImpl.java:3937)
> at
> org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.writeMetadataDDLToDatabase(EntityManagerSetupImpl.java:3992)
> at
> org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.writeDDL(EntityManagerSetupImpl.java:3903)
> at
> org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.writeDDL(EntityManagerSetupImpl.java:3776)
> at
> org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:724)
> at
> org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:204)
> at
> org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:182)
> at
> org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getDatabaseSession(EntityManagerFactoryImpl.java:527)
> at
> org.eclipse.persistence.jpa.PersistenceProvider.createContainerEntityManagerFactory(PersistenceProvider.java:358)
> at
> org.glassfish.persistence.jpa.PersistenceUnitLoader.loadPU(PersistenceUnitLoader.java:199)
> at
> org.glassfish.persistence.jpa.PersistenceUnitLoader.<init>(PersistenceUnitLoader.java:107)
> at
> org.glassfish.persistence.jpa.JPADeployer$1.visitPUD(JPADeployer.java:223)
> at
> org.glassfish.persistence.jpa.JPADeployer$PersistenceUnitDescriptorIterator.iteratePUDs(JPADeployer.java:510)
> at
> org.glassfish.persistence.jpa.JPADeployer.createEMFs(JPADeployer.java:230)
> at
> org.glassfish.persistence.jpa.JPADeployer.prepare(JPADeployer.java:168)
> at
> com.sun.enterprise.v3.server.ApplicationLifecycle.prepareModule(ApplicationLifecycle.java:922)
> at
> com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:431)
> at
> com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
> at
> org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
> at
> com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:537)
> at
> com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:546)
> at
> com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1423)
> at
> com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1500(CommandRunnerImpl.java:108)
> at
> com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1762)
> at
> com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1674)
> at
> org.glassfish.deployment.admin.ReDeployCommand.execute(ReDeployCommand.java:131)
> at
> com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:527)
> at
> com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:523)
> at java.security.AccessController.doPrivileged(Native Method) at
> javax.security.auth.Subject.doAs(Subject.java:356) at
> com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:522)
> at
> com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:546)
> at
> com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1423)
> at
> com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1500(CommandRunnerImpl.java:108)
> at
> com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1762)
> at
> com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1674)
> at
> com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
> at
> com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
> at
> org.glassfish.grizzly.http.server.StaticHttpHandler.service(StaticHttpHandler.java:297)
> at
> com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:246)
> at
> org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
> at
> org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
> at
> org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
> at
> org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
> at
> org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
> at
> org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
> at
> org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
> at
> org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
> at
> org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
> at
> org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
> at
> org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
> at
> org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
> at
> org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
> at
> org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
> at
> org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
> at
> org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
> at java.lang.Thread.run(Thread.java:724) Caused by:
> java.sql.SQLSyntaxErrorException: Column name 'MEMBER3' appears more
> than once in the CREATE TABLE statement. at
> org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown
> Source) at
> org.apache.derby.client.am.SqlException.getSQLException(Unknown
> Source) at
> org.apache.derby.client.am.Connection.prepareStatement(Unknown Source)
> at
> com.sun.gjc.spi.base.ConnectionHolder.prepareStatement(ConnectionHolder.java:586)
> at
> com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareCachedStatement(ConnectionWrapper40.java:255)
> at
> com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareCachedStatement(ConnectionWrapper40.java:52)
> at
> com.sun.gjc.spi.ManagedConnectionImpl.prepareCachedStatement(ManagedConnectionImpl.java:992)
> at
> com.sun.gjc.spi.jdbc40.ConnectionWrapper40.prepareStatement(ConnectionWrapper40.java:173)
> at
> org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.prepareStatement(DatabaseAccessor.java:1551)
> at
> org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.prepareStatement(DatabaseAccessor.java:1500)
> at
> org.eclipse.persistence.internal.databaseaccess.DatabaseCall.prepareStatement(DatabaseCall.java:778)
> at
> org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:619)
> ... 78 more Caused by: org.apache.derby.client.am.SqlException:
> Column name 'MEMBER3' appears more than once in the CREATE TABLE
> statement. at
> org.apache.derby.client.am.Statement.completeSqlca(Unknown Source) at
> org.apache.derby.client.net.NetStatementReply.parsePrepareError(Unknown
> Source) at
> org.apache.derby.client.net.NetStatementReply.parsePRPSQLSTTreply(Unknown
> Source) at
> org.apache.derby.client.net.NetStatementReply.readPrepareDescribeOutput(Unknown
> Source) at
> org.apache.derby.client.net.StatementReply.readPrepareDescribeOutput(Unknown
> Source) at
> org.apache.derby.client.net.NetStatement.readPrepareDescribeOutput_(Unknown
> Source) at
> org.apache.derby.client.am.Statement.readPrepareDescribeOutput(Unknown
> Source) at
> org.apache.derby.client.am.PreparedStatement.readPrepareDescribeInputOutput(Unknown
> Source) at
> org.apache.derby.client.am.PreparedStatement.flowPrepareDescribeInputOutput(Unknown
> Source) at
> org.apache.derby.client.am.PreparedStatement.prepare(Unknown Source)
> at org.apache.derby.client.am.Connection.prepareStatementX(Unknown
> Source) ... 88 more
答案 0 :(得分:0)
@Entity
public class AClass {
@Id
String aid;
CClass a;
String member1;
}
如果CClass应该用@Embedded注释
@Entity
public class AClass {
@Id
String aid;
@Embedded
CClass a;
String member1;
}
嵌入式注释用于指定持久字段或 实体或可嵌入类的属性,其值为实例 可嵌入类的