Spring-Data: Generated values effects updating entity instead of insert
<p>There is a behaviour I dont understand.</p>

<p>I have to entities like:</p>

<pre><code>@Entity
@EqualsAndHashCode(of={"name"})
@ToString(of={"name", "entityB"})
public class EntityA {

    @Id
    @GeneratedValue
    @Getter
    @Setter
    private Long id;

    @Getter
    @Setter
    @Column(nullable=false, unique=true)
    private String name;

    @Getter
    @Setter
    @ManyToOne(fetch=EAGER, cascade={PERSIST, MERGE})
    private EntityB entityB;

}

@ToString(of = { "name" })
@EqualsAndHashCode(of = { "name" })
@Entity
class EntityB {

    @Id
    @GeneratedValue
    @Getter
    @Setter
    private Long id;

    @Getter
    @Setter
    @XmlAttribute
    @Column(nullable=false, unique=true)
    private String name;
}
</code></pre>

<p>And a logic to insert data to db:</p>

<pre><code>@Component
public class DatabaseInitializer implements InitializingBean {
    @Autowired EntityARepository repository; // Spring-Data CrudRepository!

    @Override
    public void afterPropertiesSet() throws Exception {
        final Set&lt;EntityA&gt; aEntities = createAEntities();
        repository.save(aEntities);

        Iterator&lt;EntityA&gt; iterator = repository.findAll().iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    private  Set&lt;EntityA&gt; createAEntities() throws Exception {
        Set&lt;EntityA&gt; aEntities = new HashSet&lt;&gt;();
        aEntities.add(getFirstEntityA());
        aEntities.add(getSecondEntityA());
        return aEntities;
    }

    private EntityA getFirstEntityA(){
        EntityA a = new EntityA();
        a.setId(1L);
        a.setName("a-1");
        a.setEntityB(getFirstEntityB());
        return a;
    }   

    private EntityA getSecondEntityA(){
        EntityA a = new EntityA();
        a.setId(2L);
        a.setName("a-2");
        a.setEntityB(getFirstEntityB());
        return a;
    }

    //

    private EntityB getFirstEntityB() {
        EntityB b = new EntityB();
        b.setId(1l);
        b.setName("b-1");
        return b;
    }

}
</code></pre>

<p>When starting application, I get following outputs:</p>

<pre><code>org.hibernate.SQL: select entitya0_.id as id1_0_1_, entitya0_.entityb_id as entityb_3_0_1_, entitya0_.name as name2_0_1_, entityb1_.id as id1_1_0_, entityb1_.name as name2_1_0_ from entitya entitya0_ left outer join entityb entityb1_ on entitya0_.entityb_id=entityb1_.id where entitya0_.id=?
org.hibernate.SQL: select entityb0_.id as id1_1_0_, entityb0_.name as name2_1_0_ from entityb entityb0_ where entityb0_.id=?
org.hibernate.SQL: insert into entityb (id, name) values (default, ?)
org.hibernate.SQL: insert into entitya (id, entityb_id, name) values (default, ?, ?)
org.hibernate.SQL: update entitya set entityb_id=?, name=? where id=?

EntityA(name=a-1, entityB=EntityB(name=b-1))
</code></pre>

<p>As you can see, it updates entitya instead of adding a new row to db.</p>

<p>When I remove the <code>@GeneratedValue</code> from both entities it works.</p>

<pre><code>org.hibernate.SQL: select entitya0_.id as id1_0_1_, entitya0_.entityb_id as entityb_3_0_1_, entitya0_.name as name2_0_1_, entityb1_.id as id1_1_0_, entityb1_.name as name2_1_0_ from entitya entitya0_ left outer join entityb entityb1_ on entitya0_.entityb_id=entityb1_.id where entitya0_.id=?
org.hibernate.SQL: select entityb0_.id as id1_1_0_, entityb0_.name as name2_1_0_ from entityb entityb0_ where entityb0_.id=?
org.hibernate.SQL: select entitya0_.id as id1_0_1_, entitya0_.entityb_id as entityb_3_0_1_, entitya0_.name as name2_0_1_, entityb1_.id as id1_1_0_, entityb1_.name as name2_1_0_ from entitya entitya0_ left outer join entityb entityb1_ on entitya0_.entityb_id=entityb1_.id where entitya0_.id=?
org.hibernate.SQL: insert into entityb (name, id) values (?, ?)
org.hibernate.SQL: insert into entitya (entityb_id, name, id) values (?, ?, ?)
org.hibernate.SQL: insert into entitya (entityb_id, name, id) values (?, ?, ?)

EntityA(name=a-1, entityB=EntityB(name=b-1))
EntityA(name=a-2, entityB=EntityB(name=b-1))
</code></pre>

<p>When I want to use ID-Generator and remove setId(...) from entity-creator, I get a <code>NullPointerException</code> in <code>HsqlException</code>.</p>

<pre><code>    Caused by: org.hsqldb.HsqlException: java.lang.NullPointerException
    at org.hsqldb.error.Error.error(Unknown Source) ~[hsqldb-2.3.3.jar:2.3.3]
    at org.hsqldb.persist.RowStoreAVL.indexRow(Unknown Source) ~[hsqldb-2.3.3.jar:2.3.3]
    at org.hsqldb.TransactionManager2PL.addInsertAction(Unknown Source) ~[hsqldb-2.3.3.jar:2.3.3]
    at org.hsqldb.Session.addInsertAction(Unknown Source) ~[hsqldb-2.3.3.jar:2.3.3]
    at org.hsqldb.Table.insertSingleRow(Unknown Source) ~[hsqldb-2.3.3.jar:2.3.3]
    at org.hsqldb.StatementDML.insertRowSet(Unknown Source) ~[hsqldb-2.3.3.jar:2.3.3]
    at org.hsqldb.StatementInsert.getResult(Unknown Source) ~[hsqldb-2.3.3.jar:2.3.3]
    at org.hsqldb.StatementDMQL.execute(Unknown Source) ~[hsqldb-2.3.3.jar:2.3.3]
    at org.hsqldb.Session.executeCompiledStatement(Unknown Source) ~[hsqldb-2.3.3.jar:2.3.3]
    at org.hsqldb.Session.execute(Unknown Source) ~[hsqldb-2.3.3.jar:2.3.3]
    ... 87 common frames omitted
Caused by: java.lang.NullPointerException: null
    at org.hsqldb.index.IndexAVLMemory.insert(Unknown Source) ~[hsqldb-2.3.3.jar:2.3.3]
    ... 96 common frames omitted
</code></pre>

<p>At least what I want is that I don`t need to give entityA a identifier, it should generate by it self and I want at least two entitiesA.</p>