【问题标题】:Build with Hibernate is not working (only Hibernate)使用 Hibernate 构建不起作用(仅 Hibernate)
【发布时间】:2016-10-24 14:37:52
【问题描述】:

我使用 JavaFx 和 Hibernate 在 Java 中创建了一个应用程序。 在我的 IDE 中一切正常。但是当我构建应用程序并启动 .jar 文件时,所有使用 Hibernate 的功能都不起作用。

我创建了另一个 testapp 来向您展示我的代码:

这是我的主要课程:

package testapp;

import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class Testapp extends Application {

    @Override
    public void start(Stage primaryStage) {

        ComboBox<Test> cmbtest=new ComboBox<>();
        ObservableList<Test> l=FXCollections.observableArrayList(loadCmb());     
        StackPane root = new StackPane();      
        root.getChildren().add(cmbtest);
        cmbtest.setItems(l);
        Scene scene = new Scene(root, 300, 250);        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    public List<Test> loadCmb(){
        List<Test> list;
        SessionFactory sf;
        sf= NewHibernateUtil.getSessionFactory();       
        Session session = sf.openSession();
        Query query=session.createQuery("FROM Test test");        
        list=query.list();
        return list;   
        }

}
<?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.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?zeroDateTimeBehavior=convertToNull</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">****</property>
    <mapping resource="testapp/Test.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

这是我的 NewHibernateUtil:

package testapp;

import java.io.File;
import javax.imageio.spi.ServiceRegistry;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class NewHibernateUtil {

    private static SessionFactory sf;
    private static ServiceRegistry serviceRegistrry;    

    static {
        try {   
             File file=new File("src/testapp/hibernate.cfg.xml");
            Configuration cfg= new Configuration().configure(file);            
            StandardServiceRegistryBuilder sb= new StandardServiceRegistryBuilder();
            sb.applySettings(cfg.getProperties());
            StandardServiceRegistry standardServiceRegistry= sb.build();
            sf = cfg.buildSessionFactory(standardServiceRegistry);
        } catch (Throwable ex) {            
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

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

这是我唯一的课程,由我在 MySql 中的数据库创建

package testapp;
// Generated 24/10/2016 01:53:10 PM by Hibernate Tools 4.3.1



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


     private String name;
     private Integer number;

    public Test() {
    }


    public Test(String name) {
        this.name = name;
    }
    public Test(String name, Integer number) {
       this.name = name;
       this.number = number;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public Integer getNumber() {
        return this.number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }
}

mySql 是一张有 2 列的表 测试表: 姓名和号码

如果我在我的 IDE 中运行此程序,则应用程序运行正常,但是当我构建应用程序时,.jar 文件无法正常工作。

这是我构建的输出:

ant -f C:\\Users\\Dani-Fla-Mathi\\Documents\\NetBeansProjects\\testapp jfx-rebuild
init:
deps-clean:
Created dir: \testapp\build
Updating property file: \testapp\build\built-clean.properties
Deleting directory \testapp\build
clean:
init:
deps-jar:
Created dir: \testapp\build
Updating property file: \testapp\build\built-jar.properties
Created dir: \testapp\build\classes
Created dir: \testapp\build\empty
Created dir: \testapp\build\generated-sources\ap-source-output
Compiling 3 source files to \testapp\build\classes
Note:\testapp\src\testapp\Testapp.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Copying 3 files to \testapp\build\classes
compile:
Created dir: \testapp\dist
Copying 17 files to \testapp\dist\lib
Detected JavaFX Ant API version 1.3
Launching <fx:jar> task from C:\Program Files\Java\jdk1.8.0_05\jre\..\lib\ant-javafx.jar
Warning: From JDK7u25 the Codebase manifest attribute should be used to restrict JAR repurposing.
         Please set manifest.custom.codebase property to override the current default non-secure value '*'.
Launching <fx:deploy> task from C:\Program Files\Java\jdk1.8.0_05\jre\..\lib\ant-javafx.jar
jfx-deployment-script:
jfx-deployment:
jar:
jfx-rebuild:
BUILD SUCCESSFUL (total time: 2 seconds)

如果您需要更多信息,请告诉我

【问题讨论】:

  • 抱歉这个 build* using*
  • 没有任何代码,没有错误消息,有人怎么能推荐你缺少的东西?
  • 任何人都需要相当多的信息才能回答这个问题(而不仅仅是猜测大量可能出错的事情)。请阅读stackoverflow.com/help/how-to-ask 并尝试根据那里的指南来构建您的问题。
  • 我假设他没有创建正确的类路径,只是运行 javafx 应用程序的 jar。有多种选择。阅读以下内容很可能是个好主意docs.oracle.com/javase/8/docs/technotes/tools/windows/…
  • @DimaSan 这是问题所在。我在应用程序中没有收到任何错误。例如。我有一个打开场景的按钮。在这个场景中,我有一个组合框,其中填充了我的数据库中的一些信息。当我单击此按钮时,场景未打开

标签: java hibernate javafx build netbeans-8


【解决方案1】:

您正在从一个文件加载休眠配置,该文件的路径是相对于当前工作目录指定的:

File file=new File("src/testapp/hibernate.cfg.xml");
Configuration cfg= new Configuration().configure(file);  

显然,当您将应用程序部署为 jar 文件时,此文件路径将不再有意义(除此之外,src 文件夹将不会包含在 jar 文件中,并且不会在其他位置可用运行)。

您应该将配置文件指定为资源,并提供 URL:

URL resource = NewHibernateUtil.class.getResource("hibernate.cfg.xml");
Configuration cfg= new Configuration().configure(resource);            

假设其他所有内容都已正确部署,即配置文件已部署到 jar 文件、hibernate jar 位于类路径等,这应该可以解决问题。

【讨论】:

  • 谢谢。那是错误。现在工作正常。
  • @DanielNacher 不客气。请将答案标记为正确:如果有类似问题,它将帮助其他人找到它。
猜你喜欢
  • 2018-10-22
  • 2015-03-17
  • 2015-10-14
  • 2014-09-27
  • 2017-06-03
  • 2013-09-18
  • 2015-07-14
  • 2014-12-01
  • 1970-01-01
相关资源
最近更新 更多