java - 未找到异常 hibernate.properties

标签 java hibernate maven

我尝试配置 hibernate 并出现此错误。我检查了 stackoverflow 上的所有答案,但没有帮助。我将 cfg 文件放入资源中,但没有帮助。

public class Dbconnect {

        public static void main(String[] args) throws Exception {

              StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure("/hibernate.cfg.xml").build();     

           Metadata metadata = new MetadataSources( standardRegistry )
                   .addAnnotatedClass( UserProfile.class )
                   .getMetadataBuilder()
                   .applyImplicitNamingStrategy( ImplicitNamingStrategyJpaCompliantImpl.INSTANCE )
                    .build();

            SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();


        }
    }

我的项目结构

enter image description here

我的cfg文件

    <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mytestdb</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">pass</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    </session-factory>
</hibernate-configuration>

更新。 添加了 mvn 依赖并存在时区错误。

<dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.0</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>javax.activation-api</artifactId>
            <version>1.2.0</version>
        </dependency>

错误:

    окт. 15, 2018 3:19:44 ПП org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.3.6.Final}
окт. 15, 2018 3:19:44 ПП org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.sun.xml.bind.v2.runtime.reflect.opt.Injector (file:/C:/Users/12/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.0/jaxb-runtime-2.3.0.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)
WARNING: Please consider reporting this to the maintainers of com.sun.xml.bind.v2.runtime.reflect.opt.Injector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
INFO: HHH000422: Disabling contextual LOB creation as connection was null
ERROR: The server time zone value 'RTZ 2 (çèìà)' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

最佳答案

您的 hibernate.cfg.xml 文件似乎位于路径 src/main/resources 中;当 maven 构建项目时,该路径将(默认情况下)位于类路径的根目录,因此要将其加载到您的上下文中,您应该使用:

new StandardServiceRegistryBuilder().configure("/hibernate.cfg.xml")

编辑

新的堆栈跟踪显示在您进行上述更改后现在可以找到 xml 文件;不幸的是,由于缺少库(jaxb),它无法被解析。

使用以下内容更改 pom.xml 以添加依赖项:

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.3.0</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>javax.activation-api</artifactId>
    <version>1.2.0</version>
</dependency>

关于java - 未找到异常 hibernate.properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52815836/

相关文章:

java - Jasper Report PDF 中未显示乌克兰(西里尔文)字符

java - 什么样的数据库适合存储我的对象?

hibernate - 如何使用 Criteria 返回具有选定列的实体

database - Hibernate Criteria 一对多关系

scala - Maven项目找不到junit

java - junit5 - 并行运行测试(pom 中没有 forkCount)

java - Maven jar 文件 : library include failed

java - 修补使用 XAMPP 部署的 Web 应用程序的巧妙方法是什么?

Java正则表达式仅当星号是最后一个字符时才匹配

java - 我是否正确地将 Hibernate 与 Hikari 一起使用?