如何使用EntityManager从db获取ID?

时间:2017-09-11 09:07:31

标签: java spring hibernate jpa entitymanager

使用实体管理器如何从其中一个列值为true的数据库中获取行的ID,然后更新该行。我的数据库结构是这样的:

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

如何获取 MONDAY: FALSE 的行的 ID ,其余时间为 TRUE 获得 ID 后更新该行。

在sql中获取ID,它会像这样:

SELECT id 
FROM histogram 
WHERE monday = true 
AND tueday = false 
AND wednessday = false 
AND thursday = false 
AND friday = false 
AND saturday = false 
AND sunday = false ;

这是我的模特课:

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;
    }

}

这是我的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);
 }
 }
}

这是我的服务类:

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);
 }
}

1 个答案:

答案 0 :(得分:0)

由于您熟悉wih sql,您可以使用jpql查找具有指定条件的项目。

TypedQuery<Long> 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 ");

List<Long> histograms = query.getResultList();

然后使用检索到的id来获取直方图实体并应用更改

Histogram histogram = entityManager.find(Histogram.class,histogramId);
//changes on the entity
...
entityManager.persist(histogram);

另一种选择是使用标准api。