空指针指向外键

时间:2019-01-04 10:22:05

标签: java mysql hibernate

我正在使用MySQL和Hibernate。我已将记录添加到Receiving表中,并在idbarcodetransaction中将rcidbarcode字段作为外键。

当我使用ID搜索特定记录时:

int id =(Integer)defaultTableModel.getValueAt(jReceivingsTable.getSelectedRow(),0);

Receivings receivings = manager.searchReceivingsId(id);

那不是null,打印id会返回适当的值,那么为什么将其设置为Transaction表时却得到null?

Rctransaction rctransaction = new Rctransaction();
    rctransaction.getReceivings().getId().setId(id); // here id has value 
    rctransaction.getReceivings().getId().setBarcode(barcode);
    rctransaction.setReceivings(receivings);  

以下是类:
Receivings.java

public class Receivings  implements java.io.Serializable {


     private ReceivingsId id;
     private Suppliers suppliers;
     private String itemName;
     private String category;
     private double wholesalePrice;
     private double retailPrice;
     private long tax1;
     private long tax2;
     private String type1;
     private String type2;
     private int quantityStock;
     private int receivingQuantity;
     private int recorderLevel;
     private String receivingDate;
     private double discount;
     private Set itemses = new HashSet(0);
     private Set rctransactions = new HashSet(0);

    public Receivings() {
    }


    public Receivings(ReceivingsId id, Suppliers suppliers, String itemName, String category, double wholesalePrice, double retailPrice, long tax1, long tax2, String type1, String type2, int quantityStock, int receivingQuantity, int recorderLevel, String receivingDate, double discount) {
        this.id = id;
        this.suppliers = suppliers;
        this.itemName = itemName;
        this.category = category;
        this.wholesalePrice = wholesalePrice;
        this.retailPrice = retailPrice;
        this.tax1 = tax1;
        this.tax2 = tax2;
        this.type1 = type1;
        this.type2 = type2;
        this.quantityStock = quantityStock;
        this.receivingQuantity = receivingQuantity;
        this.recorderLevel = recorderLevel;
        this.receivingDate = receivingDate;
        this.discount = discount;
    }
    public Receivings(ReceivingsId id, Suppliers suppliers, String itemName, String category, double wholesalePrice, double retailPrice, long tax1, long tax2, String type1, String type2, int quantityStock, int receivingQuantity, int recorderLevel, String receivingDate, double discount, Set itemses, Set rctransactions) {
       this.id = id;
       this.suppliers = suppliers;
       this.itemName = itemName;
       this.category = category;
       this.wholesalePrice = wholesalePrice;
       this.retailPrice = retailPrice;
       this.tax1 = tax1;
       this.tax2 = tax2;
       this.type1 = type1;
       this.type2 = type2;
       this.quantityStock = quantityStock;
       this.receivingQuantity = receivingQuantity;
       this.recorderLevel = recorderLevel;
       this.receivingDate = receivingDate;
       this.discount = discount;
       this.itemses = itemses;
       this.rctransactions = rctransactions;
    }

    public ReceivingsId getId() {
        return this.id;
    }

    public void setId(ReceivingsId id) {
        this.id = id;
    }
    public Suppliers getSuppliers() {
        return this.suppliers;
    }

    public void setSuppliers(Suppliers suppliers) {
        this.suppliers = suppliers;
    }
    public String getItemName() {
        return this.itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }
    public String getCategory() {
        return this.category;
    }

    public void setCategory(String category) {
        this.category = category;
    }
    public double getWholesalePrice() {
        return this.wholesalePrice;
    }

    public void setWholesalePrice(double wholesalePrice) {
        this.wholesalePrice = wholesalePrice;
    }
    public double getRetailPrice() {
        return this.retailPrice;
    }

    public void setRetailPrice(double retailPrice) {
        this.retailPrice = retailPrice;
    }
    public long getTax1() {
        return this.tax1;
    }

    public void setTax1(long tax1) {
        this.tax1 = tax1;
    }
    public long getTax2() {
        return this.tax2;
    }

    public void setTax2(long tax2) {
        this.tax2 = tax2;
    }
    public String getType1() {
        return this.type1;
    }

    public void setType1(String type1) {
        this.type1 = type1;
    }
    public String getType2() {
        return this.type2;
    }

    public void setType2(String type2) {
        this.type2 = type2;
    }
    public int getQuantityStock() {
        return this.quantityStock;
    }

    public void setQuantityStock(int quantityStock) {
        this.quantityStock = quantityStock;
    }
    public int getReceivingQuantity() {
        return this.receivingQuantity;
    }

    public void setReceivingQuantity(int receivingQuantity) {
        this.receivingQuantity = receivingQuantity;
    }
    public int getRecorderLevel() {
        return this.recorderLevel;
    }

    public void setRecorderLevel(int recorderLevel) {
        this.recorderLevel = recorderLevel;
    }
    public String getReceivingDate() {
        return this.receivingDate;
    }

    public void setReceivingDate(String receivingDate) {
        this.receivingDate = receivingDate;
    }
    public double getDiscount() {
        return this.discount;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }
    public Set getItemses() {
        return this.itemses;
    }

    public void setItemses(Set itemses) {
        this.itemses = itemses;
    }
    public Set getRctransactions() {
        return this.rctransactions;
    }

    public void setRctransactions(Set rctransactions) {
        this.rctransactions = rctransactions;
    }   
}  

ReceivingsId.java

//Composition of Id and Barcode of Receivings  
public class ReceivingsId  implements java.io.Serializable {


     private int id;
     private int barcode;

    public ReceivingsId() {
    }

    public ReceivingsId(int id, int barcode) {
       this.id = id;
       this.barcode = barcode;
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }
    public int getBarcode() {
        return this.barcode;
    }

    public void setBarcode(int barcode) {
        this.barcode = barcode;
    }


   public boolean equals(Object other) {
         if ( (this == other ) ) return true;
         if ( (other == null ) ) return false;
         if ( !(other instanceof ReceivingsId) ) return false;
         ReceivingsId castOther = ( ReceivingsId ) other; 

         return (this.getId()==castOther.getId())
 && (this.getBarcode()==castOther.getBarcode());
   }

   public int hashCode() {
         int result = 17;

         result = 37 * result + this.getId();
         result = 37 * result + this.getBarcode();
         return result;
   }   
}

Rctransaction.java

public class Rctransaction  implements java.io.Serializable {


     private Integer id;
     private Receivings receivings;
     private String transaction;
     private String supplierName;
     private String itemName;
     private double quantity;
     private String paymentType;
     private double amountTendred;
     private double due;
     private double total;
     private double price;
     private int recorderLevel;

    public Rctransaction() {
    }

    public Rctransaction(Receivings receivings, String transaction, String supplierName, String itemName, double quantity, String paymentType, double amountTendred, double due, double total, double price, int recorderLevel) {
       this.receivings = receivings;
       this.transaction = transaction;
       this.supplierName = supplierName;
       this.itemName = itemName;
       this.quantity = quantity;
       this.paymentType = paymentType;
       this.amountTendred = amountTendred;
       this.due = due;
       this.total = total;
       this.price = price;
       this.recorderLevel = recorderLevel;
    }

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
    public Receivings getReceivings() {
        return this.receivings;
    }

    public void setReceivings(Receivings receivings) {
        this.receivings = receivings;
    }
    public String getTransaction() {
        return this.transaction;
    }

    public void setTransaction(String transaction) {
        this.transaction = transaction;
    }
    public String getSupplierName() {
        return this.supplierName;
    }

    public void setSupplierName(String supplierName) {
        this.supplierName = supplierName;
    }
    public String getItemName() {
        return this.itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }
    public double getQuantity() {
        return this.quantity;
    }

    public void setQuantity(double quantity) {
        this.quantity = quantity;
    }
    public String getPaymentType() {
        return this.paymentType;
    }

    public void setPaymentType(String paymentType) {
        this.paymentType = paymentType;
    }
    public double getAmountTendred() {
        return this.amountTendred;
    }

    public void setAmountTendred(double amountTendred) {
        this.amountTendred = amountTendred;
    }
    public double getDue() {
        return this.due;
    }

    public void setDue(double due) {
        this.due = due;
    }
    public double getTotal() {
        return this.total;
    }

    public void setTotal(double total) {
        this.total = total;
    }
    public double getPrice() {
        return this.price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
    public int getRecorderLevel() {
        return this.recorderLevel;
    }

    public void setRecorderLevel(int recorderLevel) {
        this.recorderLevel = recorderLevel;
    }
}

1 个答案:

答案 0 :(得分:-1)

您以错误的顺序实例化了Hibernate对象。

首先,您需要一个ReceivingsId:

ReceivingsId rid = new ReceivingsId (id, barcode);

然后将此rid添加到Receivings

Receivings rec = new Receivings();
rec.setId(rid);

最后将其设置为Rctransaction

Rctransaction rctransaction = new Rctransaction();
rctransaction.setReceivings(rec);  

如果在实例化对象后尝试调用getReceivings而未设置对象,则始终会收到NullPointerException。为避免这种情况,可以在Receivings构造函数中实例化Rctransaction对象。