我已经将我的问题缩小到了一些hibernate注释和对象反序列化之间的交互。我使用spring boot和hibernate将数据存储到postgres数据库中。 反序列化是由杰克逊完成,所有这三个例子都使用一个JSON字符串,它是有效的,看起来有点像:{"id":-1,"alertType":"someType","alertInfos":[{"id":-1,"description":"somedescription"}],...}
春季休息杰克逊对象反序列化失败,冬眠注释
我所拥有的是另一个类引用的类:
//bi-directional many-to-one association to AlertInfo
@OneToMany(mappedBy="alert", cascade=CascadeType.ALL)
private List<AlertInfo> alertInfos;
一旦这是反序列化我会得到:
模板不能为空或空! (通过参考链:model.ressource.Alert [ “alertInfos”] - >的java.util.ArrayList [0])
注意的东西决定
ArrayList
是List
接口的实现
反序列化
//bi-directional many-to-one association to AlertInfo
//@OneToMany(mappedBy="alert", cascade=CascadeType.ALL)
private ArrayList<AlertInfo> alertInfos;
当然我不能把它存储到数据库了,因为Hibernate不知道的关系:如果我把休眠注释和变化ArrayList
工作。
谁能告诉我为什么选项三不起作用?
这使得它甚至不启动应用程序的逻辑选项:
//bi-directional many-to-one association to AlertInfo
@OneToMany(mappedBy="alert", cascade=CascadeType.ALL)
private ArrayList<AlertInfo> alertInfos;
这会导致:
非法尝试映射非集合作为一个@OneToMany,@ManyToMany或@ CollectionOfElements:model.ressource.Alert.alertInfos
向谁可能关注:AlertInfo.class的一面:
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "alert_id")
private Alert alert;
我该如何解决最后一个问题,以便像预期的那样工作?
EDIT 1:
请求实体:
Alert.class
@Entity
@Table(name = "alerts", schema = "ressource_db", uniqueConstraints = { @UniqueConstraint(columnNames = { "id" }) })
@NamedQuery(name = "Alert.findAll", query = "SELECT a FROM Alert a")
public class Alert implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(sequenceName = "ressource_db.id_seq", name = "AlertIdSequence", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AlertIdSequence")
private Long id;
@Column(name = "original_id")
private String identifier;
@Transient
private String sender;
@Type(type = "org.hibernate.spatial.GeometryType")
@JsonSerialize(using = GeometrySerializer.class)
@JsonDeserialize(using = GeometryDeserializer.class)
private Geometry geometry;
@Column(name = "last_edit_date_time")
private Timestamp lastEditDateTime;
@Column(name = "msg_status")
private String status;
@Column(name = "msg_type")
private String msgType;
@Type(type = "org.hibernate.spatial.GeometryType")
@JsonSerialize(using = GeometrySerializer.class)
@JsonDeserialize(using = GeometryDeserializer.class)
private Point position;
private String scope;
@Column(name = "sent_date_time")
private Timestamp sent;
@Column(name = "version_count")
private Long versionCount;
@Column(name = "alert_msg")
private String alertmsg;
@Column(name = "is_current")
private boolean isCurrent;
// bi-directional many-to-one association to AlertInfo
@OneToMany(mappedBy = "alert", cascade = CascadeType.ALL)
private List<AlertInfo> alertInfos;
public Alert() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getIdentifier() {
return identifier;
}
public void setIdentifier(String identifier) {
this.identifier = identifier;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public Geometry getGeometry() {
return geometry;
}
public void setGeometry(Geometry geometry) {
this.geometry = geometry;
}
public Timestamp getLastEditDateTime() {
return lastEditDateTime;
}
public void setLastEditDateTime(Timestamp lastEditDateTime) {
this.lastEditDateTime = lastEditDateTime;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMsgType() {
return msgType;
}
public void setMsgType(String msgType) {
this.msgType = msgType;
}
public Point getPosition() {
return position;
}
public void setPosition(Point position) {
this.position = position;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
public Timestamp getSent() {
return sent;
}
public void setSent(Timestamp sent) {
this.sent = sent;
}
public Long getVersionCount() {
return versionCount;
}
public void setVersionCount(Long versionCount) {
this.versionCount = versionCount;
}
public String getAlertmsg() {
return alertmsg;
}
public void setAlertmsg(String alertmsg) {
this.alertmsg = alertmsg;
}
public boolean isCurrent() {
return isCurrent;
}
public void setCurrent(boolean isCurrent) {
this.isCurrent = isCurrent;
}
public List<AlertInfo> getAlertInfos() {
return this.alertInfos;
}
public void setAlertInfos(List<AlertInfo> alertInfos) {
this.alertInfos = alertInfos;
}
public AlertInfo addAlertInfo(AlertInfo alertInfo) {
if (getAlertInfos() == null) {
this.alertInfos = new ArrayList<AlertInfo>();
}
getAlertInfos().add(alertInfo);
alertInfo.setAlert(this);
return alertInfo;
}
public AlertInfo removeAlertInfo(AlertInfo alertInfo) {
getAlertInfos().remove(alertInfo);
alertInfo.setAlert(null);
return alertInfo;
}
}
AlertInfo.class
@Entity
@Table(name = "alert_infos", schema = "ressource_db", uniqueConstraints = { @UniqueConstraint(columnNames = { "id" }) })
@NamedQuery(name = "AlertInfo.findAll", query = "SELECT a FROM AlertInfo a")
public class AlertInfo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(sequenceName = "ressource_db.id_seq", name = "AlertInfoIdSequence", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AlertInfoIdSequence")
private Long id;
@Column(name = "event_category")
private ArrayList<String> category;
@Transient
private String event;
private String urgency;
private String severity;
private String certainty;
@Transient
private List<CapAlertInfoEventCode> eventCodes;
@Transient
private Timestamp effective;
@Transient
private Timestamp expires;
@Transient
private String senderName;
private String headline;
private String description;
// bi-directional many-to-one association to Alert
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "alert_id")
private Alert alert;
public AlertInfo() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCertainty() {
return certainty;
}
public void setCertainty(String certainty) {
this.certainty = certainty;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getEvent() {
return event;
}
public void setEvent(String event) {
this.event = event;
}
public String getSeverity() {
return severity;
}
public void setSeverity(String severity) {
this.severity = severity;
}
public String getUrgency() {
return urgency;
}
public void setUrgency(String urgency) {
this.urgency = urgency;
}
public List<CapAlertInfoEventCode> getEventCodes() {
return eventCodes;
}
public void setEventCodes(List<CapAlertInfoEventCode> eventCodes) {
this.eventCodes = eventCodes;
}
public Timestamp getEffective() {
return effective;
}
public void setEffective(Timestamp effective) {
this.effective = effective;
}
public Timestamp getExpires() {
return expires;
}
public void setExpires(Timestamp expires) {
this.expires = expires;
}
public String getSenderName() {
return senderName;
}
public void setSenderName(String senderName) {
this.senderName = senderName;
}
public String getHeadline() {
return headline;
}
public void setHeadline(String headline) {
this.headline = headline;
}
public ArrayList<String> getCategory() {
return category;
}
public void setCategory(ArrayList<String> category) {
this.category = category;
}
public Alert getAlert() {
return alert;
}
public void setAlert(Alert alert) {
this.alert = alert;
}
}
请在这里发布整个实体 – WeMakeSoftware
添加实体到原始问题的描述 – Baiteman