Reddy Reddy - 4 months ago 15
Java Question

Hibernate One to Many Mapping is trying to update mapping column to null

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>

Answer

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.