Entity2 와 Entity3를 List로 받으려고 시도 했을 때 발생한 오류 - SET 바꿔서 해결(?)
양방향, 단방향, 1:N, N:1, N:M 항상 신경써야함..
Entity 1
@Getter @Setter
@Entity(name = "a_table")
public class ATable {
@OneToMany
@JoinColumn(name = "user_id")
private Set<B> b;
@OneToMany
@JoinColumn(name="user_id")
private Set<C> c;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id", updatable = false, nullable = false, insertable=false)
private Long id;
@Column(name="user_id")
private Long userId;
}
Entity 2
@Getter @Setter
@Entity(name = "b_table")
public class BTable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id", updatable = false, nullable = false, insertable=false)
private Long id;
@Column(name="user_id")
private Long userId;
}
Entity 3
@Getter @Setter
@Entity(name = "c_table")
public class CTable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id", updatable = false, nullable = false, insertable=false)
private Long id;
@Column(name="user_id")
private Long userId;
}
ATableRepository
@Repository
@Component
public interface ATableRepository extends JpaRepository<ATable, Long>{
@EntityGraph(attributePaths={"b", "c"})
@Query(value = "SELECT a FROM a_table AS c
LEFT JOIN FETCH b_table b ON a.userId = b.userId
LEFT JOIN FETCH c_table c ON a.userId = c.userId
GROUP BY a.userId")
List<ATable> findAll(); //이 부분은 적당히 수정
}
'JAVA > JAVA__Interface-JPA' 카테고리의 다른 글
에러 모음 (0) | 2021.03.22 |
---|---|
JPA에서 1:N JOIN 하기 (0) | 2021.03.18 |