Spring Boot自动装配问题:不是托管类型

时间:2016-05-31 13:39:53

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

我有一个存储库类:

public interface WorkOrderRepository extends JpaRepository<WorkOrderDTO, 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')")
Collection<WorkOrderDTO> findWorkOrdersByFleet(Long fleetCompanyID);

@Query(value = "SELECT * FROM workorder WHERE fleet_company_id=?1")
Collection<WorkOrderDTO> findWorkOrdersByFleet1(Long fleetCompanyID);
}

实体类:

@Entity
@Table(name="workorder")
public class WorkOrder implements Serializable {
@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;

@Transient
private String 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 String getServiceTypes() {
    return serviceTypes;
}


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



}

我有一个扩展实体类的pojo:

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


}

我想将POJO WorkOrderDTO传递给JpaRepository而不是实体,以便映射列 service_types ,而不是实体类的一部分。但是当我设置WorkOrderDTO而不是WorkOrder时,我遇到了自动装配问题。也许,这是一些注释问题。我没有给POJO注释任何注释。

1 个答案:

答案 0 :(得分:0)

您可以使用&#34; new&#34;操作员。您必须在WorkOrderDTO中使用所需的值创建构造函数,例如

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

然后你可以在jpql中使用它 - 查询:

@Query(value = "SELECT new your.package.WorkorderDTO(w.<select servicetypes somehow>) FROM workorder w WHERE fleet_company_id=?1")

然而,我发现你的第一个查询令人困惑,我认为它应该是一个原生查询......在那里你不能使用&#34; new&#34;操作

也许您可以将ServiceType(如Vehicle或FleetCompany)映射为List?然后你可以只为你的DTO连接列表中的值。

编辑:您可以使用@OneToMany来映射List,因为它可能在您的WorkOrder的Vehicle类中,只是为了澄清我之前的段落。