Erdel
Erdel

Reputation: 380

OneToOne Mapping does not set mapped object

I have 3 classes namely Satislar, Urunler and Musteriler

In Satislar class, there is 2 foreign keys: urunid and musteriid

Every Satislar object has one Urunler object and one Musteriler object.

So I need two different OneToOne mapping in my Satislar class. First to Urunler and second to Musteriler.

When I add a new Satislar object to database, I will use existing Musteriler object and Urunler object. Code must not create new Musteriler, Urunler objects. To achieve that I have used

(insertable= false, updatable=false)

But I can not set Urunler and Musteriler objects to a Satislar object. It says :

column does not allow nulls. INSERT fails.

I think I have a problem with annotations.

Here is my code blocks.

...
Session session = null;    
Urunler urun = gelenUrunler.get(tblUrunler.getSelectedRow());
Musteriler musteri = gelenMusteriler.get(cmbMusteriler.getSelectedIndex());
System.out.println(musteri.getId() + "asd " + urun.getId());
Satislar satis = new Satislar();
satis.setUrun(urun);
satis.setMusteri(musteri);
satis.setAdet(Integer.valueOf(txtAdet.getText().trim()));
satis.setTarih(new Date());

try {

    session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();

    session.save(satis);    
    session.getTransaction().commit();  

} catch (Exception e) {
    System.out.println("Satış sırasında hata oluştu." + e);
} finally {
    session.close();        
}
...

Satislar class:

...
private static final long serialVersionUID = 1L;
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@Column(name = "urunid", insertable = false, updatable = false)
private Integer urunid;
@Basic(optional = false)
@Column(name = "adet")
private Integer adet;
@Basic(optional = false)
@Column(name = "musteriid", insertable = false, updatable = false)
private Integer musteriid;
@Basic(optional = false)
@Column(name = "tarih")
@Temporal(TemporalType.TIMESTAMP)
private Date tarih;




@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "urunid", insertable = false, updatable = false)
private Urunler urun;

public Urunler getUrun() {
    return urun;
}

public void setUrun(Urunler urun) {
    this.urun = urun;
}

@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "musteriid", insertable = false, updatable = false)
private Musteriler musteri;

public Musteriler getMusteri() {
    return musteri;
}

public void setMusteri(Musteriler musteri) {
    this.musteri = musteri;
}
...

Upvotes: 0

Views: 51

Answers (2)

tharindu_DG
tharindu_DG

Reputation: 9261

When you specify the @JoinColumn(name = "column_id") for the relation, hibernate will figure out column_id as the foreignkey column for the relation. You just have to add the entities together and save them.

Satislar s = new Satislar();

Urunler u = new Urunler();

Musteriler m = new Musteriler();

s.setUrun(u);
s.setMusteri(m);

session.save(s);

You should remove the urunid and musteriid fields from Satislar and specify the column names as you want for those columns in the JoinColumn annotation.

@Entity
public class Satislar {
   ....

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "urun_id")
    private Urunler urun;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "musteri_id")
    private Musteriler musteri;
    ....
}

Hope this helps.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691625

You've made everything non-insertable and non-updatable. So Hibernate doesn't insert anything in the columns when it inserts the Satislar.

Remove the urunid and musteriid fields: they're useless. You already have a OneToOne association, which is sufficient.

Then remove the insertable and updatable attributes from the two JoinColumn annotations.

Also, if several Satislar refer to the same Urunler or Musteriler, what you actually have is a ManyToOne association, and not a OneToOne.

And finally, cascade = CascadeType.ALL is very dubious: if an Urunler can exist without a Satislar, then you probably don't want to create/remove an Urunler when creating/removing a Satislar.

Upvotes: 1

Related Questions