Hibernate – Error accessing stax stream – with hibernate.properties

Tags: , , ,



I’m getting this error :

INFO: HHH000205: Loaded properties from resource 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: Error accessing stax stream at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:107) at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65) at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57) at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:165) at org.hibernate.cfg.Configuration.configure(Configuration.java:258) at gradletests.HibernateUtils.getSessionFactory(HibernateUtils.java:15) at gradletests.MainTest.main(MainTest.java:14) Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1] Message: Content is not allowed in prolog. at java.xml/com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:652) at java.xml/com.sun.xml.internal.stream.XMLEventReaderImpl.peek(XMLEventReaderImpl.java:277) at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:103) … 6 more

while trying to open a session like this :

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;
}

Obviously, my property file is read regarding the output :

INFO: HHH000205: Loaded properties from resource 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 looks like this :

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

And my build.gradle import those :

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'

The second part of the error :

Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1] Message: Content is not allowed in prolog. at java.xml/com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:652) at java.xml/com.sun.xml.internal.stream.XMLEventReaderImpl.peek(XMLEventReaderImpl.java:277) at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:103) … 6 more

is refering to a row and a col of a file, it’s the hibernate.properties, I know it because if I add a return to the beginning of the file, it results with row 2 and col 1 instead of 1,1.

I don’t understand because it seems to read the file correctly a first time and then crash like it’s waiting for an xml format.

I thought I didnt load correctly the property file in my class but I didnt find anything about how I should load it. Any idea ?

Answer

The hibernate.properties file is applied automatically, you don’t need to load it. Apart from that, if you look at the Java Doc of the method Configuration#configure you will see that it expects the path to a XML file. If you want to load other properties, you have to load these properties yourself with Properties#load add add them with the method Configuration#mergeProperties



Source: stackoverflow