简体   繁体   English

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

[英]Spring Boot Autowiring Problems: Not An Managed Type

I have a repository class: 我有一个存储库类:

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

And an entity class: 和实体类:

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



}

and I have a pojo that extends the entity class: 我有一个扩展实体类的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;
}


}

I want to pass the POJO WorkOrderDTO to the JpaRepository instead of the entity for it to map column service_types which is not part of the entity class. 我想将POJO WorkOrderDTO传递给JpaRepository而不是实体,以便映射不属于实体类的列service_types But I have autowiring problems when I set WorkOrderDTO instead of WorkOrder . 但是当我设置WorkOrderDTO而不是WorkOrder时,我遇到了自动装配问题。 Maybe, it is some annotations problem. 也许,这是一些注释问题。 I didn't put any annotations to the POJO. 我没有给POJO添加任何注释。

You could use the "new" Operator. 您可以使用“新”运算符。 You must create a constructor in WorkOrderDTO with the values you need, eg 您必须在WorkOrderDTO中使用所需的值创建构造函数,例如

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

then you can use it like that in a jpql - query: 那么你可以在jpql中使用它 - 查询:

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

However, I find your first query confusing, I think it is supposed to be a native query... There you can't use the "new" operator. 但是,我发现你的第一个查询令人困惑,我认为它应该是一个原生查询...在那里你不能使用“新”运算符。

Maybe it is possible for you to map the ServiceType like Vehicle or FleetCompany as a List? 也许您可以将ServiceType(如Vehicle或FleetCompany)映射为List? Then you could concatenate just the values in the List for your DTO. 然后你可以只为你的DTO连接列表中的值。

EDIT: You could use @OneToMany to map a List, as it is probably in your Vehicle class for WorkOrder, just to clarify my previous paragraph. 编辑:您可以使用@OneToMany来映射List,因为它可能在您的WorkOrder的Vehicle类中,只是为了澄清我的上一段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自动装配失败:不是托管类型 - Autowiring fails: Not an managed Type 在 Spring Boot 中自动装配枚举 - Autowiring Enums in Spring boot 自动装配到非Spring托管类(POJO)中时发生NullPointerException-Spring Boot - NullPointerException when autowiring into non-spring managed class (POJO) - Spring Boot Spring Boot中的继承:@Autowired字段为空(需要在非Spring托管类中自动装配 - 多个实例) - Inheritance in Spring Boot: @Autowired fields are null (need autowiring in non spring managed class - multiple instances) Spring Boot:在自动装配具体类时没有“找到类型的限定bean” - Spring Boot: “No qualifying bean of type… found” when autowiring concrete class Spring Boot,JPA。 存储库不是托管类型 - Spring boot, JPA. Repository not a managed type 带有外部 jar 的 Spring Boot &#39;Not a Managed Type&#39; - Spring boot with external jar 'Not a Managed Type' Spring Boot 应用程序中的“IllegalArgumentException:不是托管类型” - "IllegalArgumentException: Not a managed type" in Spring Boot application 不是托管类型。 Spring Boot 测试 - Not a managed type. Spring Boot Tests Spring Boot - 不是托管类型,创建 bean 时出错 - Spring Boot - Not a managed type, Error creating bean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM