Hibernate Session未打开使用Javafx

时间:2013-08-07 09:29:09

标签: hibernate javafx

我正在使用Netbeans 7.3.1

在Havnate中使用Javafx开发简单的Application

在小项目中,我使用Wizards Setup连接了hibernate。我想使用hibernate在TableView中显示公司表数据。但是,当我运行此应用程序时,它显示以下错误消息:

    INFO: building session factory
    Initial SessionFactory creation failed.net.sf.cglib.core.CodeGenerationException:     
    java.lang.reflect.InvocationTargetException-->null
    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1440)
at 
    com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent
    (CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent
    (EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent
    (EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent
    (CompositeEventDispatcher.java:38)

<小时/> 我有以下文件:

1。的hibernate.cfg.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration 
 DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 <hibernate-configuration>
   <session-factory>
     <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
     <property name="hibernate.connection.driver_class">
       org.apache.derby.jdbc.ClientDriver</property>
     <property name="hibernate.connection.url">
       jdbc:derby://localhost:1527/sample</property>
     <property name="hibernate.connection.username">app</property>
     <property name="hibernate.connection.password">app</property>
     <property name="hibernate.show_sql">true</property>
     <property name="hibernate.query.factory_class">
     org.hibernate.hql.ast.ASTQueryTranslatorFactory</property>
     <mapping resource="sample/entity/Company.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

2。 hibernate.reveng.xml中                                         

第3。 HibernateUtil.java

 package sample.util;
 import org.hibernate.cfg.AnnotationConfiguration;
 import org.hibernate.SessionFactory;
 public class HibernateUtil {
     private static final SessionFactory sessionFactory;

static {
    try {
        // Create the SessionFactory from standard (hibernate.cfg.xml) 
        // config file.
        sessionFactory = 
          new AnnotationConfiguration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        // Log the exception. 
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}
}

**4. Company.java**
 package sample.entity;
 // Generated Aug 6, 2013 12:55:08 PM by Hibernate Tools 3.2.1.GA


import java.util.Date;

/**
 * Company generated by hbm2java
*/
public class Company  implements java.io.Serializable {


 private Integer id;
 private String companyName;
 private String companyLogo;
 private String ownerName;
 private String address1;
 private String address2;
 private String city;
 private String state;
 private String pincode;
 private String countryCode;
 private String country;
 private String areaCode;
 private String offPhoneNo;
 private String offFaxNo;
 private String emailId;
 private String tanNo;
 private String panNo;
 private String website;
 private Date updt;
 private Date uptm;

public Company() {
}

public Company(String companyName, String companyLogo, String ownerName,
String address1, String address2, String city, String state, String pincode, 
String countryCode, String country, String areaCode, String offPhoneNo, 
String offFaxNo, String emailId, String tanNo, String panNo, String website, 
Date updt, Date uptm) {
   this.companyName = companyName;
   this.companyLogo = companyLogo;
   this.ownerName = ownerName;
   this.address1 = address1;
   this.address2 = address2;
   this.city = city;
   this.state = state;
   this.pincode = pincode;
   this.countryCode = countryCode;
   this.country = country;
   this.areaCode = areaCode;
   this.offPhoneNo = offPhoneNo;
   this.offFaxNo = offFaxNo;
   this.emailId = emailId;
   this.tanNo = tanNo;
   this.panNo = panNo;
   this.website = website;
   this.updt = updt;
   this.uptm = uptm;
}

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

public void setId(Integer id) {
    this.id = id;
}
public String getCompanyName() {
    return this.companyName;
}

public void setCompanyName(String companyName) {
    this.companyName = companyName;
}
public String getCompanyLogo() {
    return this.companyLogo;
}

public void setCompanyLogo(String companyLogo) {
    this.companyLogo = companyLogo;
}
public String getOwnerName() {
    return this.ownerName;
}

public void setOwnerName(String ownerName) {
    this.ownerName = ownerName;
}
public String getAddress1() {
    return this.address1;
}

public void setAddress1(String address1) {
    this.address1 = address1;
}
public String getAddress2() {
    return this.address2;
}

public void setAddress2(String address2) {
    this.address2 = address2;
}
public String getCity() {
    return this.city;
}

public void setCity(String city) {
    this.city = city;
}
public String getState() {
    return this.state;
}

public void setState(String state) {
    this.state = state;
}
public String getPincode() {
    return this.pincode;
}

public void setPincode(String pincode) {
    this.pincode = pincode;
}
public String getCountryCode() {
    return this.countryCode;
}

public void setCountryCode(String countryCode) {
    this.countryCode = countryCode;
}
public String getCountry() {
    return this.country;
}

public void setCountry(String country) {
    this.country = country;
}
public String getAreaCode() {
    return this.areaCode;
}

public void setAreaCode(String areaCode) {
    this.areaCode = areaCode;
}
public String getOffPhoneNo() {
    return this.offPhoneNo;
}

public void setOffPhoneNo(String offPhoneNo) {
    this.offPhoneNo = offPhoneNo;
}
public String getOffFaxNo() {
    return this.offFaxNo;
}

public void setOffFaxNo(String offFaxNo) {
    this.offFaxNo = offFaxNo;
}
public String getEmailId() {
    return this.emailId;
}

public void setEmailId(String emailId) {
    this.emailId = emailId;
}
public String getTanNo() {
    return this.tanNo;
}

public void setTanNo(String tanNo) {
    this.tanNo = tanNo;
}
public String getPanNo() {
    return this.panNo;
}

public void setPanNo(String panNo) {
    this.panNo = panNo;
}
public String getWebsite() {
    return this.website;
}

public void setWebsite(String website) {
    this.website = website;
}
public Date getUpdt() {
    return this.updt;
}

public void setUpdt(Date updt) {
    this.updt = updt;
}
public Date getUptm() {
    return this.uptm;
}

public void setUptm(Date uptm) {
    this.uptm = uptm;
}
}

5。 Company.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Aug 6, 2013 12:55:08 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="sample.entity.Company" table="company" catalog="sample">
    <id name="id" type="java.lang.Integer">
        <column name="Id" />
        <generator class="identity" />
    </id>
    <property name="companyName" type="string">
        <column name="CompanyName" length="100" />
    </property>
    <property name="companyLogo" type="string">
        <column name="CompanyLogo" length="50" />
    </property>
    <property name="ownerName" type="string">
        <column name="OwnerName" length="50" />
    </property>
    <property name="address1" type="string">
        <column name="Address1" length="50" />
    </property>
    <property name="address2" type="string">
        <column name="Address2" length="50" />
    </property>
    <property name="city" type="string">
        <column name="City" length="30" />
    </property>
    <property name="state" type="string">
        <column name="State" length="30" />
    </property>
    <property name="pincode" type="string">
        <column name="Pincode" length="10" />
    </property>
    <property name="countryCode" type="string">
        <column name="CountryCode" length="10" />
    </property>
    <property name="country" type="string">
        <column name="Country" length="30" />
    </property>
    <property name="areaCode" type="string">
        <column name="AreaCode" length="30" />
    </property>
    <property name="offPhoneNo" type="string">
        <column name="OffPhoneNo" length="15" />
    </property>
    <property name="offFaxNo" type="string">
        <column name="OffFaxNo" length="15" />
    </property>
    <property name="emailId" type="string">
        <column name="EmailId" length="50" />
    </property>
    <property name="tanNo" type="string">
        <column name="TanNo" length="35" />
    </property>
    <property name="panNo" type="string">
        <column name="PanNo" length="35" />
    </property>
    <property name="website" type="string">
        <column name="Website" length="30" />
    </property>
    <property name="updt" type="date">
        <column name="Updt" length="10" />
    </property>
    <property name="uptm" type="time">
        <column name="Uptm" length="8" />
    </property>
    </class>
   </hibernate-mapping>

使用下面的代码,我想获取数据:

@FXML   
void initialize() {
    assert btnQuery != null : "fx:id=\"btnQuery\" was not injected: check your 
      FXML  file 'testing.fxml'.";
    assert tableview != null : "fx:id=\"tableview\" was not injected: check your 
      FXML file 'testing.fxml'.";
    assert txtName != null : "fx:id=\"txtName\" was not injected: check your 
      FXML file 'testing.fxml'.";
}
private String QUERY_ALL = "from Company";

@FXML
void btnQueryClick(ActionEvent event) {
    executeHQLQuery(QUERY_ALL);
}

private void executeHQLQuery(String hql) {

    try {

        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Query q = session.createQuery(hql);   
        System.out.println(hql);
        List resultList = q.list();            
        displayResult(resultList);
        session.getTransaction().commit();
    } catch (HibernateException he) {
        he.printStackTrace();
    }
}

这是生成的异常:

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

日志输出:

INFO: building session factory Initial SessionFactory creation failed.net.sf.cglib.core.CodeGenerationException:
   java.lang.reflect.InvocationTargetException-->null

   java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle
      (FXMLLoader.java:1440)"

我在Simple Swing应用程序中尝试过相同的Hibernate连接正常工作,但是使用javafx它会显示上面列出的麻烦。

1 个答案:

答案 0 :(得分:0)

我没有使用HibernateUtil类

就这样做了

您可以通过以下代码获取会话。这是我的控制器部分。而且对我来说工人很好

Session session = null;
        Transaction tx = null;
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
List<Sample> results = null;

        try {

            session = sessionFactory.openSession();
            tx = session.beginTransaction();
            Criteria cr = session.createCriteria(Sample.class);
            //cr.add(Restrictions.eq("qualification", "yes"));
            results = cr.list();
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
        }