【问题标题】:Hibernate - Error accessing stax stream - with hibernate.propertiesHibernate - 访问 stax 流时出错 - 使用 hibernate.properties
【发布时间】:2021-06-26 15:17:10
【问题描述】:

我收到此错误:

信息:HHH000205:从资源 hibernate.properties 加载的属性:{hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver, hibernate.dialect=org.hibernate.dialect.Oracle8iDialect, hibernate.connection.password=** **, hibernate.connection.username=myUserName, hibernate.connection.url=jdbc:oracle:thin:@//myHost:1521/mySID, hibernate.bytecode.use_reflection_optimizer=false, show_sql=true}

org.hibernate.HibernateException:访问 stax 流时出错 在 org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:107) 在 org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65) 在 org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57) 在 org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:165) 在 org.hibernate.cfg.Configuration.configure(Configuration.java:258) 在 gradletests.HibernateUtils.getSessionFactory(HibernateUtils.java:15) 在 gradlettests.MainTest.main(MainTest.java:14) 引起:javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1] 消息:prolog 中不允许有内容。 在 java.xml/com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:652) 在 java.xml/com.sun.xml.internal.stream.XMLEventReaderImpl.peek(XMLEventReaderImpl.java:277) 在 org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:103) ... 6 更多

在尝试打开这样的会话时:

private static SessionFactory factory;
private static boolean isInitialized = false;
public static synchronized SessionFactory getSessionFactory() {
    if (!isInitialized) {
        factory = new Configuration().configure("hibernate.properties").
                addAnnotatedClass(Myclass.class).
                buildSessionFactory();
        isInitialized = true;
    }
    return factory;
}

显然,我的属性文件是关于输出读取的:

信息:HHH000205:从资源 hibernate.properties 加载的属性:{hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver, hibernate.dialect=org.hibernate.dialect.Oracle8iDialect, hibernate.connection.password=****, hibernate.connection.username=myUserName, hibernate.connection.url=jdbc:oracle:thin:@//myHost:1521/mySID, hibernate.bytecode.use_reflection_optimizer=false, show_sql=true}

hibernate.properties 看起来像这样:

hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
hibernate.connection.url=jdbc:oracle:thin:@//myHost:1521/mySID
hibernate.connection.username=myUserName
hibernate.connection.password=myPassword
hibernate.dialect=org.hibernate.dialect.Oracle8iDialect
show_sql=true

我的 build.gradle 导入那些:

implementation group: 'org.springframework',        name: 'spring-context',     version: '5.1.4.RELEASE'
implementation group: 'org.springframework',        name: 'spring-orm',         version: '5.1.4.RELEASE'
implementation group: 'org.springframework.data',   name: 'spring-data-jpa',    version: '2.1.4.RELEASE'
implementation group: 'org.hibernate',              name: 'hibernate-core',     version: '5.4.1.Final'
implementation group: 'com.oracle.database.jdbc',   name: 'ojdbc8',             version: '21.1.0.0'

第二部分的错误:

原因:javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1] 消息:prolog 中不允许内容。在 java.xml/com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:652) 在 java.xml/com.sun.xml.internal.stream.XMLEventReaderImpl.peek(XMLEventReaderImpl. java:277) 在 org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:103) ... 6 更多

指的是文件的行和列,它是hibernate.properties,我知道是因为如果我在文件的开头添加一个返回,它会导致第2行和第1列而不是1,1 .

我不明白,因为它似乎第一次正确读取文件,然后像在等待 xml 格式一样崩溃。

我以为我没有正确加载班级中的属性文件,但我没有找到有关如何加载它的任何信息。有什么想法吗?

【问题讨论】:

    标签: java hibernate jpa configuration


    【解决方案1】:

    hibernate.properties 文件会自动应用,您无需加载它。除此之外,如果您查看方法 Configuration#configure 的 Java Doc,您会发现它需要 XML 文件的路径。如果你想加载其他属性,你必须自己加载这些属性Properties#load添加使用方法Configuration#mergeProperties添加它们

    【讨论】:

    • 显然我必须定义一个xml文件。我虽然可以避免这些...我最终决定使用它们而不是属性文件,即使讨厌它们...谢谢您的时间,很抱歉我抽出时间回复。
    猜你喜欢
    • 1970-01-01
    • 2016-04-04
    • 2019-01-02
    • 2016-10-01
    • 2020-03-29
    • 1970-01-01
    • 2016-01-13
    • 2011-08-24
    相关资源
    最近更新 更多