主要问题是我使用了JDK9,其中JAXB API不再在默认类路径上。这里是我找到解决方案的链接。
我使用了示例教程中的hibernate 4.3.6版本,并且一切都运行良好,但是当我将版本更新到5.2.10时,我遇到了问题(我知道负责getSessionFactory的代码不同,所以我更改了它)。
我的第一个功能:
private static SessionFactory getSessionFactory(){
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
return sessionFactory;
}
private static SessionFactory getSessionFactory(){
StandardServiceRegistry registry;
SessionFactory sessionFactory;
registry = new StandardServiceRegistryBuilder().configure().build();
MetadataSources sources = new MetadataSources(registry);
Metadata metadata = sources.getMetadataBuilder().build();
sessionFactory = metadata.getSessionFactoryBuilder().build();
return sessionFactory;
}
例外情况:
线程“main”org.hibernate.internal.util.config.ConfigurationException中出现异常:无法在资源hibernate.cfg.xml中的行号0和列0处执行反封送处理。消息:空
和hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL94Dialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1:5432/postgres</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">123</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">false</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="com.example.Item"/>
</session-factory>
</hibernate-configuration>
<?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>pl.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.1.4</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
</dependencies>
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
StandardServiceRegistry standardRegistry =
new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata metaData =
new MetadataSources(standardRegistry).getMetadataBuilder().build();
sessionFactory = metaData.getSessionFactoryBuilder().build();
} catch (Throwable th) {
System.err.println("Enitial SessionFactory creation failed" + th);
throw new ExceptionInInitializerError(th);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
@Entity
@Table(name = "employee")
public class Employee implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="emp_name")
private String empName;
@Column(name="emp_address")
private String empAddress;
@Column(name="emp_mobile_nos")
private String empMobileNos;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpAddress() {
return empAddress;
}
public void setEmpAddress(String empAddress) {
this.empAddress = empAddress;
}
public String getEmpMobileNos() {
return empMobileNos;
}
public void setEmpMobileNos(String empMobileNos) {
this.empMobileNos = empMobileNos;
}
}
测试课。
public class Test{
public static void main(String[] args) throws Exception {
SessionFactory sessFact = HibernateUtil.getSessionFactory();
Session session = sessFact.getCurrentSession();
org.hibernate.Transaction tr = session.beginTransaction();
Employee emp = new Employee();
emp.setEmpName("Deepak Kumar");
emp.setEmpMobileNos("000000");
emp.setEmpAddress("Delhi - India");
session.save(emp);
tr.commit();
System.out.println("Successfully inserted");
sessFact.close();
}
}
lis 17, 2017 11:54:50 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.10.Final}
lis 17, 2017 11:54:50 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
lis 17, 2017 11:54:50 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
Enitial SessionFactory creation failedorg.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 0 and column 0 in RESOURCE hibernate.cfg.xml. Message: null
Exception in thread "main" java.lang.ExceptionInInitializerError
at pl.lostandfound.HibernateUtil.<clinit>(HibernateUtil.java:31)
at pl.lostandfound.Test.main(Test.java:13)
Caused by: org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 0 and column 0 in RESOURCE hibernate.cfg.xml. Message: null
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:133)
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65)
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
at pl.lostandfound.HibernateUtil.<clinit>(HibernateUtil.java:24)
... 1 more
Caused by: javax.xml.bind.JAXBException
- with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:241)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:477)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:656)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:599)
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:122)
... 5 more
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
at javax.xml.bind.ContextFinder.safeLoadClass(ContextFinder.java:594)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:239)
... 9 more
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Enitial SessionFactory creation failedorg.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]
Exception in thread "main" java.lang.ExceptionInInitializerError
我当然会首先修复ClassNotFound异常(java.lang.ClassNotFoundException:com.sun.xml.internal.bind.v2.ContextFactory)。这表示需要加载的类不存在...
添加以下依赖项可能会解决您的问题:
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
最新版本摘自:https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl
如果ClassNotFound异常仍然存在...我会更仔细地查看hibernate使用的是什么版本的JAXB。分析依赖关系的一个非常好的方法是以下两个命令:
mvn clean dependency:analyze
而且
mvn clean dependency:tree
编辑4:检查以下答案:
我可以用运行我的应用程序,但是当我只是做时,我得到了 这是我的Docker文件
Maven执行 问题-这个自定义提供程序是否不再受支持,或者是否有任何解决方案可以使用、和/或执行测试? 注意-测试执行在JUnit-4中运行良好。
我正在尝试从命令行从Android SDK 工具运行 。但是,它不会启动并打印以下异常: 我的命令行是: 已设置。 以下是我的安装(sdkmanager - list): 怎么了?
问题内容: 我正在尝试执行以下代码: 但是Python没有响应。该过程占用100%的CPU,并且不会停止。我已经在Python 2.7.1和Python 3.2上尝试了相同的结果。 问题答案: 由于您有嵌套的量词(),因此正则表达式会遭受灾难性的回溯。由于您的正则表达式要求字符串结尾(在您的示例中失败),因此正则表达式引擎尝试对字符串进行所有排列都是徒劳的,希望找到匹配的组合。那就是它卡住的地方。
我需要重写执行程序的执行方法,我需要改变线程超过核心池大小的行为,只有当队列已满时才会创建。 然而,在实时应用程序中,这种行为是不可取的,因为它会导致队列中存在的任务无休止地等待。 我已将execute方法更改如下: 尝试实现:核心线程-
我必须建立一个docker图像。在我的存储库中,我保存了这些文件: Dockerfile docker-提示 我的Dockerfile是: 我已经运行了这个cmd: docker构建-t"myImage" 但是如果失败了: 步骤8/8: COPY["docker-提示符","sudo","/usr/local/bin/"]COPY失败:stat /var/lib/docker/tmp/docker