【问题标题】:How to get ID from db using EntityManager?如何使用 EntityManager 从 db 获取 ID?
【发布时间】:2017-09-11 09:07:30
【问题描述】:

使用实体管理器 如何从数据库中获取其中一列值为真的行的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 且其余日期为 TRUE 的行的 ID并在获得 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);
 }
}

【问题讨论】:

    标签: java spring hibernate jpa entitymanager


    【解决方案1】:

    由于您熟悉 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。

    【讨论】:

      猜你喜欢
      • 2017-11-03
      • 2019-10-10
      • 2020-01-11
      • 2014-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-16
      相关资源
      最近更新 更多