我是Hibernate和JavaFX的新手。在这里,我试图使用hibernate在POJO类的表中查看我的数据。但每当我运行我的代码时,它会显示以下错误。应用程序启动方法中的异常
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
/D:/workspace_eclipse/PreBiziPos/bin/com/pos/bizipos/controller/CompanyInfoSearch.fxml
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml/javafx.fxml.FXMLLoader.load(Unknown Source)
at com.pos.bizipos.MainApp.start(MainApp.java:22)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
... 1 more
Caused by: java.lang.IllegalStateException: Transaction already active
at org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:52)
at org.hibernate.internal.AbstractSharedSessionContract.beginTransaction(AbstractSharedSessionContract.java:409)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:355)
at com.sun.proxy.$Proxy33.beginTransaction(Unknown Source)
at com.pos.bizipos.controller.CompanyInfoSearchController.getInfo(CompanyInfoSearchController.java:106)
at com.pos.bizipos.controller.CompanyInfoSearchController.initialize(CompanyInfoSearchController.java:95)
这是我的pojo类BPG_COMPANY_INFO.java
package com.pos.bizipos.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import org.hibernate.annotations.GenericGenerator;
@Entity
public class BPG_COMPANY_INFO implements Serializable{
@Id
@GenericGenerator(name = "sequence_company_id", strategy = "com.pos.bizipos.CompanyIdGenerator")
@GeneratedValue(generator = "sequence_company_id")
@Column(length=11)
private String Company_Id;
@Column(length=100)
private String Company_Name;
@Column(length=100)
private String Country_Name;
@Column(length=10)
private int Post_Code;
@Lob
private String Company_Adress;
@Column(length=100)
private String Company_Email;
@Column(length=100)
private String Company_Web;
private int Company_Phone;
@Column(length=100)
private String Company_Division;
@Column(length=100)
private String Company_District;
@Column(length=100)
private String Company_Police_Station;
@Column(length=100)
private String Company_Upazilla;
@Column(length=100)
private String Company_City;
//For all tables the following fields are common
@Column(length=1)
private int Active_Status;
@Lob
private String Remarks;
@Column(length=12)
private int Entered_By;
private Date Entry_Timestap;
@Column(length=12)
private int Updated_By;
private Date Update_Timestap;
@Column(length=500)
private String Flex1;
@Column(length=12)
private String Flex2;
@Column(length=12)
private String Flex3;
@Column(length=12)
private String Flex4;
@Column(length=12)
private String Flex5;
public String getCompany_City() {
return Company_City;
}
public void setCompany_City(String company_City) {
Company_City = company_City;
}
public int getActive_Status() {
return Active_Status;
}
public void setActive_Status(int active_Status) {
Active_Status = active_Status;
}
public String getRemarks() {
return Remarks;
}
public void setRemarks(String remarks) {
Remarks = remarks;
}
public int getEntered_By() {
return Entered_By;
}
public void setEntered_By(int entered_By) {
Entered_By = entered_By;
}
public Date getEntry_Timestap() {
return Entry_Timestap;
}
public void setEntry_Timestap(Date entry_Timestap) {
Entry_Timestap = entry_Timestap;
}
public int getUpdated_By() {
return Updated_By;
}
public void setUpdated_By(int updated_By) {
Updated_By = updated_By;
}
public Date getUpdate_Timestap() {
return Update_Timestap;
}
public void setUpdate_Timestap(Date update_Timestap) {
Update_Timestap = update_Timestap;
}
public String getFlex1() {
return Flex1;
}
public void setFlex1(String flex1) {
Flex1 = flex1;
}
public String getFlex2() {
return Flex2;
}
public void setFlex2(String flex2) {
Flex2 = flex2;
}
public String getFlex3() {
return Flex3;
}
public void setFlex3(String flex3) {
Flex3 = flex3;
}
public String getFlex4() {
return Flex4;
}
public void setFlex4(String flex4) {
Flex4 = flex4;
}
public String getFlex5() {
return Flex5;
}
public void setFlex5(String flex5) {
Flex5 = flex5;
}
public String getCompany_Division() {
return Company_Division;
}
public void setCompany_Division(String company_Division) {
Company_Division = company_Division;
}
public String getCompany_District() {
return Company_District;
}
public void setCompany_District(String company_District) {
Company_District = company_District;
}
public String getCompany_Police_Station() {
return Company_Police_Station;
}
public void setCompany_Police_Station(String company_Police_Station) {
Company_Police_Station = company_Police_Station;
}
public String getCompany_Upazilla() {
return Company_Upazilla;
}
public void setCompany_Upazilla(String company_Upazilla) {
Company_Upazilla = company_Upazilla;
}
public BPG_COMPANY_INFO() {
}
public BPG_COMPANY_INFO(String id,String name,String country,int postcode,String adress,String email,String web,int phone,String div,String dist,String upazilla,String pstation) {
this.Company_Id=id;
this.Company_Name=name;
this.Country_Name=country;
this.Post_Code=postcode;
this.Company_Adress=adress;
this.Company_Email=email;
this.Company_Web=web;
this.Company_Phone=phone;
this.Company_Division=div;
this.Company_District=dist;
this.Company_Upazilla=upazilla;
this.Company_Police_Station=pstation;
}
public BPG_COMPANY_INFO(String name,String country,int postcode,String adress,String email,String web,int phone,String div,String dist,String upazilla,String pstation) {
this.Company_Name=name;
this.Country_Name=country;
this.Post_Code=postcode;
this.Company_Adress=adress;
this.Company_Email=email;
this.Company_Web=web;
this.Company_Phone=phone;
this.Company_Division=div;
this.Company_District=dist;
this.Company_Upazilla=upazilla;
this.Company_Police_Station=pstation;
}
public String getCompany_Id() {
return Company_Id;
}
public void setCompany_Id(String company_Id) {
Company_Id = company_Id;
}
public String getCompany_Name() {
return Company_Name;
}
public void setCompany_Name(String company_Name) {
Company_Name = company_Name;
}
public String getCountry_Name() {
return Country_Name;
}
public void setCountry_Name(String country_Name) {
Country_Name = country_Name;
}
public int getPost_Code() {
return Post_Code;
}
public void setPost_Code(int post_Code) {
Post_Code = post_Code;
}
public String getCompany_Adress() {
return Company_Adress;
}
public void setCompany_Adress(String company_Adress) {
Company_Adress = company_Adress;
}
public String getCompany_Email() {
return Company_Email;
}
public void setCompany_Email(String company_Email) {
Company_Email = company_Email;
}
public String getCompany_Web() {
return Company_Web;
}
public void setCompany_Web(String company_Web) {
Company_Web = company_Web;
}
public int getCompany_Phone() {
return Company_Phone;
}
public void setCompany_Phone(int company_Phone) {
Company_Phone = company_Phone;
}
}
这是我的CompanyInfoSearch.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="500.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.pos.bizipos.controller.CompanyInfoSearchController">
<children>
<VBox layoutX="46.0" layoutY="148.0" prefHeight="600.0" prefWidth="700.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<HBox prefHeight="0.0" prefWidth="680.0" spacing="5.0">
<children>
<Label text="Company Details">
<font>
<Font name="System Bold" size="18.0" />
</font>
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</HBox.margin>
</Label>
<Region prefHeight="200.0" prefWidth="200.0" HBox.hgrow="ALWAYS" />
<Button fx:id="back_button" mnemonicParsing="false" text="Back">
<HBox.margin>
<Insets bottom="10.0" left="5.0" right="5.0" top="10.0" />
</HBox.margin>
</Button>
<Button fx:id="Show_button" mnemonicParsing="false" text="Show">
<HBox.margin>
<Insets bottom="10.0" left="5.0" right="5.0" top="10.0" />
</HBox.margin>
</Button>
</children>
<VBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</VBox.margin>
</HBox>
<TableView fx:id="table" editable="true">
<columns>
<TableColumn fx:id="col_id" prefWidth="40.0" text="ID" />
<TableColumn fx:id="col_name" prefWidth="57.0" text="Name" />
<TableColumn fx:id="col_country" prefWidth="71.0" text="Country" />
<TableColumn fx:id="col_division" prefWidth="55.0" text="Division" />
<TableColumn fx:id="col_city" prefWidth="62.0" text="City" />
<TableColumn fx:id="col_distict" prefWidth="62.0" text="District" />
<TableColumn fx:id="col_upazilla" prefWidth="68.0" text="Upazilla" />
<TableColumn fx:id="col_pstation" maxWidth="500.0" prefWidth="91.0" text="Police Station" />
<TableColumn fx:id="col_post" prefWidth="72.0" text="Postcode" />
<TableColumn fx:id="col_mail" prefWidth="61.0" text="Mail" />
<TableColumn fx:id="col_web" prefWidth="68.0" text="Web" />
<TableColumn fx:id="col_phone" prefWidth="89.0" text="Phone" />
</columns>
</TableView>
<Button mnemonicParsing="false" text="Button" />
</children>
</VBox>
</children>
</AnchorPane>
这是我的控制器类
package com.pos.bizipos.controller;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.transform.Transformers;
import com.pos.bizipos.HibernateUtil;
import com.pos.bizipos.entity.BPG_COMPANY_INFO;
import com.pos.bizipos.interfaces.CompanyInterface;
import com.pos.bizipos.model.BPG_COMPANY_INFOmodel;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
public class CompanyInfoSearchController implements Initializable,CompanyInterface {
@FXML
private TableColumn<BPG_COMPANY_INFO, String> col_id;
@FXML
private TableColumn<BPG_COMPANY_INFO, String> col_upazilla;
@FXML
private TableColumn<BPG_COMPANY_INFO, String> col_web;
@FXML
private TableColumn<BPG_COMPANY_INFO, String> col_name;
@FXML
private TableColumn<BPG_COMPANY_INFO, String> col_distict;
@FXML
private Button back_button;
@FXML
private TableColumn<BPG_COMPANY_INFO, String> col_pstation;
@FXML
private Button Show_button;
@FXML
private TableColumn<BPG_COMPANY_INFO, String> col_division;
@FXML
private TableColumn<BPG_COMPANY_INFO, String> col_city;
@FXML
private TableColumn<BPG_COMPANY_INFO, String> col_mail;
@FXML
private TableColumn<BPG_COMPANY_INFO, String> col_country;
@FXML
private TableColumn<BPG_COMPANY_INFO, Integer> col_post;
@FXML
private TableColumn<BPG_COMPANY_INFO, Integer> col_phone;
@FXML
private TableView<BPG_COMPANY_INFO> table;
private BPG_COMPANY_INFOmodel model;
private Session session;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
col_id.setCellValueFactory(new PropertyValueFactory<BPG_COMPANY_INFO, String>("Company_Id"));
col_name.setCellValueFactory(new PropertyValueFactory<BPG_COMPANY_INFO, String>("Company_Name"));
col_country.setCellValueFactory(new PropertyValueFactory<BPG_COMPANY_INFO, String>("Company_Country"));
col_division.setCellValueFactory(new PropertyValueFactory<BPG_COMPANY_INFO, String>("Company_Division"));
col_distict.setCellValueFactory(new PropertyValueFactory<BPG_COMPANY_INFO, String>("Company_District"));
col_mail.setCellValueFactory(new PropertyValueFactory<BPG_COMPANY_INFO, String>("Company_Mail"));
col_web.setCellValueFactory(new PropertyValueFactory<BPG_COMPANY_INFO, String>("Company_Web"));
col_phone.setCellValueFactory(new PropertyValueFactory<BPG_COMPANY_INFO, Integer>("Company_Phone"));
col_pstation.setCellValueFactory(new PropertyValueFactory<BPG_COMPANY_INFO, String>("Company_Police_Station"));
col_post.setCellValueFactory(new PropertyValueFactory<BPG_COMPANY_INFO, Integer>("Company_Post_Code"));
col_upazilla.setCellValueFactory(new PropertyValueFactory<BPG_COMPANY_INFO, String>("Company_Upazilla"));
col_city.setCellValueFactory(new PropertyValueFactory<BPG_COMPANY_INFO, String>("Company_City"));
table.setItems(getInfo());
}
private ObservableList<BPG_COMPANY_INFO> getInfo() {
// TODO Auto-generated method stub
ObservableList<BPG_COMPANY_INFO> list = FXCollections.observableArrayList();
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<BPG_COMPANY_INFO> company = session.createQuery("from BPG_COMPANY_INFO").list();
session.beginTransaction().commit();
company.stream().forEach(list::add);
return list;
}
}
这是我的MainApp.java
package com.pos.bizipos;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class MainApp extends Application {
private double xOffset=0;
private double yOffset=0;
@Override
public void start(Stage window) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/com/pos/bizipos/controller/CompanyInfoSearch.fxml"));
root.setOnMousePressed((MouseEvent event) -> {
xOffset = event.getSceneX();
yOffset = event.getSceneY();
});
/* root.setOnMousePressed((MouseEvent event) -> {
window.setX(event.getScreenX() - xOffset);
window.setY(event.getScreenY() - yOffset);
});*/
Scene scene = new Scene(root);
window.setTitle("BiziPOS:: Version 1.0");
// window.getIcons().add(new Image("/images/logo.png"));
// window.initStyle(StageStyle.UNDECORATED);
window.setScene(scene);
window.show();
}
public static void main(String [] args) {
if(HibernateUtil.setSessionFactory())
launch(args);
else
{
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("An error has occured!");
alert.setHeaderText("Database Connection Error!");
alert.setContentText("Please contact the developer");
alert.showAndWait();
Platform.exit();
});
}
}
}
任何人都可以帮助我吗?