gpt4 book ai didi

javascript - 如何使用 Java 将列表保存在对象中?

转载 作者:行者123 更新时间:2023-12-01 02:07:00 24 4
gpt4 key购买 nike

保存之前 enter image description here

保存后 enter image description here

@Entity
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Notification {


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long notificationId;
private Long businessId;
private String actionBy;
private String date;
private String notification;
public ArrayList<UserNotification> user;

//constructor here

//getters setters here
}

而 UserNotification.java 是

public class UserNotification {
private Long id;
private String user;
private String notifCount;
//getters setters here
}

我不知道为什么它返回 null。我什么时候犯了错误?

编辑:

 var usersObj=[];
BusinessRoleService.getByBusinessId($sessionStorage.businessRole.business).then(function(response){
if(response.status==200){
for(var x=0;x<response.data.length;x++){
usersObj.push({id: x, user: response.data[x].userId, notifCount: $scope.notification});
}


}
});

var obj = {
"businessId": businessId,
"actionBy": user,
"date": date,
"notification": user+" "+action,
"user": usersObj
}

然后我会将 obj 传递给我的服务来保存

这是我的数据库保存后的样子

enter image description here

最佳答案

在您的 Notification Java 对象中,user 属性具有 @Transient 注释。这意味着它不会被持久化。

因此, Controller 响应中的属性为 null。

正如其他人所说,JavaScript 和 Java 结构也存在差异。在您的通知 java 对象中,您的列表应命名为 users

上次编辑后:

为什么UserNotification不是@Entity?您还需要告诉 JPA NotificationUserNotification 之间的关系类型。我的猜测是这两个类都缺少一堆注释。阅读此内容:https://en.wikibooks.org/wiki/Java_Persistence/OneToMany

关于javascript - 如何使用 Java 将列表保存在对象中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50020723/

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