D
D
Denis Kuznetsov2019-04-13 09:32:39
Java
Denis Kuznetsov, 2019-04-13 09:32:39

How to properly connect JAXB for Hibernate?

Hello, I can’t build a Maven project using hibernate
, here is my pom.xml : there are only 2 dependencies added third-party ones in the hope that this will help, but no

<?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>firstCRUDapp</groupId>
    <artifactId>firstCRUDapp</artifactId>
    <version>1.0</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>7</source>
                    <target>7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.5.jre7</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.18.Final</version>
        </dependency>

    </dependencies>

</project>

here is my hibernate.cfg.xml :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgres</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">postgres</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</property>
        <property name="hibernate.show_sql">true</property>
    </session-factory>
</hibernate-configuration>

here is the build of the config
public class HibernateSessionFactoryUtil {

    private static SessionFactory sessionFactory;

    private HibernateSessionFactoryUtil() {}

    public static SessionFactory getSessionFactory() {

        if(sessionFactory == null) {
            try {
                Configuration configuration = new Configuration().configure(); //ошибка здесь 
                configuration.addAnnotatedClass(User.class);
                configuration.addAnnotatedClass(Auto.class);

                //???????
                StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
                sessionFactory = configuration.buildSessionFactory(builder.build());
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }

        return sessionFactory;
    }
}

here is the project structure:
5cb180148724b149671380.png
error: I
5cb1806bca866384504287.png
checked the connection itself through the idea:
5cb180f09e34c547924962.png
I read that it seems like you can specify the path to cfg.xml through idea, but my project settings look like this and I didn’t see a way to specify the path somewhere:
5cb181f8e5e2b633416059.png
//
5cb1820b91487091806119.png
//
5cb1822679a75300132655.png
I can’t understand in than the problem, everything seems to be done according to the instructions, thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-04-13
@DennisKingsman

The exception occurs due to the fact that Hibernate needs JAXB, and in Java9 it was moved to a separate model. You can try adding it to pom.xml as an explicit dependency. If it does not help, you will have to deal with modularity ( one , two ) and how to connect modules in IDEA .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question