Spring Data JPA查询内部联接表抛出错误

时间:2019-11-13 19:44:35

标签: sql hibernate spring-boot

Spring DATA JPA问题...我正在尝试编写查询以访问我的sql联接表中的数据。

我的联接表设置如下:

@Entity
@Table(name="WritingCompany")
public class WritingCompany {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "companyId")
    private Long id;
//    private String companyName;

    @ManyToMany
    @JoinTable(name = "Join-WritingCo-Carrier",
            joinColumns = @JoinColumn(name="writingCo"),
            inverseJoinColumns = @JoinColumn(name="carrier")
    )
    private Set<CarrierAppointment> carriers;


//...getters and setters

}

 public class CarrierAppointment {
   
 //  ...
 
   @ManyToMany(
            cascade=CascadeType.ALL,
            mappedBy = "companyName"
    )
    private Set<WritingCompany> companies;

    public Set<WritingCompany> getCompanies() {
        return companies;
    }

    public void setCompanies(Set<WritingCompany> companies) {
        this.companies = companies;
    }

//...

}

我不确定我需要为哪个类库编写查询...我试图从连接表中查找所有具有相同载体ID且与一个特定载体匹配的WritingCo名称。

Join Table是通过写作公司设置的,但是我觉得应该通过CarrierAppointment存储库访问它,因为我与运营商ID相匹配。

这是我在CarrierAppointment存储库中尝试过的操作,它引发了此错误(意外的令牌:在第1行第94列附近加入):

    @Query("Select companyName FROM WritingCompany INNER JOIN CarrierAppointment ON Join-WritingCo-Carrier.carrier= CarrierAppointment.CarrierAppointmentId")
    List<CarrierAppointment> findCarrierAppoinmentFromJoin(CarrierAppointment carrier);
我也尝试过:

    @Query("SELECT writingCo FROM Join-WritingCo-Carrier WHERE carrier= CarrierAppointment.CarrierAppointmentId")
    List<CarrierAppointment> findCarrierAppoinmentFromJoin(CarrierAppointment carrier);

然后我在WritingCompanyRepository中尝试了此操作,它也引发了类似的错误:

    @Query("Select companyName FROM WritingCompany INNER JOIN CarrierAppointment ON Join-WritingCo-Carrier.carrier= CarrierAppointment.CarrierAppointmentId")
    List<WritingCompany> findAllWithDescriptionQuery(CarrierAppointment carrier);
我很难理解这个查询在说什么。我是否曾经需要访问sql连接表中的列,还是只是通过询问组成连接表中连接列的每个类来查询连接表?在INNER JOIN声明之后,该语句的正确部分是什么?有人可以提供查询原因的更深层解释,以便我弄清楚为什么它不起作用吗?我一直在阅读很多内部联接示例,但似乎无法弄清楚。

0 个答案:

没有答案