Spring JPA to store entity within entity
<p>I am using Spring data JPA with hibernate at the back with H2 in memory database. </p>

<p>I have following entities:</p>

<pre><code>@Entity
public class EntityA {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long entityAId;

    @Column
    @NotEmpty
    private String name;

    @OneToMany(mappedBy = "entityA")
    private List&lt;EntityB&gt; entityBList;

    // getters and setters
}

@Entity
public class EntityB {

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

    @Column
    @NotEmpty
    private String name;

    @ManyToOne
    @JoinColumn(referencedColumnName = "entityAId")
    private EntityA entityA;

    // getters and setters
}
</code></pre>

<p>Here are my repositories:</p>

<pre><code>@Repository
public interface EntityARepository extends JpaRepository&lt;EntityA, Long&gt; {

}

@Repository
public interface EntityBRepository extends JpaRepository&lt;EntityB, Integer&gt; {
    @Query(value = "FROM EntityB b where b.entityA.entityAId = ?1")
    public List&lt;EntityB&gt; getEntityBByEntityAId(Long entityAId);
}
</code></pre>

<p>My question is:</p>

<p>If i have an instance of <code>entityA</code> with 'n' instances of <code>entityB</code> in it, and if i call <code>entityARepository.save(entityA);</code> then will it save entityB as well (is it possible?)? I tried this but it didn't worked for me. I did tried doing</p>

<pre><code>entityARepository.save(entityA);
entityBRepository.save(entityBList);
</code></pre>

<p>Which saved the result. But when i retrieved <code>entityA</code> using <code>findOne</code> method from repository by an <code>entityAId</code>, i get an empty list without any instance of <code>EntityB</code> type. </p>

<p>I even tried querying <code>entityBRepository</code> separately by <code>entityAId</code>, but i get an empty <code>entityB</code> result list. </p>

<p>Can somebody please help me to know what exactly am missing?</p>