Java JPA2:Enum与AttributeConverter组合的ElementCollection

Java JPA2:Enum与AttributeConverter组合的ElementCollection,java,jpa,Java,Jpa,我试图结合两个概念: 存储实体的一对多关系中的枚举(使用@ElementCollection,@CollectionTable) 不是通过字符串或序号,而是通过唯一ID(使用@AttributeConverter)持久化枚举 单独使用它们应该可以很好地工作(根据文档判断),但是当我将它们组合在一起时,在构建过程中会出现错误 以下是实体: @Entity @Table(name = "push_tokens") public class PushTokenDAO implements Seri

我试图结合两个概念:

  • 存储实体的一对多关系中的枚举(使用
    @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;

    /* ... */

}
转换器:

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);
    }

}
问题发生在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
),则转换不会完成;枚举映射到其序号。这不是我想要的


仅供参考:代码运行在JavaSE虚拟机上,而不是应用服务器或其他东西上。我必须在
persistence.xml
中指定每个实体类。解决方案与
@ElementCollection
、枚举或与
@AttributeConverter
的组合无关

相反,答案在于这个问题。错误消息也以这种方式暗示('未找到转换器…')


persistence.xml
中指定转换器类后,编织工作正常,代码生成,并按预期运行。总而言之:根据上面的代码,将
@ElementCollection
、Enum和
@AttributeConverter
组合使用效果很好。

解决方案与
@ElementCollection
、Enum或与
@AttributeConverter
的组合无关

相反,答案在于这个问题。错误消息也以这种方式暗示('未找到转换器…')

persistence.xml
中指定转换器类后,编织工作正常,代码生成,并按预期运行。总之:根据上面的代码,将
@ElementCollection
、Enum和
@AttributeConverter
组合在一起就可以了。

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;
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.