簡體   English   中英

Hibernate和JPA 2.1-將java.time.LocalDateTime映射為java.util.Map中的鍵

[英]Hibernate & JPA 2.1 - mapping java.time.LocalDateTime as key in java.util.Map

從域模型類給出以下枚舉:

public enum OperationMode {
    BATTERY_CHANGE_MODE,
    PBP_MODE
}

我還定義了一個AttributeConverter來在LocalDateTime和TimeStamp之間轉換

@Converter
public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> {

   @Override
   public Timestamp convertToDatabaseColumn(LocalDateTime locDateTime) {
       return (locDateTime == null ? null : Timestamp.valueOf(locDateTime));
   }

    @Override
    public LocalDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
        return (sqlTimestamp == null ? null : sqlTimestamp.toLocalDateTime());
    }
}

在Entity類中,我需要為java.utitl.Map定義一個有效的映射作為元素集合:

@ElementCollection
@MapKeyColumn(name = "time")
@Convert(converter = LocalDateTimeAttributeConverter.class)
// some annotations are missing here...
private Map<LocalDateTime, OperationMode> operationHistory;

建議使用什么方法使它正常工作?

解決方案是以下映射:

@ElementCollection
@MapKeyColumn(name = "time")
@Convert(converter = LocalDateTimeAttributeConverter.class, attributeName = "key")
@Enumerated(EnumType.STRING)
private Map<LocalDateTime, OperationMode> operationHistory;

缺少attributeName'key '

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM