1

I am trying to set up Hibernate in my project to enable ORM-based storage in my database.

Right now I am simply trying to load a configuration in Hibernate to open a session. I figured that this would be an easy test to see if everything worked, after which I could start on the more daunting tasks of actually mapping my classes.

But I didn't realise that just getting to load Hibernate would already be a process that I'd get stuck on for over a day.

Yesterday I couldn't get past an issue with loading the .cfg.xml files. I would continously run into this error:

[15:40:41 ERROR]: [org.bukkit.craftbukkit.v1_19_R1.CraftServer] Unable to perform unmarshalling at line number 0 and column 0 in RESOURCE test.cfg.xml. Message: null initializing LiberCore v0.3 (Is it up to date?)
org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 0 and column 0 in RESOURCE test.cfg.xml. Message: null
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:134) ~[?:?]
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:66) ~[?:?]
    at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57) ~[?:?]
    at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:254) ~[?:?]
    at org.hibernate.cfg.Configuration.configure(Configuration.java:260) ~[?:?]
    at net.libercraft.data.database.HibernateManager.<init>(HibernateManager.java:29) ~[?:?]
    at net.libercraft.data.DataManager.initializeDatabase(DataManager.java:17) ~[?:?]
    at net.libercraft.main.Main.onLoad(Main.java:34) ~[?:?]
    at org.bukkit.craftbukkit.v1_19_R1.CraftServer.loadPlugins(CraftServer.java:460) ~[purpur-1.19.2.jar:git-Purpur-1765]
    at net.minecraft.server.dedicated.DedicatedServer.e(DedicatedServer.java:314) ~[purpur-1.19.2.jar:git-Purpur-1765]
    at net.minecraft.server.MinecraftServer.v(MinecraftServer.java:1142) ~[purpur-1.19.2.jar:git-Purpur-1765]
    at net.minecraft.server.MinecraftServer.lambda$spin$1(MinecraftServer.java:310) ~[purpur-1.19.2.jar:git-Purpur-1765]
    at java.lang.Thread.run(Thread.java:833) [?:?]
Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:278) ~[?:?]
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:421) ~[?:?]
    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:123) ~[?:?]
    ... 12 more
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:445) ~[?:?]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:587) ~[?:?]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[?:?]
    at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:122) ~[?:?]
    at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:155) ~[?:?]
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:276) ~[?:?]
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:421) ~[?:?]
    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:123) ~[?:?]
    ... 12 more

Googling + reading other posts first just pointed me to a bunch of libraries to add because JAXB was no longer included with java by default. So I tried adding the following (different posts pointed to different libraries which made it pretty confusing..):

        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>javax.transaction-api</artifactId>
            <version>1.3</version>
        </dependency>

        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>javax.activation-api</artifactId>
            <version>1.2.0</version>
        </dependency>

        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</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>org.hibernate.common</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>6.0.4.Final</version>
        </dependency>

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

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>

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

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

        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>4.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.25.0-GA</version>
        </dependency>

This didn't change much as I just kept getting the same error.

After a bit of googling, I found something that explained about a jaxb.properties file that I could use to change the package pointing to com.sun.xml.internal.bind.v2.ContextFactory to a package that should be included. This worked for some time, at which point I just got errors that it couldn't find my cfg.xml files. When I tried placing those at a different location in my .jar, I went back to the error above however.

I also read about using a hibernate.properties file instead of using xml. However, I want to include different config files in my jar and select one based on the environment that my application is running in. For that reason I don't want to try just a simple hibernate.properties file, since that won't really allow me to customize it.

Today I decided to take a different route. Instead of providing the configuration via the xml file, I would just enter the configuration programmatically. This has solved the previous error of not being able to process the xml files.

But now I am getting new errors that I am unable to solve.

[09:22:53 ERROR]: [org.bukkit.craftbukkit.v1_19_R1.CraftServer] com/fasterxml/classmate/TypeResolver initializing LiberCore v0.3 (Is it up to date?)
java.lang.NoClassDefFoundError: com/fasterxml/classmate/TypeResolver
    at org.hibernate.boot.internal.ClassmateContext.<init>(ClassmateContext.java:16) ~[?:?]
    at org.hibernate.boot.internal.BootstrapContextImpl.<init>(BootstrapContextImpl.java:81) ~[?:?]
    at org.hibernate.boot.internal.MetadataBuilderImpl.<init>(MetadataBuilderImpl.java:124) ~[?:?]
    at org.hibernate.boot.MetadataSources.getMetadataBuilder(MetadataSources.java:158) ~[?:?]
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:673) ~[?:?]
    at net.libercraft.data.database.HibernateManager.<init>(HibernateManager.java:50) ~[?:?]
    at net.libercraft.data.DataManager.initializeDatabase(DataManager.java:17) ~[?:?]
    at net.libercraft.main.Main.onLoad(Main.java:34) ~[?:?]
    at org.bukkit.craftbukkit.v1_19_R1.CraftServer.loadPlugins(CraftServer.java:460) ~[purpur-1.19.2.jar:git-Purpur-1765]
    at net.minecraft.server.dedicated.DedicatedServer.e(DedicatedServer.java:314) ~[purpur-1.19.2.jar:git-Purpur-1765]
    at net.minecraft.server.MinecraftServer.v(MinecraftServer.java:1142) ~[purpur-1.19.2.jar:git-Purpur-1765]
    at net.minecraft.server.MinecraftServer.lambda$spin$1(MinecraftServer.java:310) ~[purpur-1.19.2.jar:git-Purpur-1765]
    at java.lang.Thread.run(Thread.java:833) [?:?]
Caused by: java.lang.ClassNotFoundException: com.fasterxml.classmate.TypeResolver
    at org.bukkit.plugin.java.PluginClassLoader.loadClass0(PluginClassLoader.java:153) ~[purpur-api-1.19.2-R0.1-SNAPSHOT.jar:?]
    at org.bukkit.plugin.java.PluginClassLoader.loadClass(PluginClassLoader.java:105) ~[purpur-api-1.19.2-R0.1-SNAPSHOT.jar:?]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[?:?]
    ... 13 more

After googling I found a few posts that give some solutions:

How to resolve java.lang.NoClassDefFoundError: com/fasterxml/classmate/TypeResolver while running spring boot app

Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.JavaType not found

but these are not much help to me.

I tried adding the hibernate-validator library (multiple versions) but it didn't solve the error. I wouldn't know how I could swap the com.fasterxml.jackson with org.codehaus.jackson packages or even if there are org.codehaus.jackson being used in my project.

I feel like I'm doing something essentially wrong. Because in between these errors that I just can't solve, I am getting a lot more ClassNotFoundException etc. that require me to just import more maven libraries to fix them. Isn't that something that Maven should take care of by itself? Like, shouldn't it automatically load any dependant libraries for the libraries that I add to my pom.xml? Why is it requiring me to add all of those manually before my software will work.

None of the tutorial videos on youtube talk about that process, they simply tell you to add the single maven dependancy for hibernate itself. No extra libraries required. So it seems odd to me that I'm being led in that direction by these errors.

It is quite essential for me to implement an ORM solution to my project, and I'm bummed out it's going this way for me. I heard Hibernate was the best solution for this, but maybe there's different solutions for me to try out?

For some context: I'm working on a plugin for the game minecraft and the database will be used to store data about players and other game features.

  • 1
    Did you check the [get started guide?](https://hibernate.org/orm/documentation/getting-started/) Also, `hibernate.cfg.xml` is kinda outdated – XtremeBaumer Sep 06 '22 at 09:35
  • no I didn't check that guide.. migth've been a good starting point thanks for the tip! I hope using info provided by the devs will get me further – Martyn Corsair Sep 06 '22 at 09:58

1 Answers1

0

Bear in mind that if you are on a recent platform 'javax' libraries may have moved to the package 'jakarta'. Try searching in your IDE/classpath what the actual implementations are! I have been successful with this.

        <dependency>
            <groupId>jakarta.activation</groupId>
            <artifactId>javax.activation-api</artifactId>
            <version>2.0.1</version>
        </dependency>

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

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