gpt4 book ai didi

java - JPA2 : ElementCollection of Enum combined with AttributeConverter

转载 作者:太空宇宙 更新时间:2023-11-04 13:28:13 24 4
gpt4 key购买 nike

我试图结合两个概念:

  • 存储枚举,它们是实体的一对多关系(使用 @ElementCollection@CollectionTable)
  • 不是通过字符串或序号来保存枚举,而是通过唯一 ID(使用 @AttributeConverter)来保存

它们单独应该工作正常(根据文档判断),但是当我将它们组合起来时,我在构建过程中遇到错误。

这是实体:

@Entity
@Table(name = "push_tokens")
public class PushTokenDAO implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long idx;

@ElementCollection(targetClass = NotificationType.class, fetch = FetchType.EAGER)
@Convert(converter = NotificationTypeConverter.class)
@CollectionTable(
name = "notificationtypes_of_tokens",
joinColumns = @JoinColumn(name = "fk_tokenIdx")
)
@Column(name = "notificationType")
private List<NotificationType> notificationTypes;

/* ... */

}

这是枚举:

public enum NotificationType {
NEW_TRANSFER(10),
DELIVERY_REPORT(20),
UPLOAD_CONFIRMATION(30),
EXPIRY_WARNING(40);

private final int code;

NotificationType(int code) {
this.code = code;
}

public static NotificationType parse(int code) {
for (NotificationType t : NotificationType.values()) {
if (t.code == code) {
return t;
}
}
throw new IllegalArgumentException("No such NotificationType");
}

public int getCode() {
return code;
}
}

转换器:

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Converter(autoApply = true)
public class NotificationTypeConverter implements AttributeConverter<NotificationType, Integer> {

@Override
public Integer convertToDatabaseColumn(NotificationType x) {
return x.getCode();
}

@Override
public NotificationType convertToEntityAttribute(Integer y) {
return NotificationType.parse(y);
}

}

集合表notificationtypes_of_tokens中只有两个数字列:

CREATE TABLE `notificationtypes_of_tokens` (
`fk_tokenIdx` bigint(11) unsigned NOT NULL,
`notificationType` int(11) unsigned NOT NULL,
PRIMARY KEY (`fk_tokenIdx`,`notificationType`),
KEY `fk_tokenIdx` (`fk_tokenIdx`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

问题发生在 EclipseLink 静态编织期间,这是构建过程的一部分:

Failed to execute goal de.empulse.eclipselink:staticweave-maven-plugin:1.0.0:weave (default) 
on project skp-server: Execution default of goal de.empulse.eclipselink:staticweave-maven-plugin:1.0.0:weave
failed: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070):
org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [skp-server-PU] failed.
Internal Exception: Exception [EclipseLink-7351] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070):
org.eclipse.persistence.exceptions.ValidationException
Exception Description: The converter class [com.skalio.skaliopush.db.NotificationTypeConverter]
specified on the mapping attribute [notificationTypes] from the class
[com.skalio.skaliopush.db.entity.PushTokenDAO] was not found. Please ensure the converter
class name is correct and exists with the persistence unit definition.

如果我没有在实体中专门引用转换器(省略@Convert),则转换不会完成;枚举被映射到它的序数。这不是我想要的。

仅供引用:代码运行在 Java SE VM 上,而不是应用程序服务器或其他东西上。我必须在 persistence.xml 中指定每个实体类。

最佳答案

该解决方案与@ElementCollection、Enums或与@AttributeConverter的组合无关。

相反,答案就在问题 what has to be specified in persistence.xml when running on Java SE 中。 。错误消息也以这种方式暗示(“未找到转换器...”)。

persistence.xml 中指定转换器类后,编织工作正常,代码构建并按预期运行。总之:组合@ElementCollection、Enum和@AttributeConverter效果很好,如上面的代码所示。

关于java - JPA2 : ElementCollection of Enum combined with AttributeConverter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32444403/

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