创建PostgreSQL数组类型,如" text []"使用JPA(EclipseLink)

时间:2014-05-11 23:54:05

标签: postgresql jpa eclipselink

我无法创建EclipseLink将创建的JPA模型类 关注PostgreSQL DDL:

CREATE TABLE array_example (
  id serial not null, 
  params text[] not null
);

据我所知,PostgreSQL的数组类型不是SQL标准,因此不受JPA标准的约束。 EclipseLink似乎对非标准扩展提供某种支持。到目前为止,我提出了以下课程:

....
import org.eclipse.persistence.annotations.Array;
import org.eclipse.persistence.annotations.Struct;

@Entity
@Table(name = "array_example")
@Struct(name = "params") // Else: [EclipseLink-157] "Normal descriptors do not support non-relational extensions."
@XmlRootElement
public class ArrayExamplePostgres implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private int id;

    @NotNull
    @Array(databaseType = "TEXT[]") // Needs @Struct on class!
    @Column(name = "params")
    private List<String> params;
    ....

运行我的测试脚本时出现以下错误:

Mai 12, 2014 1:35:13 AM org.eclipse.persistence.default
WARNING: The default table generator currently only supports generating default table schema from a relational project.
Mai 12, 2014 1:35:13 AM org.eclipse.persistence.session.file:/srv/home/james/workspace/java_test/java_test_ee6_jpa/target/classes/_postgresJavaTestJpaPU
WARNING: 
Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: Unable to find server array type for provided name TEXT[].
Error Code: 0
Query: InsertObjectQuery(de.lathspell.java_test_ee6_jpa.model.ArrayExamplePostgres@3c77af84)
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:331)
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:326)
    at org.eclipse.persistence.mappings.structures.ObjectRelationalDataTypeDescriptor.buildFieldValueFromDirectValues(ObjectRelationalDataTypeDescriptor.java:102)
    at org.eclipse.persistence.mappings.foundation.AbstractCompositeDirectCollectionMapping.writeFromObjectIntoRow(AbstractCompositeDirectCollectionMapping.java:617)
    at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildRow(ObjectBuilder.java:1394)
    at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildRow(ObjectBuilder.java:1382)
    at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.insertObjectForWrite(DatabaseQueryMechanism.java:450)
    at org.eclipse.persistence.queries.InsertObjectQuery.executeCommit(InsertObjectQuery.java:80)
    at org.eclipse.persistence.queries.InsertObjectQuery.executeCommitWithChangeSet(InsertObjectQuery.java:90)
    at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.executeWriteWithChangeSet(DatabaseQueryMechanism.java:300)
    at org.eclipse.persistence.queries.WriteObjectQuery.executeDatabaseQuery(WriteObjectQuery.java:58)
    at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:899)
    at org.eclipse.persistence.queries.DatabaseQuery.executeInUnitOfWork(DatabaseQuery.java:798)
    at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWorkObjectLevelModifyQuery(ObjectLevelModifyQuery.java:108)
    at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWork(ObjectLevelModifyQuery.java:85)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2894)
    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.CommitManager.commitNewObjectsForClassWithChangeSet(CommitManager.java:226)
    at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsWithChangeSet(CommitManager.java:125)
    at org.eclipse.persistence.internal.sessions.AbstractSession.writeAllObjectsWithChangeSet(AbstractSession.java:4200)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabase(UnitOfWorkImpl.java:1439)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithPreBuiltChangeSet(UnitOfWorkImpl.java:1585)
    at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.writeChanges(RepeatableWriteUnitOfWork.java:452)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.flush(EntityManagerImpl.java:846)
    at de.lathspell.java_test_ee6_jpa.model.ArrayExamplePostgresTest.testArrays(ArrayExamplePostgresTest.java:31)
...

PostgreSQL日志文件包含两个可疑的SELECT(在_text后返回1009,然后是typedelim = NULL),因为丢失了表,它在失败之前不久:

2014-05-12 01:34:32 CEST postgres@java_test_ee6_jpa [17555] LOG:  execute <unnamed>: SELECT oid FROM pg_catalog.pg_type WHERE typname = $1
2014-05-12 01:34:32 CEST postgres@java_test_ee6_jpa [17555] DETAIL:  parameters: $1 = '_text

2014-05-12 01:34:32 CEST postgres@java_test_ee6_jpa [17555] LOG:  execute <unnamed>: SELECT e.typdelim FROM pg_catalog.pg_type t, pg_catalog.pg_type e WHERE t.oid = $1 and t.typelem = e.oid
2014-05-12 01:34:32 CEST postgres@java_test_ee6_jpa [17555] DETAIL:  parameters: $1 = '1009'

2014-05-12 01:34:32 CEST postgres@java_test_ee6_jpa [17555] LOG:  execute S_1: BEGIN
2014-05-12 01:34:32 CEST postgres@java_test_ee6_jpa [17555] ERROR:  relation "array_example" does not exist at character 13
2014-05-12 01:34:32 CEST postgres@java_test_ee6_jpa [17555] STATEMENT:  INSERT INTO array_example (params) VALUES ($1)

持久性配置的相关部分是:

<persistence-unit name="postgresJavaTestJpaPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>de.lathspell.java_test_ee6_jpa.model.ArrayExamplePostgres</class>
    ...
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql:java_test_ee6_jpa"/>
        <property name="javax.persistence.jdbc.user" value="postgres"/>
        <property name="javax.persistence.jdbc.password" value="secret"/>
        <property name="eclipselink.ddl-generation.output-mode" value="both"/>
        <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
        <property name="eclipselink.drop-ddl-jdbc-file-name" value="src/main/sql/eclipselink-postgres-drop.sql"/>
        <property name="eclipselink.create-ddl-jdbc-file-name" value="src/main/sql/eclipselink-postgres-create.sql"/>
        <property name="eclipselink.jdbc.native-sql" value="true"/>
    </properties>
</persistence-unit>

(完整的班级位于https://svn.code.sf.net/p/lathspellsphp/code/java_test/java_test_ee7_jpa/src/main/java/de/lathspell/java_test_ee7_jpa/model/ArrayExamplePostgres.java

修改

使用@Array时(databaseType =&#34; TEXT&#34;)我得到一个SQL查询但没有生成架构:

2014-05-12 23:03:08 CEST postgres@java_test_ee7_jpa [4183] LOG:  execute <unnamed>: SELECT pg_type.oid   FROM pg_catalog.pg_type   LEFT   JOIN (select ns.oid as nspoid, ns.nspname, r.r           from pg_namespace as ns           join ( select s.r, (current_schemas(false))[s.r] as nspname                    from generate_series(1, array_upper(current_schemas(false), 1)) as s(r) ) as r          using ( nspname )        ) as sp     ON sp.nspoid = typnamespace  WHERE typname = $1  ORDER BY sp.r, pg_type.oid DESC LIMIT 1
2014-05-12 23:03:08 CEST postgres@java_test_ee7_jpa [4183] DETAIL:  parameters: $1 = '_TEXT'
-> 0 rows

使用@Array时(databaseType =&#34; text []&#34;我得到一个SQL查询但没有生成架构:

2014-05-12 23:12:21 CEST postgres@java_test_ee7_jpa [4435] LOG:  execute <unnamed>: SELECT pg_type.oid   FROM pg_catalog.pg_type   LEFT   JOIN (select ns.oid as nspoid, ns.nspname, r.r           from pg_namespace as ns           join ( select s.r, (current_schemas(false))[s.r] as nspname                    from generate_series(1, array_upper(current_schemas(false), 1)) as s(r) ) as r          using ( nspname )        ) as sp     ON sp.nspoid = typnamespace  WHERE typname = $1  ORDER BY sp.r, pg_type.oid DESC LIMIT 1
2014-05-12 23:12:21 CEST postgres@java_test_ee7_jpa [4435] DETAIL:  parameters: $1 = '_text[]'
-> 0 rows

使用@Array时(databaseType =&#34; text&#34;我得到两个实际产生结果的SQL查询,但仍然没有创建表,也不是EclipseLink创建的schema.sql:

2014-05-12 23:04:46 CEST postgres@java_test_ee7_jpa [4266] LOG:  execute <unnamed>: SELECT pg_type.oid   FROM pg_catalog.pg_type   LEFT   JOIN (select ns.oid as nspoid, ns.nspname, r.r           from pg_namespace as ns           join ( select s.r, (current_schemas(false))[s.r] as nspname                    from generate_series(1, array_upper(current_schemas(false), 1)) as s(r) ) as r          using ( nspname )        ) as sp     ON sp.nspoid = typnamespace  WHERE typname = $1  ORDER BY sp.r, pg_type.oid DESC LIMIT 1
2014-05-12 23:04:46 CEST postgres@java_test_ee7_jpa [4266] DETAIL:  parameters: $1 = '_text'
--> 1009
2014-05-12 23:04:46 CEST postgres@java_test_ee7_jpa [4266] LOG:  execute <unnamed>: SELECT e.typdelim FROM pg_catalog.pg_type t, pg_catalog.pg_type e WHERE t.oid = $1 and t.typelem = e.oid
2014-05-12 23:04:46 CEST postgres@java_test_ee7_jpa [4266] DETAIL:  parameters: $1 = '1009'
--> ','

在最后一个示例中,将打印一个新警告。如果我从persistence.xml中删除ArrayExample模型类,它就会消失,因此它似乎相关:

org.eclipse.persistence.default
WARNING: The default table generator currently only supports generating default table schema from a relational project.

0 个答案:

没有答案