在Java中使用PostgreSQL Domain和Struct类型

创建EntityManagerFactory实例后,我收到错误消息:...Exception Description: Predeployment of PersistenceUnit [aPU] failed.Internal Exception: Exception [EclipseLink-7157] (Eclipse Persistence Services ...

创建EntityManagerFactory实例后,我收到错误消息:

...
Exception Description: Predeployment of PersistenceUnit [aPU] failed.
Internal Exception: Exception [EclipseLink-7157] (Eclipse Persistence Services - 2.0.2.v20100323-r6872): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Entity class [class Table] must use a @JoinColumn instead of @Column to map its relationship attribute [Price].
...

列价格是域类型(例如:CREATE TYPE MONEY AS NUMERIC(10,2)FINAL).

如何使用Domain或Struct PostgreSQL类型? JPA可以吗?

解决方法:

问题不在于列类型,至少这不是JPA现在抱怨的问题,问题是JPA不知道如何映射不是基本支持类型之一的Price类型,也不知道实体.

2.1.1 Persistent Fields and Properties

The persistent fields or properties of
an entity may be of the following
types: Java primitive types;
java.lang.String; other Java
serializable types (including
wrappers of the primitive types,
java.math.BigInteger,
java.math.BigDecimal,
java.util.Date,
java.util.Calendar,
java.sql.Date, java.sql.Time,
java.sql.Timestamp, user-defined
serializable types, byte[],
Byte[], char[], and Character[]; enums; entity
types and/or collections of entity types; and embeddable classes (see section 2.1.5).

使用标准JPA,尝试使用Embeddable和Embedded注释:

@Embeddable
public class Price {
    private BigDecimal amount;
    ...
}

然后在你的实体中:

@Embedded
@AttributeOverrides({
    @AttributeOverride(name="amount", column=@Column(name="AMOUNT"))
})
public Price getPrice() { ... }

另一种选择是使用TransformationMapping(特定于EclipseLink).

参考

> JPA 1.0规范

> 2.1.5可嵌入类
> 2.1.6映射非关系字段或属性的默认值
> 9.1.34可嵌入注释
> 9.1.35嵌入式注释

本文标题为:在Java中使用PostgreSQL Domain和Struct类型

基础教程推荐