Home>

There are two classes:

@Builder
@AllArgsConstructor
@Entity
@Data
@Table (name= "A")
public class A {
    @Id
    @GeneratedValue
    @Column (name= "id", length= 16, unique= true, nullable= false)
    private UUID id;
    @Column (unique= true)
    private String name;
}
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
@Table (name= "B")
public class B {
    @Id
    @GeneratedValue
    @Column (name= "id", length= 16, unique= true, nullable= false)
    private UUID id;
    @ManyToOne (fetch= FetchType.LAZY)
    @JoinColumn (name= "a_Id", nullable= false)
    private A a;
}

An interface has been created for database queries

public interface B extends JpaRepository <
B, UUID >
 {
}

We need a request something like:

@Query ("SELECT b FROM B b WHERE b.a_Id=: a")
    Set <
B >
 findA (@Param ("a") A a);
or
@Query ("SELECT b FROM B b WHERE b.a_Id=: a")
    Set <
B >
 findA (@Param ("a") UUID a);

both don't work. Is it possible to somehow implement such a selection by a nested class?

create OneToMany in the first entity to the second entity. There will be an equivalent to your request

ArchDemon2021-12-01 20:30:21

also an option, thanks! there is still such a way and it works Set findByA_id (UUID id); Is it possible to display not a list of classes B, but for example a list B.a.name -a list of names of class A? (without changing class A)

Alin2021-12-01 20:32:46