java org.hibernate.propertyvalueexception 非空属性引用空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2204658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:02:03  来源:igfitidea点击:

org.hibernate.propertyvalueexception not-null property references a null

javanetbeansjpa

提问by kobrakai

im new to JPA, i got the error above when trying to persist the transaction data.

我是 JPA 的新手,在尝试保留事务数据时出现上述错误。

Here's my Entity:

这是我的实体:

  @Entity
  @Table(name = "transaction1")
  @NamedQueries({
  @NamedQuery(name = "Transaction1.findAll", query = "SELECT t FROM Transaction1 t"),
  @NamedQuery(name = "Transaction1.findByTransactionID", query = "SELECT t FROM Transaction1 t WHERE t.transactionID = :transactionID"),
  @NamedQuery(name = "Transaction1.findByAmount", query = "SELECT t FROM Transaction1 t WHERE t.amount = :amount"),
  @NamedQuery(name = "Transaction1.findByFromAccNo", query = "SELECT t FROM Transaction1 t WHERE t.fromAccNo = :fromAccNo"),
  @NamedQuery(name = "Transaction1.findByFromSortCodeNo", query = "SELECT t FROM Transaction1 t WHERE t.fromSortCodeNo = :fromSortCodeNo"),
  @NamedQuery(name = "Transaction1.findByName", query = "SELECT t FROM Transaction1 t WHERE t.name = :name"),
  @NamedQuery(name = "Transaction1.findByToAccNo", query = "SELECT t FROM Transaction1 t WHERE t.toAccNo = :toAccNo"),
  @NamedQuery(name = "Transaction1.findByToSortCodeNo", query = "SELECT t FROM Transaction1 t WHERE t.toSortCodeNo = :toSortCodeNo"),
  @NamedQuery(name = "Transaction1.findByTransactionDate", query = "SELECT t FROM Transaction1 t WHERE t.transactionDate = :transactionDate")})
public class Transaction1 implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "Transaction_ID")
private Integer transactionID;
@Basic(optional = false)
@Column(name = "Amount")
private double amount;
@Basic(optional = false)
@Column(name = "From_Acc_No")
private int fromAccNo;
@Basic(optional = false)
@Column(name = "From_Sort_Code_No")
private String fromSortCodeNo;
@Basic(optional = false)
@Column(name = "Name")
private String name;
@Basic(optional = false)
@Column(name = "To_Acc_No")
private int toAccNo;
@Basic(optional = false)
@Column(name = "To_Sort_Code_No")
private String toSortCodeNo;
@Basic(optional = false)
@Column(name = "Transaction_Date")
@Temporal(TemporalType.TIMESTAMP)
private Date transactionDate;
static EntityManagerFactory emf = Persistence.createEntityManagerFactory("SWSXXPU");
public Transaction1() {
}

public Transaction1(Integer transactionID) {
    this.transactionID = transactionID;
}

public Transaction1(Integer transactionID, double amount, int fromAccNo, String fromSortCodeNo, String name, int toAccNo, String toSortCodeNo, Date transactionDate) {
    this.transactionID = transactionID;
    this.amount = amount;
    this.fromAccNo = fromAccNo;
    this.fromSortCodeNo = fromSortCodeNo;
    this.name = name;
    this.toAccNo = toAccNo;
    this.toSortCodeNo = toSortCodeNo;
    this.transactionDate = transactionDate;
}

public Integer getTransactionID() {
    return transactionID;
}

public void setTransactionID(Integer transactionID) {
    this.transactionID = transactionID;
}

public double getAmount() {
    return amount;
}

public void setAmount(double amount) {
    this.amount = amount;
}

public int getFromAccNo() {
    return fromAccNo;
}

public void setFromAccNo(int fromAccNo) {
    this.fromAccNo = fromAccNo;
}

public String getFromSortCodeNo() {
    return fromSortCodeNo;
}

public void setFromSortCodeNo(String fromSortCodeNo) {
    this.fromSortCodeNo = fromSortCodeNo;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getToAccNo() {
    return toAccNo;
}

public void setToAccNo(int toAccNo) {
    this.toAccNo = toAccNo;
}

public String getToSortCodeNo() {
    return toSortCodeNo;
}

public void setToSortCodeNo(String toSortCodeNo) {
    this.toSortCodeNo = toSortCodeNo;
}

public Date getTransactionDate() {
    return transactionDate;
}

public void setTransactionDate(Date transactionDate) {
    this.transactionDate = transactionDate;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (transactionID != null ? transactionID.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 Transaction1)) {
        return false;
    }
    Transaction1 other = (Transaction1) object;
    if ((this.transactionID == null && other.transactionID != null) || (this.transactionID != null && !this.transactionID.equals(other.transactionID))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "Entities.Transaction1[transactionID=" + transactionID + "]";
}

Here's my servlet:

这是我的 servlet:

   try{

         Transaction1 t = new Transaction1();

         t.setFromAccNo(youraccinput);
         t.setFromSortCodeNo(yoursortcodeinput);
         t.setToAccNo(toaccinput);
         t.setToSortCodeNo(destsortcodeinput);
         t.setName(recname);
         t.setAmount(amtsender);

         EntityManager em;
         EntityManagerFactory emf;
         try {
              emf = Persistence.createEntityManagerFactory("SWSXXPU");
              em = emf.createEntityManager();
              em.getTransaction().begin();
              em.persist(t);
              em.getTransaction().commit();
              request.getRequestDispatcher("ListTransaction").forward(request, response);
         }
         catch(Throwable e){
              out.print(e.getMessage());
              out.print(e.getCause());
          }
         Accounts.debitSourceAccBalance(youraccinput, toaccinput, recname, amtsender);
         }
         catch(Throwable e){
             out.print(e.getMessage());
             out.print(e.getCause());
         }

Full error message:

完整的错误信息:

org.hibernate.PropertyValueException: not-null property references a null or transient value: Entities.Transaction1.transactionDateorg.hibernate.PropertyValueException: not-null property references a null or transient value: Entities.Transaction1.transactionDate

org.hibernate.PropertyValueException:非空属性引用空值或瞬态值:Entities.Transaction1.transactionDateorg.hibernate.PropertyValueException:非空属性引用空值或瞬态值:Entities.Transaction1.transactionDate

I debugged it and it seemed like there is a value property of not null is null, which i dont seem to understand this error message. I thought I dont need to persist transactionID, as it is set to autoincrement, and transactionDate to timestamp oncurrentupdate. Can anyone please kindly help to fix?

我调试了它,似乎有一个值属性 not null is null,我似乎不明白这个错误消息。我认为我不需要保留 transactionID,因为它设置为自动增量,而 transactionDate 设置为 oncurrentupdate 的时间戳。任何人都可以请帮助修复吗?

回答by Chandra Sekar

The transactionDatefield has been marked with @Basic(optional = false). Ensure that the value for this field is not null while persisting the Transaction.

transactionDate字段已标有@Basic(optional = false)。确保在持久化事务时此字段的值不为空。

If the transactionData can be genuinely null (I guess not), you can remove the optionaloption from @Basic.

如果 transactionData 可以真正为 null(我猜不是),您可以optional@Basic.