java - 没有找到异常hibernate.properties
问题描述
我试图配置我的休眠并出现此错误。我检查了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();
}
}
我的项目结构
我的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
文件位于 path 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>
推荐阅读
- reactjs - React-router 在生产模式下重定向我的路由
- pine-script - 如何在 pine 脚本中仅获取指标的最后一个值
- python - 多项式坐标
- google-cloud-storage - KingswaySoft Google Cloud Storage Source 组件缺少参数
- reactjs - Azure Active Directory - 如何为我在租户下注册的应用程序提供 http redirectUri
- raspberry-pi - 如何同时在树莓派上使用相同的引脚插入设备?/ 树莓派 4 B 型
- python - 将列值的第一行转换为标题或转行并将它们转换为标题
- python - 在 django 管理命令中异步运行函数
- javascript - HTML
- cordova - 如何在cordova插件中包含多个包的java代码?