java - Duplicate rows when save() using JPA -
basically i'm creating gui using javafx alongside spring , hibernate. problem related jpa
suppose first time user inserts data in db (column monday true , other columns 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
my problem second time when inserting data db, duplicates first 2 rows, not want.
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
what want this:
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
or solution can deleting rows of monday column true , recreate previously, auto-generated ids new:
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
this dao class:
@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 histogram h h.monday = true , h.tuesday = false , h.wednessday = false , h.thursday = false , h.friday = false , h.saturday = false , h.sunday= false"); return query.getresultlist(); } public void updatebesoinrequestbymonday(long id) { histogram histogram = getbesoinrequestbyid(id); if (histogram != null) { entitymanager.merge(histogram); } } }
this service class:
@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); } }
this how using on button click (as can see in commented code i've tried different tricks none of them working):
iterator = 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 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++;
update: histogram entity:
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 , 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; } }
please guide me how resolve this.
if want update particular existing rows, first have 2 rows using functions findbyid(someid). call update method on it. doing inserting db without checking if row need updated exists.
Comments
Post a Comment