Reputation: 35
I am new to this forum and hibernate. I am having a problem with hibernate many-to-one mapping.
@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name = "DTE_ID")
@NotNull
private Dte raisedByDte;
This is the code I am using in main object and foreign key is DTE_ID
. But when I am trying to save it is updating all fields in referenced table. My reference object is as follows:
@Entity
@Table(name = "DTE_MASTERS", uniqueConstraints = @UniqueConstraint(columnNames = "DTE_NAME"))
public class Dte {
@Id
@Column(name="DTE_ID", updatable = false, nullable = false)
private int dte_id;
@Column(name="DTE_NAME")
private String dte_name;
public Dte() {
super();
// TODO Auto-generated constructor stub
}
public Dte(int dte_id, String dte_name) {
super();
this.dte_id = dte_id;
this.dte_name = dte_name;
}
public int getDte_id() {
return dte_id;
}
public void setDte_id(int dte_id) {
this.dte_id = dte_id;
}
public String getDte_name() {
return dte_name;
}
public void setDte_name(String dte_name) {
this.dte_name = dte_name;
}
I want to restrict the update of DTE_MASTERS
when I am inserting ..can some body please guide me through this?
Upvotes: 2
Views: 1062
Reputation: 26502
You have to remove the cascade option from the mapping. It enforces the same operation as was performed on the parent object.
I am guessing that you are doing merge()
on the main object.. and with that option, the merge()
(which would result in an update if the entity is not new) will also be invoked on the Dte
dependency:
@ManyToOne(fetch = FetchType.EAGER)
Also, keep in mind that if you any non-transient field within the Dte
entity once its loaded with the main entity, all of the changes will be commited implicilty upon the transaction end.
In order to prevent that you would need to perform session.evict(dte);
so that any changes will not get persisted in the database even if they were performed in within the transactional method.
Upvotes: 0