Hibernate ManyToOne Mapping

时间:2015-12-16 13:44:24

标签: java hibernate many-to-one

我有一个名为Urunler的类和另一个名为Kategoriler的类。 Urunler表有外键,它是Kategoriler表的主键。 Kategoriler对象可以有多个Urunler对象。

我需要从Urunler类到Kategoriler类的ManyToOne映射。 但我无法实现它。

错误告诉我,我没有设置Urunler表的katid外键。

这是我的代码:

@Entity
@Table(name = "urunler")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Urunler.findAll", query = "SELECT u FROM Urunler u"),
@NamedQuery(name = "Urunler.findById", query = "SELECT u FROM Urunler u WHERE u.id = :id"),
@NamedQuery(name = "Urunler.findByAd", query = "SELECT u FROM Urunler u WHERE u.ad = :ad"),

@NamedQuery(name = "Urunler.findByAlis", query = "SELECT u FROM Urunler u WHERE u.alis = :alis"),
@NamedQuery(name = "Urunler.findBySatis", query = "SELECT u FROM Urunler u WHERE u.satis = :satis"),
@NamedQuery(name = "Urunler.findByStok", query = "SELECT u FROM Urunler u WHERE u.stok = :stok"),
@NamedQuery(name = "Urunler.findByAciklama", query = "SELECT u FROM Urunler u WHERE u.aciklama = :aciklama"),
@NamedQuery(name = "Urunler.findByKatid", query = "SELECT u FROM Urunler u WHERE u.katid = :katid")})
public class Urunler implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;

@ManyToOne(cascade = CascadeType.ALL)
private Kategoriler kategori;

public Kategoriler getKategori() {
    return kategori;
}

public void setKategori(Kategoriler kategori) {
    this.kategori = kategori;
}

@Basic(optional = false)
@Column(name = "ad")
private String ad;
@Basic(optional = false)
@Column(name = "alis")
private String alis;
@Basic(optional = false)
@Column(name = "satis")
private String satis;
@Basic(optional = false)
@Column(name = "stok")
private String stok;
@Column(name = "aciklama")
private String aciklama;
@Basic(optional = false)
@Column(name = "katid")
private Integer katid;

public Integer getKatid() {
    return katid;
}

public void setKatid(Integer katid) {
    this.katid = katid;
}

public Urunler() {
}

public Urunler(Integer id) {
    this.id = id;
}

public Urunler(Integer id, String ad, String kategori, String alis, String satis, String stok, int katid) {
    this.id = id;
    this.ad = ad;

    this.alis = alis;
    this.satis = satis;
    this.stok = stok;

}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getAd() {
    return ad;
}

public void setAd(String ad) {
    this.ad = ad;
}

public String getAlis() {
    return alis;
}

public void setAlis(String alis) {
    this.alis = alis;
}

public String getSatis() {
    return satis;
}

public void setSatis(String satis) {
    this.satis = satis;
}

public String getStok() {
    return stok;
}

public void setStok(String stok) {
    this.stok = stok;
}

public String getAciklama() {
    return aciklama;
}

public void setAciklama(String aciklama) {
    this.aciklama = aciklama;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Urunler)) {
        return false;
    }
    Urunler other = (Urunler) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "HibClasses.Urunler[ id=" + id + " ]";
 }

}                            

这个Kategoriler类:

@Entity
@Table(name = "kategoriler")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Kategoriler.findAll", query = "SELECT k FROM Kategoriler k"),
@NamedQuery(name = "Kategoriler.findById", query = "SELECT k FROM Kategoriler k WHERE k.id = :id"),
@NamedQuery(name = "Kategoriler.findByAd", query = "SELECT k FROM Kategoriler k WHERE k.ad = :ad"),
@NamedQuery(name = "Kategoriler.findByAciklama", query = "SELECT k FROM Kategoriler k WHERE k.aciklama = :aciklama")})
public class Kategoriler implements Serializable {
private static final long serialVersionUID = 1L;
@Id 
@GeneratedValue
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Column(name = "ad")
private String ad;
@Column(name = "aciklama")
private String aciklama;

public Kategoriler() {
}

public Kategoriler(Integer id) {
    this.id = id;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getAd() {
    return ad;
}

public void setAd(String ad) {
    this.ad = ad;
}

public String getAciklama() {
    return aciklama;
}

public void setAciklama(String aciklama) {
    this.aciklama = aciklama;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Kategoriler)) {
        return false;
    }
    Kategoriler other = (Kategoriler) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "HibClasses.Kategoriler[ id=" + id + " ]";
}

}

这就是我所说的:

btnDuzenle.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {

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

            session.beginTransaction();
            Urunler urunler = new Urunler();
            Kategoriler kategoriler = new Kategoriler();
            kategoriler.setAd("Gıda");
            kategoriler.setAciklama("Yenir la bunlar");

            urunler.setAd("Bisküvi");
            urunler.setAciklama("Biskrem");
            urunler.setAlis("4");
            urunler.setSatis("5");
            urunler.setKategori(kategoriler);
            urunler.setStok("4");



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

        }
    });

2 个答案:

答案 0 :(得分:1)

我找到了解决方案。

我已经改变了

...
@ManyToOne(cascade = CascadeType.ALL)
private Kategoriler kategori;

public Kategoriler getKategori() {
return kategori;
}
...

到那个:

...
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "katid")
private Kategoriler kategori;

public Kategoriler getKategori() {
return kategori;
}
...

...
@Column(name = "katid")
private Integer katid;

public Integer getKatid() {
return katid;
} 
...

到那个:

...
@Column(name = "katid", insertable = false , updatable = false)
private Integer katid;

public Integer getKatid() {
return katid;
}
...

答案 1 :(得分:0)

Kategoriler应该有一个由Kategoriler对象映射的列表,其中包含正确的anotations:

@OneToMany(mappedBy="kategoriler",cascade=CascadeType.ALL)
private List<Urunler> employees = new ArrayList<Urunler >();