gpt4 book ai didi

java - 在 Java 中使用 PostgreSQL 域和结构类型

转载 作者:搜寻专家 更新时间:2023-11-01 03:29:50 24 4
gpt4 key购买 nike

创建 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,尝试使用 EmbeddableEmbedded 注释:

@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 - 在 Java 中使用 PostgreSQL 域和结构类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3486961/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com