代码之家  ›  专栏  ›  技术社区  ›  Filip

缺少Hibernate ServiceRegistry类?

  •  0
  • Filip  · 技术社区  · 4 年前

    我正在开始学习hibernate,并遵循本教程

    https://www.javaguides.net/2018/11/hibernate-hello-world-tutorial.html

    但当运行我的罐子时,我得到了

    Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/service/ServiceRegistry
    

    这很不言自明,但我不知道如何得到它

    我已尝试添加

    import org.hibernate.service.ServiceRegistry;
    

    但运气不佳

    本教程中的最小结构是:

    pom文件:

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.3.7.Final</version>
        </dependency>
    </dependencies>
    

    App.java

    package net.javaguides.hibernate;
    
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    
    public class App {
        public static void main(String[] args) {
            Student student = new Student("Ramesh", "Fadatare", "rameshfadatare@javaguides.com");
            Transaction transaction = null;
    
            try (Session session = HibernateUtil.getSessionFactory().openSession()) {
                transaction = session.beginTransaction();
                session.save(student);
                transaction.commit();
            } catch (Exception e) {
                if (transaction != null) {
                    transaction.rollback();
                }
                e.printStackTrace();
            }
        }
    }
    

    HibernateUtil.java

    package net.javaguides.hibernate;
    
    import org.hibernate.SessionFactory;
    import org.hibernate.boot.Metadata;
    import org.hibernate.boot.MetadataSources;
    import org.hibernate.boot.registry.StandardServiceRegistry;
    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
    
    public class HibernateUtil {
        private static StandardServiceRegistry registry;
        private static SessionFactory sessionFactory;
    
        public static SessionFactory getSessionFactory() {
            if (sessionFactory == null) {
                try {
                    registry = new StandardServiceRegistryBuilder().configure().build();
    
                    MetadataSources sources = new MetadataSources(registry);
    
                    Metadata metadata = sources.getMetadataBuilder().build();
    
                    sessionFactory = metadata.getSessionFactoryBuilder().build();
    
                } catch (Exception e) {
                    e.printStackTrace();
                    if (registry != null) {
                        StandardServiceRegistryBuilder.destroy(registry);
                    }
                }
            }
            return sessionFactory;
        }
    
        public static void shutdown() {
            if (registry != null) {
                StandardServiceRegistryBuilder.destroy(registry);
            }
        }
    }
    

    如果重要的话,我正在使用Manjaro Linux

    0 回复  |  直到 4 年前
        1
  •  0
  •   Filip    4 年前

    显然,问题在于我生成的jar(及其目标文件夹)中缺少Hibernate本身

    解决方案是将依赖项打包到jar本身或目标文件夹中(并让类路径引用它们?)

    我选择了第一个,但这会减缓重新编译的速度:

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>com.fistmedia.javapp.App</mainClass>
                </manifest>
            </archive>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id> <!-- this is used for inheritance merges -->
                <phase>package</phase> <!-- bind to the packaging phase -->
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>