创建PostgreSQL数组类型,如;文本[]”;使用JPA(EclipseLink)
我很难创建一个JPA模型类,EclipseLink将从中创建 以下是PostgreSQL DDL:创建PostgreSQL数组类型,如;文本[]”;使用JPA(EclipseLink),postgresql,jpa,eclipselink,Postgresql,Jpa,Eclipselink,我很难创建一个JPA模型类,EclipseLink将从中创建 以下是PostgreSQL DDL: CREATE TABLE array_example ( id serial not null, params text[] not null ); 据我所知,PostgreSQL的数组类型不是SQL标准,因此不包含在JPA标准中。EclipseLink似乎对非标准扩展有某种支持。到目前为止,我得出了以下结论: .... import org.eclipse.persistence.a
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;
....
PostgreSQL日志文件包含两个可疑的SELECT(返回1009 for _text,然后返回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)
也许此示例项目有帮助:
我在一个删除的答案中建议,它似乎在寻找
text[]
的数组类型,即文本数组的数组,这是不受支持的,所以你应该尝试一下text
。这似乎解决了眼前的问题,但根据上面的编辑创建了不同的ORM。恐怕很多Java ORM除了关系数据库最基本的最低公分母特性外,什么都不擅长。在本例中,我认为它试图生成字段,就好像它是EclipseLink“NoSQL”支持的非关系对象一样。最后一个查询确实为文本类型的内置数组找到了正确的oid(1009)。警告指出,只有关系数据库类才支持模式生成。一旦添加了结构或数组扩展类型,就不能使用模式生成,因为对所有各种DB类型的支持都不存在,而且实际上,所有这些类型都是可用的—这些类型用于遗留系统上的自定义数据库类型。在大多数情况下,最好使用现有的可移植类型,或者使用脚本创建数据库。
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>
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
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
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'
--> ','
org.eclipse.persistence.default
WARNING: The default table generator currently only supports generating default table schema from a relational project.