-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Hibernate save is updating foreign key table.
PostPosted: Fri Dec 08, 2017 6:09 am 
Newbie

Joined: Fri Dec 08, 2017 6:00 am
Posts: 4
Hi friends,

I am new to this forum and hibernate .I am having a problem with hibernate many to one mapping.

Code:
@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
   @JoinColumn(name = "DTE_ID")
   @NotNull
   private Dte raisedByDte;


This is the code iam using in main object and foreign key is DTE_ID.But when i am trying to save it is updating all fields in referance table .My referance object is as follows

Code:
@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 update DTE_MASTERS when i am inserting ..can some body please guide me through this


Regards
Srini

T


Top
 Profile  
 
 Post subject: Re: Hibernate save is updating foreign key table.
PostPosted: Fri Dec 08, 2017 6:39 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Quote:
I want to restrict update DTE_MASTERS when i am inserting ..can some body please guide me through this


Can you try to rephrase it because it's not clear what you are asking.


Top
 Profile  
 
 Post subject: Re: Hibernate save is updating foreign key table.
PostPosted: Fri Dec 08, 2017 6:56 am 
Newbie

Joined: Fri Dec 08, 2017 6:00 am
Posts: 4
vlad wrote:
Quote:
I want to restrict update DTE_MASTERS when i am inserting ..can some body please guide me through this


Can you try to rephrase it because it's not clear what you are asking.


DTE_MASTERS IS A MASTERS TABLE and dte object is used as referance object many to one in base object.I do not want to do any thing with DTE_MASTERS.

just want to insert primary key vaue in main object which ia m trying insert new

@Proxy(lazy = false)
@Table(name = "MSMAN")
public class Msman {
@Id @GeneratedValue
@Column(name="MSAMN_ID")
private int msmanId;

@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name = "DTE_ID")
@NotNull
private Dte raisedByDte;



public Dte getRaisedByDte() {
return raisedByDte;
}


public void setRaisedByDte(Dte raisedByDte) {
this.raisedByDte = raisedByDte;
}

}


now ia am creating am object msman.setraisedByDte(new Dte(4)); and trying to save msman object.

New row is being inserted in msman table.but it is updating all dte_masters field value to null except DTE_ID which is 4 (already existing).


Thanks in advance


Top
 Profile  
 
 Post subject: Re: Hibernate save is updating foreign key table.
PostPosted: Fri Dec 08, 2017 7:42 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
1. First, use LAZY associations, not EAGER because EAGER is almost always a bad choice.

Code:
@ManyToOne(fetch = FetchType.LAZY)


2. Remove cascade = CascadeType.ALL from the @ManyToOne association because a child should never cascade to a parent entity.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.