如何在Java中将列表存入对象?附实体类代码示例
嗨,我来帮你梳理下怎么在Java里把UserNotification列表存入Notification对象,还有针对你给出的实体类需要调整的细节:
1. 先修正实体类的基础问题
你的Notification类里的列表字段有几个需要优化的点,同时如果是JPA持久化场景,还要补全关联映射:
- 字段访问修饰符建议用
private,通过getter/setter访问,符合封装原则 - 列表字段名
user语义模糊,改成userNotifications更清晰 - 初始化列表避免空指针,同时添加JPA关联注解(如果
UserNotification也是持久化实体)
修正后的Notification.java:
@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; // JPA一对多关联:一个Notification对应多个UserNotification @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) @JoinColumn(name = "notification_id") // 数据库外键字段名 private List<UserNotification> userNotifications = new ArrayList<>(); // JPA需要无参构造 public Notification() {} // 带核心属性的构造方法 public Notification(Long businessId, String actionBy, String date, String notification) { this.businessId = businessId; this.actionBy = actionBy; this.date = date; this.notification = notification; } // 省略其他属性的getter/setter public List<UserNotification> getUserNotifications() { return userNotifications; } public void setUserNotifications(List<UserNotification> userNotifications) { this.userNotifications = userNotifications; } // 便捷方法:直接添加单个UserNotification public void addUserNotification(UserNotification userNotification) { this.userNotifications.add(userNotification); } }
如果UserNotification也是JPA实体,需要补全实体注解和双向关联(可选):
@Entity public class UserNotification { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String username; // 原字段名`user`建议改为`username`或`userId` // 可选:双向关联回Notification @ManyToOne @JoinColumn(name = "notification_id") private Notification notification; public UserNotification() {} public UserNotification(String username) { this.username = username; } // 省略getter/setter }
2. 业务代码中实现列表存入操作
有两种常见方式可以把列表存入Notification对象:
方式1:直接设置整个列表
// 构建UserNotification列表 List<UserNotification> userList = new ArrayList<>(); userList.add(new UserNotification("alice")); userList.add(new UserNotification("bob")); // 创建Notification并绑定列表 Notification notification = new Notification(1001L, "system", "2024-05-20", "账号安全提醒"); notification.setUserNotifications(userList);
方式2:逐个添加元素(用便捷方法)
Notification notification = new Notification(1001L, "system", "2024-05-20", "账号安全提醒"); notification.addUserNotification(new UserNotification("alice")); notification.addUserNotification(new UserNotification("bob"));
3. 持久化到数据库(JPA场景)
如果要把带列表的Notification存入数据库,借助Spring Data JPA的Repository即可:
// 定义Repository接口 @Repository public interface NotificationRepository extends JpaRepository<Notification, Long> {} // 业务层调用保存 @Service public class NotificationService { @Autowired private NotificationRepository notificationRepository; public void saveNotificationWithUsers() { Notification notification = new Notification(1001L, "system", "2024-05-20", "账号安全提醒"); notification.addUserNotification(new UserNotification("alice")); notification.addUserNotification(new UserNotification("bob")); // 因为设置了CascadeType.ALL,UserNotification会自动随Notification一起保存 notificationRepository.save(notification); } }
内容的提问来源于stack exchange,提问作者Mark
相关产品推荐
相关产品推荐

