gpt4 book ai didi

java - JPA:2 个数组列表放入 1 个数据库表中

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

我有一个代表员工工作时间的 Java 类,名为 Worktime。它包含 startTimesendTimesArrayList。我尝试使用以下注释将其映射到数据库:

@Entity
@Table(name="worktime")
public class Worktime {

@Id
@Column(name="worktime_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int worktimeId;

@Column(name="date")
@Temporal(TemporalType.DATE)
private Date date;

@ElementCollection
@CollectionTable(name="WORKTIME_TIMES")
@Temporal(TemporalType.TIME)
private List<Date> startTimes = new ArrayList<>();

@ElementCollection
@CollectionTable(name="WORKTIME_TIMES")
@Temporal(TemporalType.TIME)
private List<Date> endTimes = new ArrayList<>();

@ManyToOne
@JoinColumn(name="person_id")
private Employee employee;
...
}

我在数据库中得到的结构如下:

我想要的是这两个值位于同一行。有机会通过索引来做到这一点吗?感谢您的帮助!

最佳答案

也许最好创建另一个@Entity并添加@OneToMany关系:

工作时间:

...
@OneToMany
@JoinColumn(name = "worktime_id", referencedColumnName = "worktime_id")
private WorkTimeTimes times;
...

新实体WorkTimeTimes:

@Entity
@Table(name="worktime_times")
public class WorkTimeTimes {
@Id
@Column(name="worktime_times_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int worktimeTimesId;

@Column(name="worktime_id")
private int worktimeId;

@Temporal(TemporalType.TIME)
@Column(name="start_time")
private Date startTime;

@Temporal(TemporalType.TIME)
@Column(name="end_time")
private Date endTime;

...

}

关于java - JPA:2 个数组列表放入 1 个数据库表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51638586/

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