【问题标题】:Unable to perform unmarshalling at line number 0 and column 0 in RESOURCE hibernate.cfg.xml. Message: null无法在 RESOURCE hibernate.cfg.xml 中的第 0 行和第 0 列执行解组。消息:空
【发布时间】:2023-03-16 20:09:01
【问题描述】:

这是我的配置文件 hibernate.cfg.xml :

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>


  <!-- Database connection settings -->
  <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="connection.url">jdbc:mysql://localhost:3306/bdd_disc?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT</property>
  <property name="connection.username">root</property>
  <property name="connection.password">MP18711922</property>

  <!-- JDBC connection pool (use the built-in) -->
  <property name="connection.pool_size">1</property>

  <!-- SQL dialect -->
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

  <!-- Enable Hibernate's automatic session context management -->
  <property name="current_session_context_class">thread</property>

  <!-- Disable the second-level cache -->
  <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

  <!-- Echo all executed SQL to stdout -->
  <property name="show_sql">true</property>

  <mapping class="org.o7planning.tutorial.hibernate.entities.Artists" />
  <mapping class="org.o7planning.tutorial.hibernate.entities.Disc" />
  <mapping class="org.o7planning.tutorial.hibernate.entities.Song" />

 </session-factory>

</hibernate-configuration>

以及获取 SessionFactory 的类:

 public class HibernateUtils {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    // Hibernate 5:
    private static SessionFactory buildSessionFactory() {
        try {
            // Create the ServiceRegistry from hibernate.cfg.xml
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()//
                    .configure("hibernate.cfg.xml").build();

            // Create a metadata sources using the specified service registry.
            Metadata metadata = new MetadataSources(serviceRegistry).getMetadataBuilder().build();

            return metadata.getSessionFactoryBuilder().build();
        } catch (Throwable ex) {

            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }

}

我收到错误:

原因:org.hibernate.internal.util.config.ConfigurationException:无法在 RESOURCE hibernate.cfg.xml 中的第 0 行和第 0 列执行解组。消息:空

主要工作的班级是:

私有静态 void getTagsFromFile(File f) {

    String artist = null;
    String disc = null;
    String song = null;
    Tag tag = null;
    AudioFile af;
    Artists artistInBase = null;
    Disc discInBase = null;
    Song songInBase = null;

     SessionFactory factory = HibernateUtils.getSessionFactory();
     Session session = factory.getCurrentSession();
       try {

           // All the action with DB via Hibernate
           // must be located in one transaction.
           // Start Transaction.            
           session.getTransaction().begin();

           af = AudioFileIO.read(f);
           tag = af.getTag();
           artist = tag.getFirst(FieldKey.ARTIST);
           artist = artist.replaceAll("'", " ");
           artist = artist.replaceAll("\"", " ");
           disc = tag.getFirst(FieldKey.ALBUM);
           disc = disc.replaceAll("'", " ");
           disc = disc.replaceAll("\"", "'");
           song = tag.getFirst(FieldKey.TITLE);
           song = song.replaceAll("\"", "'");

           int idArtist = -1;
           int idDisc = -1;

           Transaction tx = null;
           try {
               tx = session.beginTransaction();
               String queryAsString = "SELECT artists.name FROM artists WHERE          artists.name LIKE " + "'" + artist + "'";
           Query<Artists> query = session.createQuery(queryAsString);
           List<Artists> artists = query.getResultList();

          if (artists.isEmpty()) {
            artistInBase = new Artists(artist);
            session.save(artistInBase);
            session.flush();
            idArtist = artistInBase.getId();
          }
          else {
            artistInBase = artists.get(0);
            idArtist = artistInBase.getId();
        }
            tx.commit();
        }

        catch (Exception e) {
            if (tx != null)
                tx.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    } catch (CannotReadException | IOException | TagException | ReadOnlyFileException
            | InvalidAudioFrameException e) {
        System.err.println("Cannot parse " + f.getName());
    }

}

【问题讨论】:

  • 我也收到: 引起:javax.xml.stream.XMLStreamException: ParseError at [row,col]:[13,109] 消息:La référence à l'entité "characterEncoding" doit se terminer par le分隔符';'。

标签: hibernate


【解决方案1】:

我在 Maven 构建生命周期中遇到了类似的问题。如here 所述,我使用了maven 插件maven-processor-plugin。 maven 日志包含以下warning

[INFO] --- maven-processor-plugin:3.3.3:process (process) @ orm-project ---
[INFO] diagnostic: Note: Hibernate JPA 2 Static-Metamodel Generator 5.4.12.Final
[WARNING] diagnostic: warning: Unable to parse persistence.xml: Unable to perform unmarshalling at line number 0 and column 0. Message: null

切换到 maven 3.6.3 和 JDK 11 后,我删除了 maven-processor-plugin 并将 hibernate-jpamodelgen 添加到依赖项部分。这解决了warning。静态元模型类仍在生成。

【讨论】:

    【解决方案2】:

    好的,我明白了,我的 pom.xml 有不同版本的 hibernate。现在可以了。

    【讨论】:

    • 你能展示一下你pom.xml的样子吗?我遇到了同样的错误
    猜你喜欢
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 2017-05-22
    • 2014-02-21
    • 1970-01-01
    • 2016-07-29
    • 2013-05-06
    相关资源
    最近更新 更多