【问题标题】:OneToOne Mapping does not set mapped objectOneToOne 映射不设置映射对象
【发布时间】:2015-12-20 14:11:39
【问题描述】:

我有 3 个班级,即 SatislarUrunlerMusteriler

在 Satislar 类中,有 2 个外键:urunidmusteriid

每个Satislar 对象都有一个Urunler 对象和一个Musteriler 对象。

所以我的Satislar 类中需要两个不同的OneToOne 映射。首先是Urunler,其次是Musteriler

当我向数据库添加一个新的 Satislar 对象时,我将使用现有的 Musteriler 对象和 Urunler 对象。代码不得创建新的MusterilerUrunler 对象。 为了实现这一点,我使用了

(insertable= false, updatable=false)

但我不能将 UrunlerMusteriler 对象设置为 Satislar 对象。它说:

列不允许空值。插入失败。

我认为我的注释有问题。

这是我的代码块。

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

坐骑类:

...
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;
}
...

【问题讨论】:

    标签: java hibernate hibernate-mapping cascade one-to-one


    【解决方案1】:

    您已使所有内容都不可插入和不可更新。因此,Hibernate 在插入 Satislar 时不会在列中插入任何内容。

    删除 urunidmusteriid 字段:它们没用。您已经有一个 OneToOne 关联,这就足够了。

    然后从两个JoinColumn 注释中删除insertableupdatable 属性。

    此外,如果多个 Satislar 指代同一个 Urunler 或 Musteriler,那么您实际上拥有的是 ManyToOne 关联,而不是 OneToOne。

    最后,cascade = CascadeType.ALL 非常可疑:如果 Urunler 可以在没有 Satislar 的情况下存在,那么您可能不想在创建/移除 Satislar 时创建/移除 Urunler。

    【讨论】:

    • 我犯了一些简单的错误,因为这是我第一个使用 Hibernate 的项目。你的回答解决了。谢谢。出现 urunid 和 musteriid 的原因我完全忘了删除它们。我在想可插入和可更新用于防止另一个表发生变化。我认为级联选项可以做到这一点。我已将 OneToOne 更改为 ManyTo One 。这是我最大的错误。
    【解决方案2】:

    当您为关系指定@JoinColumn(name = "column_id") 时,hibernate 将找出column_id 作为关系的外键列。您只需将实体添加在一起并保存即可。

    Satislar s = new Satislar();
    
    Urunler u = new Urunler();
    
    Musteriler m = new Musteriler();
    
    s.setUrun(u);
    s.setMusteri(m);
    
    session.save(s);
    

    您应该从Satislar 中删除urunidmusteriid 字段,并在JoinColumn 注释中为这些列指定所需的列名。

    @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;
        ....
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-06-02
      • 2013-08-01
      • 2014-12-27
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      • 2013-10-12
      • 1970-01-01
      相关资源
      最近更新 更多