Q:

Spring jpa @OneToMany one direction always null

@Data
@Entity
@Table(name = "member")
public class MemberEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @OneToMany
    @JoinColumn(name = "memberId")
    private List<PhoneEntity> phoneEntities;
}

@Data
@Entity
@Table(name = "phone")
public class PhoneEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column
    private Integer memberId;
}

@Repository
public interface MemberRepository extends JpaRepository<MemberEntity, Integer> {

    @Query("select m from MemberEntity m join fetch m.phoneEntities where m.id=?1")
    MemberEntity findByIdWithPhone(Integer id);
}

create OneToMany relationship from Member to Phone
im trying to select Member fetch join with Phone using @Query("select m from MemberEntity m join fetch m.phoneEntities where m.id=?1")
 Query
this query created like under my-sql query
SELECT 
memberenti0_.id as id1_0_0_,
phoneentit1_.id as id1_1_1_,
phoneentit1_.member_id as member_i2_1_1_,
phoneentit1_.member_id as member_i2_1_0__,
phoneentit1_.id as id1_1_0__ 
from member memberenti0_
inner join phone phoneentit1_ on memberenti0_.id=phoneentit1_.member_id
where memberenti0_.id=?

and result MemberEntity is not null(this mean insert and inner join is work!) but MemberEntity#phoneEntities is always null
im trying to change List<PhoneEntity> Collections<PhoneEntity> Set<PhoneEntity> all collection type doesn't work.
I doubt column in Phone table call twice in the generated query(member_i2_1_1_, member_i2_1_0__), but I do not know why.

A:

You have to correct your mapping as:
@Data
@Entity
@Table(name = "member")
public class MemberEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @OneToMany(
        mappedBy = "member"
    )        
    private List<PhoneEntity> phoneEntities;
}

@Data
@Entity
@Table(name = "phone")
public class PhoneEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @ManyToOne(fetch = FetchType.LAZY) // Or Eager 
    @JoinColumn(name = "member_id")
    private MemberEntity member;
}

And there no need to specify join in the query :
@Query("select m from MemberEntity m where m.id=?1")

