【问题标题】:Javafx, Maven, Hibernate, Unable to perform unmarshalling at line number 0 and column 0Javafx、Maven、Hibernate、无法在第 0 行和第 0 列执行解组
【发布时间】:2019-12-21 19:58:55
【问题描述】:

我正在尝试使用 maven 和 hibernate 编写 javafx 项目。当我运行应用程序时,出现以下错误:

Unable to perform unmarshalling at line number 0 and column 0 in RESOURCE hibernate.cfg.xml. Message: null.
   org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 0 and column 0 in RESOURCE hibernate.cfg.xml. Message: null
java.lang.NullPointerException
    at org.openjfx.mavenfx/org.openjfx.mavenfx.LoginScene.lambda$0(LoginScene.java:48)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8890)
    at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
    at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
    at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3862)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2590)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:830)

pom.xml 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema- 
instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.openjfx</groupId>
<artifactId>mavenfx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>


    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13-rc-2</version>
        <scope>test</scope>
    </dependency>


    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>13</version>
    </dependency>

    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>13</version>
    </dependency>


    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.18</version>
    </dependency>



    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.4.9.Final</version>
    </dependency>


    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.4.9.Final</version>
    </dependency>


    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.2.Final</version>
    </dependency>




    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>2.3.0</version>
    </dependency>


    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.0</version>
    </dependency>

    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>


    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
    </dependency>



</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <release>11</release>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.3</version>
            <configuration>
                <mainClass>org.openjfx.mavenfx.App</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>
</project> 

hibernate.cfg.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
    <session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/mavenfxdatabase? 
    useTimezone=true&serverTimezone=UTC</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

    <mapping class="org.openjfx.mavenfx.user.User" />

    </session-factory>
    </hibernate-configuration>

用户.java:

@Entity
    @Table(name="users")
    public class User implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="idUsers")
private int id;

@Column(name="userName")
private String UserName;

@Column(name="password")
private String Password;

@Column(name="email")
private String Email;


public User(){

}

public User(String UserName, String Password, String Email) {
    this.UserName= UserName;
    this.Email=Email;
    this.Password = Password;
}

public String getUserName() {
    return UserName;
}

public void setUserName(String UserName) {
    this.UserName = UserName;
}

public String getPassword() {
    return Password;
}

public void setPassword(String Password) {
    this.Password = Password;
}

public String getEmail() {
    return Email;
}

public void setEmail(String Email) {
    this.Email = Email;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}
    }

休眠会话工厂类:

public class HibernateSession {

    private static SessionFactory sessionFactory;

    private HibernateSession() {}

    public static SessionFactory getSession()  {
    if(sessionFactory==null) {
        try {
        Configuration configuration = new Configuration().configure();

        sessionFactory=configuration.buildSessionFactory();

        }catch(Exception e) {
            System.out.println(e);
        }
    }
    return sessionFactory;
    }

    public static void closeSession() throws Exception {
    getSession().close();
    }


    }

模块信息文件:

  module org.openjfx.mavenfx {
    requires javafx.controls;
    requires javafx.fxml;
    requires javafx.graphics;
    requires javafx.base;
    requires org.hibernate.orm.core;
    requires java.persistence;
    requires java.logging;
    requires java.sql;
    requires java.xml.bind;

    opens org.openjfx.mavenfx; 

    }

负责将数据放入数据库的部分代码:

Session session = HibernateSession.getSession().openSession();
        session.beginTransaction();
        User user = new User();
        user.setUserName(tfname.getText());
        user.setPassword(pf.getText());
        user.setEmail(tfemail.getText());
        session.save(user);
        session.getTransaction().commit();
        HibernateSession.closeSession();

我已经尝试过其他类似问题的解决方案。提前感谢您的帮助。

【问题讨论】:

    标签: java hibernate maven javafx


    【解决方案1】:

    您的 hibernate.cfg.xml 不是有效的 XML。

    网址中有一个&amp;amp;,您必须使用&amp;amp; 转义

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/mavenfxdatabase? 
    useTimezone=true&amp;serverTimezone=UTC</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password">root</property>
            <property name="hibernate.show_sql">true</property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    
            <mapping class="org.openjfx.mavenfx.user.User" />
    
         </session-factory>
    </hibernate-configuration>
    

    【讨论】:

      【解决方案2】:

      谢谢,它成功了,但是在那之后我又遇到了另一个错误:

      org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found : org/openjfx/mavenfx/user/User.hbm.xml : origin(org/openjfx/mavenfx/user/User.hbm.xml)
      

      java.lang.NullPointerException 在 org.openjfx.mavenfx/org.openjfx.mavenfx.LoginScene.lambda$0(LoginScene.java:48) 在 javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) 在 javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 在 javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 在 javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 在 javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) 在 javafx.base/javafx.event.Event.fireEvent(Event.java:198) 在 javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8890) 在 javafx.controls/javafx.scene.control.Button.fire(Button.java:203) 在 javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206) 在 javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274) 在 javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) 在 javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) 在 javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 在 javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 在 javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在 javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 在 javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) 在 javafx.base/javafx.event.Event.fireEvent(Event.java:198) 在 javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3862) 在 javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849) 在 javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2590) 在 javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409) 在 javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299) 在 java.base/java.security.AccessController.doPrivileged(AccessController.java:391) 在 javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447) 在 javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412) 在 javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446) 在 javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556) 在 javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942) 在 javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(本机方法) 在 javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174) 在 java.base/java.lang.Thread.run(Thread.java:830)

      在用户类中我导入了 javax.persistence.*;

      我还更改了 HibernateSession 类:

      public class HibernateSession {
      
      private static SessionFactory sessionFactory;
      
      private HibernateSession() {}
      
      public static SessionFactory createSessionFactory()  {
          if(sessionFactory==null) {
              try {
              Configuration configuration = new Configuration().configure();
              configuration.addClass(User.class);
              StandardServiceRegistryBuilder registryBuilder = new 
         StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
              sessionFactory=configuration.buildSessionFactory(registryBuilder.build());
      
              }catch(Exception e) {
                  System.out.println(e);
              }
          }
          return sessionFactory;
          }
      
          public static void closeSession() throws Exception {
          createSessionFactory().close();
          }   
        }
      

      和hibernate.cfg.xml文件

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 
      3.0//EN" "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
      <hibernate-configuration>
      <session-factory>
          <property 
      name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
          <property 
      name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/mavenfxdatabase? 
      useTimezone=true&amp;serverTimezone=UTC</property>
          <property name="hibernate.connection.username">root</property>
          <property name="hibernate.connection.password">root</property>
          <property name="hibernate.show_sql">true</property>
          <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
          <property name="current_session_context_class">thread</property>
          <property name="hbm2ddl.auto">create</property>
      
          <mapping class="org.openjfx.mavenfx.user.User"/>
      
      </session-factory>
      </hibernate-configuration>
      

      是否可以在不创建 hbm.xml 文件的情况下运行应用程序?

      【讨论】:

        猜你喜欢
        • 2023-03-16
        • 2017-02-13
        • 1970-01-01
        • 2017-05-22
        • 2014-02-21
        • 2016-07-29
        • 2013-05-06
        • 2020-06-13
        相关资源
        最近更新 更多