基本上我在使用JavaFX以及Spring和Hibernate创建GUI。问题主要与JPA
有关假设第一次用户在数据库中插入数据(列星期一 TRUE ,其他列 FALSE )
ID EMPLOYEES START_TIME END_TIME MONDAY TUESDAY WEDNESSDAY THURSDAY FRIDAY SATURDAY SUNDAY
1 5 1.0 7.0 TRUE FALSE FALSE FALSE FALSE FALSE FALSE
2 7 9.0 11.0 TRUE FALSE FALSE FALSE FALSE FALSE FALSE
我的问题是第二次将数据插入db时,它会复制前两行,这不是我想要的。
ID EMPLOYEES START_TIME END_TIME MONDAY TUESDAY WEDNESSDAY THURSDAY FRIDAY SATURDAY SUNDAY
1 5 1.0 7.0 TRUE FALSE FALSE FALSE FALSE FALSE FALSE
2 7 9.0 11.0 TRUE FALSE FALSE FALSE FALSE FALSE FALSE
3 5 1.0 7.0 TRUE FALSE FALSE FALSE FALSE FALSE FALSE
4 7 9.0 11.0 TRUE FALSE FALSE FALSE FALSE FALSE FALSE
5 9 13.0 18.0 TRUE FALSE FALSE FALSE FALSE FALSE FALSE
我想要的是这样的:
ID EMPLOYEES START_TIME END_TIME MONDAY TUESDAY WEDNESSDAY THURSDAY FRIDAY SATURDAY SUNDAY
1 5 1.0 7.0 TRUE FALSE FALSE FALSE FALSE FALSE FALSE
2 7 9.0 11.0 TRUE FALSE FALSE FALSE FALSE FALSE FALSE
3 9 13.0 18.0 TRUE FALSE FALSE FALSE FALSE FALSE FALSE
或者其他解决方案可以删除星期一列 TRUE 的所有行,并像以前一样重新创建,但自动生成的ID将是新的:
ID EMPLOYEES START_TIME END_TIME MONDAY TUESDAY WEDNESSDAY THURSDAY FRIDAY SATURDAY SUNDAY
3 5 1.0 7.0 TRUE FALSE FALSE FALSE FALSE FALSE FALSE
4 7 9.0 11.0 TRUE FALSE FALSE FALSE FALSE FALSE FALSE
5 9 13.0 18.0 TRUE FALSE FALSE FALSE FALSE FALSE FALSE
这是我的DAO课程:
@Repository
public class HistogramDAO {
@PersistenceContext
private EntityManager entityManager;
public void create(Histogram histogram) {
entityManager.persist(histogram);
}
public void update(Histogram histogram) {
entityManager.merge(histogram);
}
public Histogram getBesoinRequestById(long id) {
return entityManager.find(Histogram.class, id);
}
public void delete(long id) {
Histogram histogram = getBesoinRequestById(id);
if (histogram != null) {
entityManager.remove(histogram);
}
}
public List<Long> getBesoinRequestByMonday() {
Query query = entityManager.createQuery("SELECT h.id FROM Histogram h WHERE h.monday = true AND h.tuesday = false AND h.wednessday = false AND h.thursday = false AND h.friday = false AND h.saturday = false AND h.sunday= false");
return query.getResultList();
}
public void updateBesoinRequestByMonday(long id) {
Histogram histogram = getBesoinRequestById(id);
if (histogram != null) {
entityManager.merge(histogram);
}
}
}
这是我的服务类:
@Component
@Transactional
public class HistogramService {
@Autowired
private HistogramDAO histogramDAO;
public void create(Histogram histogram) {
histogramDAO.create(histogram);
}
public void update(Histogram histogram) {
histogramDAO.update(histogram);
}
public void delete(long id) {
histogramDAO.delete(id);
}
public List<Long> getBesoinRequestByMonday() {
return histogramDAO.getBesoinRequestByMonday();
}
public void updateBesoinRequestByMonday(long id) {
histogramDAO.updateBesoinRequestByMonday(id);
}
}
这就是我在按钮点击时使用它的方式(正如您在评论代码中看到的那样,我尝试了不同的技巧,但它们都没有工作):
Iterator it = seriesContainer.iterator();
int j = 1;
while(it.hasNext()){
XYChart.Series<Number, Number> test = (Series<Number, Number>) it.next();
System.out.println(test.getData().size());
for(int i=0; i<test.getData().size(); i++){
maxArray.add(test.getData().get(i).getXValue().doubleValue());
}
double max = maxArray.stream().collect(Collectors.summarizingDouble(Double::doubleValue)).getMax();
Histogram histogram = new Histogram(test.getData().get(0).getYValue().intValue(),
test.getData().get(0).getXValue().doubleValue(),max,cbMonday.isSelected(),
cbTuesday.isSelected(),cbWednessday.isSelected(),cbThursday.isSelected(),cbFriday.isSelected(),
cbSaturday.isSelected(),cbSunday.isSelected());
histogramService.create(histogram); // Here I'm using JPA to store in db
// List<Long> ids = histogramService.getBesoinRequestByMonday();
// if(ids.isEmpty()){
// for(long entry : ids) {
// if(histogramService.getBesoinRequestByMonday().contains(entry)){
// histogramService.updateBesoinRequestByMonday(entry);
//histogramService.create(histogram);
// }
// }
// }
// if(histogramService.getBesoinRequestByMonday().isEmpty()){
// histogramService.create(histogram);
// } else{
// histogramService.create(histogram);
// List<Long> ids = histogramService.getBesoinRequestByMonday();
// for(long entry : ids) {
// histogramService.updateBesoinRequestByMonday(entry);
// histogramService.getBesoinRequestByMonday().clear();
// }
// }
maxArray.clear();
j++;
更新:直方图实体:
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.springframework.stereotype.Component;
@Entity
@Table(name = "HISTOGRAM")
public class Histogram implements Serializable {
private long id;
private int employees;
private double startTime;
private double endTime;
private boolean monday;
private boolean tuesday;
private boolean wednessday;
private boolean thursday;
private boolean friday;
private boolean saturday;
private boolean sunday;
public Histogram() {
}
public Histogram(int employees, double startTime, double endTime, boolean monday, boolean tuesday,
boolean wednessday, boolean thursday, boolean friday, boolean saturday, boolean sunday) {
this.employees = employees;
this.startTime = startTime;
this.endTime = endTime;
this.monday = monday;
this.tuesday = tuesday;
this.wednessday = wednessday;
this.thursday = thursday;
this.friday = friday;
this.saturday = saturday;
this.sunday = sunday;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
//@Size(min = 2, max = 255, message = "Enter between 2 and 255 characters!")
@Column(name = "employees")
@NotNull
public int getEmployees() {
return employees;
}
public void setEmployees(int employees) {
this.employees = employees;
}
@Column(name = "startTime")
@NotNull
public double getStartTime() {
return startTime;
}
public void setStartTime(double startTime) {
this.startTime = startTime;
}
@Column(name = "endTime")
@NotNull
public double getEndTime() {
return endTime;
}
public void setEndTime(double endTime) {
this.endTime = endTime;
}
@Column(name = "monday")
@NotNull
public boolean isMonday() {
return monday;
}
public void setMonday(boolean monday) {
this.monday = monday;
}
@Column(name = "tuesday")
@NotNull
public boolean isTuesday() {
return tuesday;
}
public void setTuesday(boolean tuesday) {
this.tuesday = tuesday;
}
@Column(name = "wednessday")
@NotNull
public boolean isWednessday() {
return wednessday;
}
public void setWednessday(boolean wednessday) {
this.wednessday = wednessday;
}
@Column(name = "thursday")
@NotNull
public boolean isThursday() {
return thursday;
}
public void setThursday(boolean thursday) {
this.thursday = thursday;
}
@Column(name = "friday")
@NotNull
public boolean isFriday() {
return friday;
}
public void setFriday(boolean friday) {
this.friday = friday;
}
@Column(name = "saturday")
@NotNull
public boolean isSaturday() {
return saturday;
}
public void setSaturday(boolean saturday) {
this.saturday = saturday;
}
@Column(name = "sunday")
@NotNull
public boolean isSunday() {
return sunday;
}
public void setSunday(boolean sunday) {
this.sunday = sunday;
}
}
请有人指导我如何解决这个问题。
答案 0 :(得分:0)
如果要更新特定的现有行,那么首先有两个使用findById(someId)等函数获取行。 然后调用它上面的update方法。你正在做的是插入数据库而不检查是否已经存在需要更新的行。