在没有加入的情况下使用 JPA 保持 OneToMany 关系 table
Persisting OneToMany relationship Using JPA without Join table
我有下表S:
`notification_job_execution`
(`jobId`,
`createdDate`,
`messageBody`)
`notification_task_execution`
(`taskId`,
`jobId`,
`status`,
`createdDate`)
Havin oneToMany 关系车间 (notification_job_execution ,notification_task_execution)
(1..n)
我有以下实体
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private String jobId;
@Temporal(TemporalType.DATE)
private Date createdDate;
private String messageBody;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "notificationJobEntity", cascade=CascadeType.ALL)
private Set<NotificationTaskEntity> notificationTaskEntities = new HashSet<NotificationTaskEntity>(
0);
和:
@Entity
@Table(name = "notification_task_execution")
public class NotificationTaskEntity implements Serializable{
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private long taskId;
private int jobId;
private String status;
@Temporal(TemporalType.DATE)
private Date createdDate;
@ManyToOne(optional = false)
private NotificationJobEntity notificationJobEntity;
我正在使用 Spring 和 JPA 以坚持这种方式:
NotificationJobEntity notificationJobEntity=new NotificationJobEntity();
notificationJobEntity.setCreatedDate(new Date());
notificationJobEntity.setMessageBody("hello youu");
NotificationTaskEntity notificationTaskEntity=new NotificationTaskEntity();
notificationTaskEntity.setCreatedDate(new Date());
notificationTaskEntity.setStatus("success");
notificationTaskEntity.setNotificationJobEntity(notificationJobEntity);
notificationTaskEntity.setNotificationJobEntity(notificationJobEntity);
notificationJobEntity.getNotificationTaskEntities().add(notificationTaskEntity);
notificationDao.save(notificationJobEntity);
我在数据库中看不到子记录持续存在(即 notificationTaskEntity)。
我如何才能持久化父级并在后台将 notificationTaskEntity 也持久化到数据库?
您必须在 NotificationTaskEntity
中指定 CascadeType
@ManyToOne(optional = false,CascadeType.PERSIST)
private NotificationJobEntity notificationJobEntity;
我认为您可能只需要添加 @JoinColumn
注释以指示数据库中的哪个字段 table 代表 NotificationTaskEntity
.
的 ID
@ManyToOne(optional = false)
@JoinColumn(name = "jobid")
private NotificationJobEntity notificationJobEntity;
我有下表S:
`notification_job_execution`
(`jobId`,
`createdDate`,
`messageBody`)
`notification_task_execution`
(`taskId`,
`jobId`,
`status`,
`createdDate`)
Havin oneToMany 关系车间 (notification_job_execution ,notification_task_execution) (1..n)
我有以下实体
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private String jobId;
@Temporal(TemporalType.DATE)
private Date createdDate;
private String messageBody;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "notificationJobEntity", cascade=CascadeType.ALL)
private Set<NotificationTaskEntity> notificationTaskEntities = new HashSet<NotificationTaskEntity>(
0);
和:
@Entity
@Table(name = "notification_task_execution")
public class NotificationTaskEntity implements Serializable{
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private long taskId;
private int jobId;
private String status;
@Temporal(TemporalType.DATE)
private Date createdDate;
@ManyToOne(optional = false)
private NotificationJobEntity notificationJobEntity;
我正在使用 Spring 和 JPA 以坚持这种方式:
NotificationJobEntity notificationJobEntity=new NotificationJobEntity();
notificationJobEntity.setCreatedDate(new Date());
notificationJobEntity.setMessageBody("hello youu");
NotificationTaskEntity notificationTaskEntity=new NotificationTaskEntity();
notificationTaskEntity.setCreatedDate(new Date());
notificationTaskEntity.setStatus("success");
notificationTaskEntity.setNotificationJobEntity(notificationJobEntity);
notificationTaskEntity.setNotificationJobEntity(notificationJobEntity);
notificationJobEntity.getNotificationTaskEntities().add(notificationTaskEntity);
notificationDao.save(notificationJobEntity);
我在数据库中看不到子记录持续存在(即 notificationTaskEntity)。
我如何才能持久化父级并在后台将 notificationTaskEntity 也持久化到数据库?
您必须在 NotificationTaskEntity
CascadeType
@ManyToOne(optional = false,CascadeType.PERSIST)
private NotificationJobEntity notificationJobEntity;
我认为您可能只需要添加 @JoinColumn
注释以指示数据库中的哪个字段 table 代表 NotificationTaskEntity
.
@ManyToOne(optional = false)
@JoinColumn(name = "jobid")
private NotificationJobEntity notificationJobEntity;