我用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>
这是我的newhbernateutil:
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列的表testTable:姓名和号码
如果我在IDE中运行这个程序,应用程序可以正常运行,但是当我构建应用程序时,.jar文件不能工作。
下面是我构建的输出:
ant -f C:\Users\Dani-Fla-Mathi\Documents\NetBeansProjects\testapp jfx-rebuild
init:
deps-clean:
Created dir: testappbuild
Updating property file: testappbuildbuilt-clean.properties
Deleting directory testappbuild
clean:
init:
deps-jar:
Created dir: testappbuild
Updating property file: testappbuildbuilt-jar.properties
Created dir: testappbuildclasses
Created dir: testappbuildempty
Created dir: testappbuildgenerated-sourcesap-source-output
Compiling 3 source files to testappbuildclasses
Note:testappsrctestappTestapp.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Copying 3 files to testappbuildclasses
compile:
Created dir: testappdist
Copying 17 files to testappdistlib
Detected JavaFX Ant API version 1.3
Launching <fx:jar> task from C:Program FilesJavajdk1.8.0_05jre..libant-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 FilesJavajdk1.8.0_05jre..libant-javafx.jar
jfx-deployment-script:
jfx-deployment:
jar:
jfx-rebuild:
BUILD SUCCESSFUL (total time: 2 seconds)
如果您需要进一步的信息,请告诉我
您正在从一个文件加载hibernate配置,该文件的路径是相对于当前工作目录指定的:
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在类路径上,等等,这应该解决这个问题。