【问题标题】:Display mysql exception by hibernate javafx通过休眠 javafx 显示 mysql 异常
【发布时间】:2018-04-17 05:43:18
【问题描述】:

我有这个代码: 班级社区

package com.saverio.Anagrafica.Entita;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

@Entity
 @Table
public class Comuni {
private final SimpleIntegerProperty id;
private final SimpleStringProperty codice;
private final SimpleStringProperty comune;
private final SimpleStringProperty cap;
private final SimpleStringProperty regione;
private final SimpleStringProperty provincia;


public Comuni() {
    this(0, null, null, null, null, null);
}


public Comuni(int id,String codice, String comune, String cap, String regione, String provincia) {
    super();
    this.id = new SimpleIntegerProperty(id);
    this.codice = new SimpleStringProperty(codice);
    this.comune = new SimpleStringProperty(comune);
    this.cap = new SimpleStringProperty(cap);
    this.regione = new SimpleStringProperty(regione);;
    this.provincia = new SimpleStringProperty(provincia);
}

public void setId (int id){
    this.id.set(id);
}

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
public final int getId(){
    return id.get();
}

public void setCodice (String codice){
    this.codice.set(codice);
}   

@Column(name="codice", unique=true)
public final String getCodice(){
    return codice.get();
}


public void setComune (String comune){
    this.comune.set(comune);
}

@Column(name="comune")
public final String getComune(){
    return comune.get();
}



public void setCap (String cap){
    this.cap.set(cap);
}

@Column(name="cap")
public final String getCap(){
    return cap.get();
}


public void setRegione (String regione){
    this.regione.set(regione);
}

@Column(name="regione")
public final String getRegione(){
    return regione.get();
}



public void setProvincia (String provincia){
    this.provincia.set(provincia);
}

@Column(name="provincia")
public final String getProvincia(){
    return provincia.get();
}   

}

类 Comuni.Dao

public static void insertDati(Comuni comune) throws HibernateException  {
    Configuration con = new Configuration();
    con.configure("hibernate.cfg.xml");
    SessionFactory sf = con.buildSessionFactory();
    Session session = sf.openSession();
    Transaction tr = session.beginTransaction();
    session.save(comune);
    tr.commit();
    session.close();
    sf.close();
}

SchedaComuneController 类:

@FXML
void salvaComune(ActionEvent event) {
    Comuni comune = new Comuni();
    comune.setCodice(textCodice.getText());
    comune.setCap(textCap.getText());
    comune.setComune(textComune.getText());
    comune.setRegione(cmbRegioni.getValue());
    comune.setProvincia(cmbProvincia.getValue());
    try{
    ComuniDao.insertDati(comune);
    btnSalva.getScene().getWindow().hide();
    }catch(HibernateException  ex){
        //if (ex.getErrorCode() == 1062){
            AlertDialog.datiBox("Attenzione, esiste gia' un comune con questo codice", "Errore!", null);
            System.out.println(ex);
        //}
    }

当运行 salvaComune 方法时,我在 Eclipse 控制台中有此异常:“org.hibernate.exception.ConstraintViolationException:无法执行语句” 我知道错误是重复条目,但我想通过 System.out.println 显示 mysql 异常。 谢谢。

【问题讨论】:

    标签: mysql hibernate exception javafx


    【解决方案1】:

    如果我理解正确,您只想将异常的堆栈跟踪打印到System.out 而不是System.errjava.lang.Throwable 类(所有异常/错误都继承自)具有以下方法:printStackTrace(java.io.PrintStream)。幸运的是,System.out 字段是 java.io.PrintStream

    对于您的用例:ex.printStackTrace(System.out)


    因评论而更新:

    您在问题中声明HibernateException 的特定类是ConstraintViolationException。查看该类的文档,该类有一个方法 getConstraintName(),它可能会为您提供一些您想要的信息。

    此外,ConstraintViolationException 扩展自 JDBCException,后者本身包装了选中的“较低级别”java.sql.SQLExceptionJDBCException 类提供了获取 SQL 状态和错误代码的方法以及对底层 SQLException 的直接访问,这可能会为您提供更多您想要的信息。 SQLException 本身的消息可能带有关于它被抛出的更具体的原因。

    但是,如果您想在控制台上显示此信息可能,这取决于您希望错误的外观,必须手动创建错误消息并将其打印到System.out

    【讨论】:

    • 谢谢斯劳。我不明白为什么 HibernateException 显示一个通用异常“无法执行语句”而不是真正的错误,在这种情况下,键“代码”的重复条目“12345”。我想显示它。
    猜你喜欢
    • 1970-01-01
    • 2014-08-22
    • 2011-02-14
    • 2011-09-20
    • 2017-07-06
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多