java - How to get ID from db using EntityManager? -
using entity manager how id of row database 1 of column value true , update row. db structure 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 how id of row has monday: false , rest of days true , after getting id update row.
in sql id similar this:
select id histogram monday = true , tueday = false , wednessday = false , thursday = false , friday = false , saturday = false , sunday = false ; this model class:
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; } } this dao:
import javax.persistence.entitymanager; import javax.persistence.persistencecontext; import javax.persistence.criteria.criteriabuilder; import javax.persistence.criteria.criteriaquery; import javax.persistence.criteria.parameterexpression; import javax.persistence.criteria.root; import org.springframework.stereotype.repository; /** * @author junaid khalid * */ @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); } } } this service class:
import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.component; import org.springframework.transaction.annotation.transactional; /** * @author junaid khalid * */ @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); } }
since familiar wih sql can use jpql find items criteria specified.
typedquery<long> 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 "); list<long> histograms = query.getresultlist(); then use id retrieved histogram entity , apply changes
histogram histogram = entitymanager.find(histogram.class,histogramid); //changes on entity ... entitymanager.persist(histogram); the other choice use criteria api.
Comments
Post a Comment