Hibernate One to Many Mapping tries to update the display column to zero

I use 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 data, it tries to update order_id_fkto null

SQL [update test_details set order_id_fk'='null where order_id_fk'='? and id'='?]; constraint [null];

Any help is greatly appreciated.

Update / Insert 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>
+4
source share
3 answers

You need to get the values ​​of the TestDetailsobject with impatience.

Just change your annotation with

@OneToMany(fetch = FetchType.EAGER, mappedBy="testOrder", cascade=CascadeType.ALL)

Hope this works.

+10
source

I ran your code and it works correctly (when added @GeneratedValue(strategy = GenerationType.AUTO)to both identifiers).

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();

which leads to the 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

, , , , , TestOrder ( @GeneratedValue).

(.. ) , @GeneratedValue IDENTITY, jpa , . jpa id, order_id_fk test_details .

PS. "--" TestDetails?

+4

, , . , , .

@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, insertable = false)
    private TestOrder orderId;
//getters and setters
}
0

Source: https://habr.com/ru/post/1546535/


All Articles