gpt4 book ai didi

java - JPA 多对多关系与额外的枚举列

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:20:47 25 4
gpt4 key购买 nike

<分区>

我正在尝试将具有要枚举的对象映射的实体保存到 Google App Engine 数据存储区中。实体类使用 JPA 注释。

Event class

import com.google.appengine.datanucleus.annotations.Unowned;
import com.google.appengine.api.datastore.Key;
import java.util.Map;
import javax.persistence.*;
import lombok.Builder;
import lombok.Data;

@Entity
@Builder
public @Data class Event {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key key;

// I want a map belonging to event in order to query a particular user whether he confirmed his participation in the event
// All addressees are initially present in this map with response set to UNDEFINED
// If user has received and read notification, than the response is updated to YES, NO, or MAYBE
@Unowned
@ElementCollection
@CollectionTable(name = "user_response")
@MapKeyJoinColumn(name = "user_id")
@Enumerated
@Column(name = "response")
private Map<User, Response> addressees;

}

Response class

public enum Response {
UNDEFINED, YES, NO, MAYBE
}

我没有在 User 类中定义任何对此 map 的引用。这是一种单向关系。

User class

@Entity
public @Data class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key key;
}

Event.addressees 列看起来很棘手。所以我运行了测试以检查是否一切正常。好吧,事实并非如此。当我尝试将 Event 实体保存到数据存储时出现异常:

java.lang.IllegalArgumentException: addressees: Response is not a supported property type.
at com.google.appengine.api.datastore.DataTypeUtils.checkSupportedSingleValue(DataTypeUtils.java:235)
at com.google.appengine.api.datastore.DataTypeUtils.checkSupportedValue(DataTypeUtils.java:199)
at com.google.appengine.api.datastore.DataTypeUtils.checkSupportedValue(DataTypeUtils.java:173)
at com.google.appengine.api.datastore.DataTypeUtils.checkSupportedValue(DataTypeUtils.java:148)
at com.google.appengine.api.datastore.PropertyContainer.setProperty(PropertyContainer.java:101)

根据 DataNucleus,默认情况下,枚举是一种可持久化的数据类型。所以我不明白为什么我会收到这样的错误消息:“Response is not a supported property type”
我怀疑问题出在 User 类上。可能 Event 到 Users 的关联还不够,User 也应该和 Events 有关联。因此,我将 events 字段添加到 User 中,如下所示:

@Unowned
@ElementCollection
@CollectionTable(name = "user_event_responses")
@ManyToMany(mappedBy="addressees", targetEntity = Event.class)
@MapKeyJoinColumn
@Enumerated
@Column(name = "response")
private Map<Event, Response> events;

无论如何都没有用。然后我看了类似的问题,并没有找到快速的答案。
请给我看一个多对多关系的例子,在 DataNucleus/JPA 中有一个额外的列!

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