@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传递给我的服务以保存
这是保存后我的数据库的样子
最佳答案
在您的Notification
Java对象中,user
属性具有@Transient
批注。这意味着它不会持久。
因此,该属性在控制器的响应中为null。
正如其他人所说,您的JavaScript和Java结构也有所不同。在Notification Java对象中,您的列表应命名为users
上次编辑后:
为什么UserNotification
不是@Entity
?另外,您还需要告诉JPA Notification
与UserNotification
之间的关系类型。我的猜测是两个类都缺少大量注释。阅读此:https://en.wikibooks.org/wiki/Java_Persistence/OneToMany
关于javascript - 如何使用Java在对象中保存列表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50020723/