找不到异常hibernate.properties

时间:2018-10-15 11:32:35

标签: java hibernate maven

我试图配置我的休眠模式并出现此错误。我检查了关于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>

upd。 添加了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.

1 个答案:

答案 0 :(得分:0)

您的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>