gpt4 book ai didi

java - springboot + hibernate + javaFX

转载 作者:行者123 更新时间:2023-11-29 04:34:21 25 4
gpt4 key购买 nike

我被困在这个问题上好几天了,一无所获。这是交易..我正在构建一个使用 springboot(主要是使用 crudeRepository)与 javafx 和 hibernate 的应用程序。

但我一直收到这个错误:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
... 52 more
Caused by: java.lang.NullPointerException
at br.jus.trt9.PjeMonitor.view.MonitorPjeController.mouseEvent(MonitorPjeController.java:153)
... 62 more

发生这种情况是因为 crudrepository clientRepo 为空,不知道为什么......在正常执行中它应该有这样的东西:

org.springframework.data.jpa.repository.support.SimpleJpaRepository@5a0a3243

有人知道为什么吗?提前谢谢你。

ClientRepo.java 进口...

/*TODO essa classe deve implementar o CrudRepository
que vem do spring-DATA-core
*/
@Repository
public interface ClientRepo extends CrudRepository<Client, Integer> {

}

主程序.java

package br.jus.trt9.PjeMonitor;

imports
...

/*
* @AutoConfigurationPackage
*
* @ComponentScan(basePackages = "br.jus.trt9.PjeMonitor")
*
* @EnableAutoConfiguration
*/

//FIXME esse warning na inicialização
/* Unable to proxy method [public final void br.jus.trt9.PjeMonitor.dao.Ge
nericDao.setClazz(java.lang.Class)] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.*/
@SpringBootApplication
@ComponentScan(basePackages = "br.jus.trt9.PjeMonitor")
public class MainApp extends Application {

private Stage primaryStage;
private BorderPane rootLayout;

private ObservableList<Audiencia> audData = FXCollections.observableArrayList();
private ConfigurableApplicationContext springContext;
private static String[] args;

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

@Override
public void init() throws Exception {
// applicationContext.ge3tAutowireCapableBeanFactory().autowireBean(this);


}

@Override
public void start(Stage primaryStage) {

// primaryStage.setScene(new Scene(rootLayout));
// primaryStage.show();

this.primaryStage = primaryStage;
this.primaryStage.setTitle("Pje Midias Monitor");

initRootLayout();

showTableAudiencia();

}

public void stop() throws Exception {
springContext.close();
}

public MainApp() {

audData.add(new Audiencia("12123123213213", LocalDate.now(), "sim"));
audData.add(new Audiencia("22222222222222", LocalDate.now(), "nao"));
audData.add(new Audiencia("22222222222222", LocalDate.now(), "nao"));
audData.add(new Audiencia("55555555555555", LocalDate.now(), "sim"));
audData.add(new Audiencia("6666666666666", LocalDate.now(), "nao"));
audData.add(new Audiencia("7777777777777", LocalDate.now(), "nao"));
audData.add(new Audiencia("8888888888888", LocalDate.now(), "sim"));
audData.add(new Audiencia("99999999999999", LocalDate.now(), "nao"));
audData.add(new Audiencia("100010110101001", LocalDate.now(), "nao"));

}

/**
* Initializes the root layout.
*/

public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();

// problema do classLoader do javaFX
loader.setClassLoader(this.getClass().getClassLoader());

// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();

} catch (IOException e) {
e.printStackTrace();
}
}

public void showTableAudiencia() {
try {
// Load person overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/visao.fxml"));
AnchorPane TableOverview = (AnchorPane) loader.load();

//loader.setControllerFactory(springContext::getBean);

// Set person overview into the center of root layout.
rootLayout.setCenter(TableOverview);

// Give the controller acckess to the main app.
MonitorPjeController controller = loader.getController();
controller.setMainApp(this);
System.out.println("carregou main app");

} catch (IOException e) {
e.printStackTrace();
}
}

public void showCadastroClients() {

try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/clientcadastro.fxml"));
AnchorPane page = (AnchorPane) loader.load();

// Create the dialog Stage.
Stage dialogStage = new Stage();
dialogStage.setTitle("Cadastro de máquinas");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public Stage getPrimaryStage() {
return primaryStage;
}

public ObservableList<Audiencia> getAudData() {
return audData;
}

public void setAudData(ObservableList<Audiencia> audData) {
this.audData = audData;
}

}

MonitorPjeController.java

package br.jus.trt9.PjeMonitor.view;

import ...;

public class MonitorPjeController {

@FXML
private TableView<Audiencia> audTable;

@FXML
private TableColumn<Audiencia, String> audId;

@FXML
private TableColumn<Audiencia, LocalDate> dataId;

@FXML
private TableColumn<Audiencia, String> assId;

@FXML
private ComboBox<Client> clientcomboBox;

@FXML
private Button BtnListaId = new Button();

@FXML
private Button BtnListarMaq;

private ObservableList<Client> myClientComboBoxData = FXCollections.observableArrayList();

// referencia a aplicacao principal
private MainApp mainapp;

//FIXME
@Autowired
ClientRepo clientRepo;



public MonitorPjeController() {

}

@FXML
private void initialize() {
audId.setCellValueFactory(cellData -> cellData.getValue().processoProperty());
dataId.setCellValueFactory(cellData -> cellData.getValue().dataProperty());
assId.setCellValueFactory(cellData -> cellData.getValue().assinadoProperty());

Client client = new Client("teste123", "21 vt");

// sets the combobox options
myClientComboBoxData.add(client);
myClientComboBoxData.add(new Client("cta122223", "10 vt"));
myClientComboBoxData.add(new Client("cta122223", "17 vt"));
myClientComboBoxData.add(new Client("cta120337", "18 vt"));

clientcomboBox.setItems(myClientComboBoxData);

clientcomboBox.setCellFactory((comboBox) -> {
return new ListCell<Client>() {
@Override
protected void updateItem(Client item, boolean empty) {
super.updateItem(item, empty);

if (item == null || empty) {
setText(null);
} else {
setText(item.getVara());
}
}
};
});

clientcomboBox.setConverter(new StringConverter<Client>() {

@Override
public String toString(Client maquina) {
if (maquina == null) {
return null;
} else {
return maquina.getVara();
}
}

@Override
public Client fromString(String string) {
// TODO Auto-generated method stub
return null;
}
});

}

public void setMainApp(MainApp mainApp) {
this.mainapp = mainApp;

audTable.setItems(mainApp.getAudData());

}


// FIXME nao salva
// TODO em caso de erro enviar msg de erro
@FXML
public void mouseEvent() {
Client client = new Client(1, "cta 12344", "18 vt");
clientRepo.save(client);

}

public ObservableList<Client> getMyClientComboBoxData() {
return myClientComboBoxData;
}

public void setMyClientComboBoxData(ObservableList<Client> myClientComboBoxData) {
this.myClientComboBoxData = myClientComboBoxData;
}

}

正如@minus 所指出的,我创建了一个 springBoot 入口点,但是应用程序启动和退出...我从未看到 javaFX View 。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Lazy;


@SpringBootApplication
public class LaunchClass {

public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);

}

}

最佳答案

您要实现的目标并不像看起来那么容易。

您可以开始阅读this articlethis并从那里继续前进。

事实是javafx和Spring+Hibernate的结合存在几个问题。

最大的问题是范围

Spring 和 Hibernate 主要是为在具有一些明确定义的范围(应用程序、 session 、请求)的 Web 应用程序中工作而开发的。

JavaFX 没有固有范围,您必须创建自定义范围并管理它们。

还有一个重要的细节,即 Spring 和 JafaFX FXMLLoader 都应该在同一个对象( Controller )中注入(inject)字段。

最后但同样重要的是 Hibernate session 的管理和范围界定问题。

这是很多工作,我知道这一点,因为我用 WELD+JavaFX 做了一些非常相似的事情。

关于java - springboot + hibernate + javaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42349185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com