2014-02-24 69 views
1

當使用相同的@Embeddable類(CClass)的多個類(AClass,BClass低於)時,該類又由DClass的元素集合或任何泛型類(如String)組成。JPA中的嵌入式集合可嵌入

我們如何在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

您得到了什麼錯誤? – FazoM

+0

更新錯誤 – Monk

+0

上面請看看這裏:http://stackoverflow.com/questions/18215116/how-do-i-make-an-abstract-jpa-entity-that-has-an-ordered-column-in -a-shared-obje – FazoM

回答

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; 
} 

嵌入式註解用於進行註釋指定一個持久字段或 屬性一個實體或可嵌入類,其值爲可嵌入類的實例

+0

不,這不起作用 – Monk

+0

你得到什麼錯誤? – Koitoer

+0

相同的錯誤,沒有更改 – Monk