2014-03-30 50 views
0

無法從數據庫中刪除/更新項目。嘗試刪除項目 時出現此異常並且此表單從MainCategory類獲取組合框值, MainCtegory的一個外鍵。這就是不允許刪除/更新的原因。java.lang.IllegalArgumentException:嘗試創建具有null實體的刪除事件

package com.auction management.entity;

import javax.persistence.Column; 

import javax.persistence.Entity; 

import javax.persistence.FetchType; 

import javax.persistence.GeneratedValue; 

import javax.persistence.GenerationType; 

import javax.persistence.Id; 

import javax.persistence.JoinColumn; 

import javax.persistence.ManyToOne; 

import javax.persistence.Table; 

@Entity 

@Table(name = "SubCategory") 

public class SubCategory { 

    @Id 
    @Column(name = "usbId",nullable = false,unique = true) 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private int subid; 

    @Column(name = "subName") 
    private String subName; 


    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "id",nullable = false) 
    private MainCategory mainCategory; 

    public MainCategory getMainCategory() { 
     return mainCategory; 
    } 

    public void setMainCategory(MainCategory mainCategory) { 
     this.mainCategory = mainCategory; 
    } 



    public SubCategory() { 
    } 

    public int getSubid() { 
     return subid; 
    } 

    public void setSubid(int subid) { 
     this.subid = subid; 
    } 

    public String getSubName() { 
     return subName; 
    } 

    public void setSubName(String subName) { 
     this.subName = subName; 
    } 


} 




public static boolean deleteSubCategory(int id){ 

     boolean status=false; 
     Session session=HibernateUtil.getSessionFactory().openSession(); 
     Transaction transaction=null; 

     try { 
      transaction=session.beginTransaction(); 
      SubCategory subcategory=(SubCategory)session.get(SubCategory.class,id); 
      session.delete(subcategory); 
      transaction.commit(); 
      status=true; 

     } catch (Exception e) { 
      e.printStackTrace(); 
      transaction.rollback(); 
      status=false; 
     } 
     finally{ 
      session.close(); 
     } 
     return status; 
    } 





private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) 
{ 

    boolean b=SubCategoryDao.deleteSubCategory(id); 

     if(b) 
     { 
      JOptionPane.showMessageDialog(this,"Type Deleted Successfully"); 
       Category=SubCategoryDao.getAllCategories(); 
       fillTable(); 
       clear(); 
     }else 
     { 
      JOptionPane.showMessageDialog(this,"Not Delete"); 
     } 
} 




private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {           
     String name=txtSubCategory.getText(); 

     SubCategory scategory=new SubCategory(); 

     scategory.setSubName(name); 
     scategory.setSubid(id); 

     SubCategoryDao.updateSubCategory(scategory); 
     Category=SubCategoryDao.getAllCategories(); 
     fillTable(); 
     clear(); 
    } 
+1

你究竟問了什麼?您只需發佈代碼和異常消息 – Mysterion

+0

我打賭子類別不在數據庫中。 – chrylis

回答

0

這大概線產生一個空: 子類別的子類別=(子類別)session.get(SubCategory.class,ID);

刪除時發生異常: session.delete(subcategory);

相關問題