我试图使用Hibernate和JPA在我的项目中创建一个Left Outer联接。但是,在我设置了所有过程并编写了要加入的代码之后,它将引发以下错误:

Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.model.AVSApplication.avsMapped


我知道它在告诉我什么,但是我尝试了一种解决方法,但仍然会继续给我同样的错误。我不认为这是数据库方面的问题,那么我认为这与控制器或AVSApplicationn.classAVSMapped.class的Entity部分有关。

AppRepository:

@Repository
public interface AppRepository extends JpaRepository<AVSMapped, String>{

    @Query("SELECT avs.appcode, avs.acro, avs.appname, mapped.gpStatus "
            + "FROM avs LEFT JOIN mapped ON avs.appcode = mapped.appcode")
    List<ApplicationDTO> fetchAppDataLeftJoin();

}


JoinQueryService:

public class JoinQueryService {

    @Resource
    private AppRepository appRepository;


    public List<ApplicationDTO> getAppLeftJoin(){
        List<ApplicationDTO> list = appRepository.fetchAppDataLeftJoin();
        list.forEach(l-> System.out.println(l));
        return list;
    }

}


控制器:

@org.springframework.stereotype.Controller
public class AVSController {

        @Autowired
        private JoinQueryService joinQueryService;



      @GetMapping("/dept")
      public ResponseEntity<List<ApplicationDTO>> getAppLeftJoin(){
          return new ResponseEntity<List<ApplicationDTO>>(joinQueryService.getAppLeftJoin(), HttpStatus.OK);
      }



AVSApplication:


@Entity
@Table(name = "avs")
public class AVSApplication {

    @Id
    @Column(name = "appcode")
    private String appcode;

    @Column(name = "acro")
    private String acro;

    @Column(name = "appname")
    private String appname;


    @ManyToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "mGpStatus", insertable = false, updatable = false)
    @Fetch(FetchMode.JOIN)
    private AVSMapped avsMapped;



AVSMapped:

@Entity
@Table(name = "mapped")
public class AVSMapped  {

    @Id
    @Column(name = "mAppcode")
    private String mAppcode;

    @Column(name = "mAcro")
    private String mAcro;

    @Column(name = "mAppname")
    private String mAppname;

    @Column(name = "mGpStatus")
    private String mGpStatus;

    @OneToMany(targetEntity = AVSApplication.class, mappedBy ="appcode", orphanRemoval = false, fetch = FetchType.LAZY)
    private Set<AVSApplication> avsApplication;



应用DTO:

public class ApplicationDTO {


    private String dtoAppCode;
    private String dtoAcro;
    private String dtoAppName;
    private String dtoGpStatus;

    //Constructor
    public ApplicationDTO(String dtoAppCode, String dtoAcro, String dtoAppName, String dtoGpStatus) {
        super();
        this.dtoAppCode = dtoAppCode;
        this.dtoAcro = dtoAcro;
        this.dtoAppName = dtoAppName;
        this.dtoGpStatus = dtoGpStatus;
        //this.dtoLastUpdate = dtoLastUpdate;
    }


更新的错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field joinQueryService in com.controller.AVSController required a bean of type 'com.model.JoinQueryService' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.model.JoinQueryService' in your configuration.

最佳答案

我可以在摘要中看到问题的数量。

一个实体类中的@ManyToMany映射不正确。应该是@ManyToOne

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "mGpStatus", insertable = false, updatable = false)
@Fetch(FetchMode.JOIN)
private AVSMapped avsMapped;


除非您具有JoinQueryService配置,否则无法在控制器中自动连接@Bean。用构造型注解为您的课程注解

@Service
public class JoinQueryService {
     ...
}


JPA存储库中,使用Entity类更改泛型类型

@Query("SELECT avs.appcode, avs.acro, avs.appname, mapped.gpStatus "
        + "FROM avs LEFT JOIN mapped ON avs.appcode = mapped.appcode")
List<AVSMapped> fetchAppDataLeftJoin();

10-05 20:43
查看更多