java repository query findall()给定另一个类的id(外键)

iyzzxitl  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(280)

我正在使用一个名为publicationrepository的jparepository,希望找到某个人的所有出版物。这些类通过类作者连接。
人员类别:

@Entity
public class Person {

        @Id
        private String email;

        private String telefon;

        private String password;

        @OneToMany(mappedBy = "person")
        Set<Author> Author;
    }

作者类别:

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Author {

    @Id
    private int id;

    @ManyToOne
    @JoinColumn(name="Person_ID")
    Person person;

    @ManyToOne
    @JoinColumn(name="Publication_ID")
    Publication publication;

    private String Date;

    private String Writerstatus;
}

出版物类

@Entity
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Publication {
    @Id
    private int id;

    private String publicationname;

    @OneToMany(mappedBy = "publication")
    Set<Author> author;
}

以及publicationrepository

public interface ProjektRepository extends JpaRepository<Projekt,Integer> {
}
aiazj4mn

aiazj4mn1#

public interface PublicationRepository extends JpaRepository<Publication,Integer> {
      @Query(value = "SELECT pub.* FROM author as auth INNER JOIN publications as pub ON auth.publication_id = pub.id WHERE auth.person_id = ?1", native = true)
      List<Publication> findAllPublicationsOfThisPerson(int personId);
}

试试这个。
我还建议用实体的表名对实体进行注解: @Table(name = "publication") 对于多对多关系,可以使用手动生成表 Author 您还可以使用 @ManyToMany 注解。
好的教程:https://attacomsian.com/blog/spring-data-jpa-many-to-many-mapping

相关问题