0

I'm new to Hibernate and I'm tring to make a simple project to get familiar with it. I'm using Intellij Idea and SQLite for this project. When I try to run my project I get the following error:

Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 0 and column 0 in RESOURCE hibernate.cfg.xml. Message: null
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:133)
    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:163)
    at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:152)

I've tried several solutions, including this one, yet I can't solve this problem.

This is my code:

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name = "Characters")
public class Character {
    @Id
    private int id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    public Character(){

    }

    public Character(int _id, String _firstName, String _lastName){
        id = _id;
        firstName = _firstName;
        lastName = _lastName;
    }

    public int getId(){
        return id;
    }

    public String getFirstName(){
        return firstName;
    }

    public String getLastName(){
        return lastName;
    }

    public void setId(int _id){
        id = _id;
    }

    public void setFirstName(String _firstName){
        firstName = _firstName;
    }

    public void setLastName(String _lastName){
        lastName = _lastName;
    }

    public String toString(){
        return "{ID: " + id + ", " + "firstName: " + firstName + ", " + "lastName: " + lastName + "}";
    }
}
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class Main {

    public static void main(String[] args){
        Character c1 = new Character(1, "c", "1");
        Character c2 = new Character(2, "c", "2");
        Character c3 = new Character(3, "c", "3");

        final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                .configure() // configures settings from hibernate.cfg1.xml
                .build();
        Metadata meta = new MetadataSources(registry).getMetadataBuilder().build();


        SessionFactory factory = meta.getSessionFactoryBuilder().build();
        Session session = factory.openSession();
        Transaction t = session.beginTransaction();

        session.persist(c1);
        t.commit();
        System.out.println("successfully saved");
        factory.close();
        session.close();

    }

}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>Example</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>18</maven.compiler.source>
        <maven.compiler.target>18</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>6.2.2.Final</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>com.github.gwenn</groupId>
            <artifactId>sqlite-dialect</artifactId>
            <version>0.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.36.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.zsoltfabok</groupId>
            <artifactId>sqlite-dialect</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.4.0-b180830.0359</version>
        </dependency>

        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>4.0.0</version>
        </dependency>

        <!-- Runtime, com.sun.xml.bind module -->
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>4.0.2</version>
        </dependency>
    </dependencies>

</project>

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>
        <property name="hibernate.connection.driver_class">org.sqlite.JDBC</property>
        <property name="hibernate.connection.url">jdbc:sqlite:exampleDB.db</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLiteDialect</property>
        <property name="hibernate.show_sql">true</property>
    </session-factory>
</hibernate-configuration>

EDIT: I tried to delete the BOM as @Jens and @Thomas said, now I get this error:

Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 0 and column 0 in RESOURCE hibernate.cfg.xml. Message: null
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:133)
    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:163)
    at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:152)
    at Main.main(Main.java:17)
Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
 - with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory]
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:269)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:412)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:122)

I don't know if this is the same error or another one.

Billy75
  • 33
  • 5
  • *Unable to perform unmarshalling at line number 0 and column 0 in RESOURCE hibernate.cfg.xml* Looks like there is a non printable invalid character at position 9 in your hibernate config file. Add it with a hex editor and take a look at it – Jens May 09 '23 at 06:32
  • To clarify: I guess Jens mentioning "position 9" is a typo - it would be position 0. A reason might be that you're using UTF-8 with BOM formatting which adds some unprintable chars at the start of the file (this is the BOM - Byte Order Mark). You'd see this as "EF BB BF" in a hex editor - or if using Notepad++ or any other text editor with similar capabilities you'd see the encoding, and you should be able to change it from there (alternatively just remove the offending BOM - it isn't necessary :) ) – Thomas May 09 '23 at 07:08
  • @Thomas thanks for the help! I think I have a different problem now, or maybe this wasn't the solution? – Billy75 May 09 '23 at 16:48
  • Well, the "caused by" you posted indicates a classpath problem. You don't seem to have a Jaxb implementation on your classpath. Google for the message you got and add info on your environment (build tool, appliction server/framework you're using) and you should find good info on how to add Jaxb. – Thomas May 10 '23 at 06:00

0 Answers0