Spring Data JPA - 获取自定义本机查询的非实体列的值

时间:2016-06-01 13:00:42

标签: java hibernate spring-mvc jpa spring-data-jpa

我正在使用Spring Boot / MVC。 我有一个使用JpaRepository的自定义查询:

 public interface WorkOrderRepository extends JpaRepository<WorkOrder, Integer> {
 @Query(value = "SELECT * FROM (SELECT * FROM workorder) Sub1 INNER JOIN (SELECT wo_number, GROUP_CONCAT(service_type SEPARATOR ', ') AS 'service_types' FROM service_type GROUP BY wo_number) Sub2 ON Sub1.wo_number=Sub2.wo_number WHERE fleet_company_id=?1 AND (order_status='On-Bidding' OR order_status='Draft')", nativeQuery = true)
 Collection<WorkOrder> findWorkOrdersByFleet(Long fleetCompanyID);
 }

返回下表:

http://imgur.com/Ylkc6U0

正如您所看到的,它具有由Concat导致的service_types列,它不是实体类的一部分。我的问题是如何获得该列的值。有人说我可以使用单独的DTO来映射service_types列吗?或者我可以使用&#39; new&#39;关键词?也许你还有其他人对我有所帮助。我还尝试制作一个瞬态列service_types,但它没有用。

这是我的实体类:

@Entity
@Table(name="workorder")
public class WorkOrder {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="wo_number")
private Long woNumber;

@ManyToOne(optional=false, cascade=CascadeType.ALL)
@JoinColumn(name = "vehicle_id")
private Vehicle vehicle;

@ManyToOne(optional=false, cascade=CascadeType.ALL)
@JoinColumn(name = "fleet_company_id")
private FleetCompany fleetCompany;

@Column(name="order_title")
private String orderTitle;

@Column(name="order_date")
private String orderDate;

@Column(name="order_time")
private String orderTime;

@Column(name="order_status")
private String orderStatus;

@Column(name="ref_number")
private String refNumber;

@Column(name="proposals")
private int proposals;


//@Column(name="serviceTypes")
@Transient
private int serviceTypes;

public WorkOrder() {
    super();
}


public Long getWoNumber() {
    return woNumber;
}


public void setWoNumber(Long woNumber) {
    this.woNumber = woNumber;
}


public String getOrderTitle() {
    return orderTitle;
}


public void setOrderTitle(String orderTitle) {
    this.orderTitle = orderTitle;
}


public String getOrderDate() {
    return orderDate;
}


public void setOrderDate(String orderDate) {
    this.orderDate = orderDate;
}


public String getOrderTime() {
    return orderTime;
}


public void setOrderTime(String orderTime) {
    this.orderTime = orderTime;
}


public String getOrderStatus() {
    return orderStatus;
}


public void setOrderStatus(String orderStatus) {
    this.orderStatus = orderStatus;
}


public String getRefNumber() {
    return refNumber;
}


public void setRefNumber(String refNumber) {
    this.refNumber = refNumber;
}


public int getProposals() {
    return proposals;
}


public void setProposals(int proposals) {
    this.proposals = proposals;
}


public Vehicle getVehicle() {
    return vehicle;
}


public void setVehicle(Vehicle vehicle) {
    this.vehicle = vehicle;
}


public FleetCompany getFleetCompany() {
    return fleetCompany;
}


public void setFleetCompany(FleetCompany fleetCompany) {
    this.fleetCompany = fleetCompany;
}


public int getServiceTypes() {
    return serviceTypes;
}


public void setServiceTypes(int serviceTypes) {
    this.serviceTypes = serviceTypes;
}


}

有些人告诉我要做DTO:

public class WorkOrderDTO extends WorkOrder {

private String service_types;

public WorkOrderDTO() {
    super();
}

public WorkOrderDTO(String service_types) {
    this.service_types = service_types;
}

public String getService_types() {
    return service_types;
}

public void setService_types(String service_types) {
    this.service_types = service_types;
}

}

并添加使存储库从WorkOrder替换为WorkOrderDTO。

 public interface WorkOrderRepository extends JpaRepository<WorkOrderDTO, Integer>

但是当我这样做时,我有自动装配问题。

1 个答案:

答案 0 :(得分:0)

我解决了自己的问题,终于!!! 我使用了@SqlResultMapping

 SqlResultSetMapping(
    name="workorder",
    classes={
        @ConstructorResult(
            targetClass=WorkOrderDTO.class,
            columns={
                @ColumnResult(name="wo_number", type = Long.class),
                @ColumnResult(name="service_types", type = String.class),
                @ColumnResult(name="order_title", type = String.class)
            }
        )
    }
)

我创建了一个新的POJO,它不是名为WorkOrderDTO的实体。

@PersistenceContext
private EntityManager em;
@Override
public Collection<WorkOrderDTO> getWork() {
    Query query = em.createNativeQuery(
              "SELECT Sub1.wo_number, Sub2.service_types, Sub1.order_title FROM (SELECT * FROM workorder) Sub1 INNER JOIN (SELECT wo_number, GROUP_CONCAT(service_type SEPARATOR ', ') AS 'service_types' FROM service_type GROUP BY wo_number) Sub2 ON Sub1.wo_number=Sub2.wo_number WHERE fleet_company_id=4 AND (order_status='On-Bidding' OR order_status='Draft')", "workorder");
    @SuppressWarnings("unchecked")
    Collection<WorkOrderDTO> dto = query.getResultList();

    Iterable<WorkOrderDTO> itr = dto;
    return (Collection<WorkOrderDTO>)itr;
}

最后,那些讨厌发布相同问题的用户不会再烦恼了。