I am using below mapping
@Entity
@Table(name = "test")
public class TestOrder implements Serializable {
    @Id
    @Column(name = "orderid", updatable = false)
    protected Long orderId;
    @OneToMany(cascade = {CascadeType.ALL})
    @JoinColumn(name = "order_id_fk")
    private List<TestDetails> details;
//getters and setters
}
@Entity
@Table(name="test_details")
public class TestDetails implements Serializable {
    @Id
    //Generator
    @Column(name = "id", updatable = false, insertable = false)
    protected Long id;
    @Column(name="order_id_fk", updatable = false)
    private Long orderId;
//getters and setters
}
When I update/insert the data, it's trying to update the order_id_fk to null
SQL [update test_details set order_id_fk'='null where order_id_fk'='? and id'='?]; constraint [null];
Any help is much appreciated.
Updating/Inserting using Spring Integration
<int-jpa:updating-outbound-gateway entity-class="com.aaaa.TestOrder" entity-manager-factory="myEntityManagerFactory" persist-mode="MERGE">
    <int-jpa:transactional propagation="REQUIRED" transaction-manager="myTransactionManager" />
</int-jpa:updating-outbound-gateway>
                You need to fetch values of TestDetails entity eagerly.
Just modify in your annotation with,
@OneToMany(fetch = FetchType.EAGER, mappedBy="testOrder", cascade=CascadeType.ALL)
Hope this will work.
I ran your code and it works correctly (when added @GeneratedValue(strategy = GenerationType.AUTO) to both IDs).
em.getTransaction().begin();
TestOrder to = new TestOrder();
TestDetails td1 = new TestDetails();
TestDetails td2 = new TestDetails();
TestDetails td3 = new TestDetails();
to.setDetails(Arrays.asList(new TestDetails[] {td1, td2, td3}));
em.persist(to);
em.getTransaction().commit();
what results in following sqls:
[03/07/14 10:03:30]  INFO jdbc.sqlonly: insert into test (orderid) values (1) 
[03/07/14 10:03:30]  INFO jdbc.sqlonly: insert into test_details (order_id_fk, id) values (NULL, 2) 
[03/07/14 10:03:30]  INFO jdbc.sqlonly: insert into test_details (order_id_fk, id) values (NULL, 3) 
[03/07/14 10:03:30]  INFO jdbc.sqlonly: insert into test_details (order_id_fk, id) values (NULL, 4) 
[03/07/14 10:03:30]  INFO jdbc.sqlonly: update test_details set order_id_fk=1 where id=2 
[03/07/14 10:03:30]  INFO jdbc.sqlonly: update test_details set order_id_fk=1 where id=3 
[03/07/14 10:03:30]  INFO jdbc.sqlonly: update test_details set order_id_fk=1 where id=4
So, if the code you posted is the code which you compile then TestOrder entity is not able to generate its ID (lack of @GeneratedValue annotation).
In case of database generating IDs (i.e. using autonumbering) for this entity, you should set @GeneratedValue to IDENTITY to let your jpa provider know that it must reread inserted row afterwards. If jpa provider fails to reread id, it updates test_details order_id_fk column with null.
PS. Why didn't you set many-to-one relationship at TestDetails side?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With