gpt4 book ai didi

java - org.dom4j.DocumentException : hibernate. 组织嵌套异常 : hibernate. 组织

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:20:06 29 4
gpt4 key购买 nike

我是 hibernate 的新手。我正在开发一个 struts 应用程序,我想在其中集成 hibernate。我正在使用 SQL Server 2008 R2 作为数据库。我已经配置了我的 hibernate 配置文件并使用了基于注释的实体。当我尝试运行时我的 hibernate 配置文件,出现以下错误:

错误

12:49:41.752 [main] DEBUG org.hibernate.util.DTDEntityResolver - trying to resol
ve system-id [http://hibernate.org/dtd/hibernate-configuration-3.0.dtd]
Initial SessionFactory creation failed.org.hibernate.HibernateException: Could n
ot parse configuration: hibernate.cfg.xml

我在下面给出了我的文件:

DAO实现

package com.myProj.dao.impl;

import java.util.logging.Logger;

import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;
import com.myProj.dao.StudentDetailsDAO;
import com.myProj.entity.StudentDetails;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class StudentDetailsDAOImpl implements StudentDetailsDAO{

Logger LOGGER;
@SessionTarget
Session session;
@TransactionTarget
Transaction transaction;
@Override
public void saveOrUpdateStudentDetail(StudentDetails studDetails) {
try{
session.saveOrUpdate(studDetails);
}catch(Exception e){
transaction.rollback();
LOGGER.info("StudentDetailsDAOImpl : saveOrUpdateStudentDetail : Exception "+e.toString());
}

}

}

StudentDetails.java

package com.myProj.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="dbo.student_details")
public class StudentDetails {
@Id
@GeneratedValue
@Column(name="id")
private int id;

@Column(name="name")
private String name;

@Column(name="grade")
private String grade;

@Column(name="dob")
private String dob;

@Column(name="stud_address")
private String stud_address;

@Column(name="stud_language")
private String stud_language;

@Column(name="student_marks_id")
private StudentMarks studentMarks;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getGrade() {
return grade;
}

public void setGrade(String grade) {
this.grade = grade;
}

public String getDob() {
return dob;
}

public void setDob(String dob) {
this.dob = dob;
}

public String getStud_address() {
return stud_address;
}

public void setStud_address(String stud_address) {
this.stud_address = stud_address;
}

public String getStud_language() {
return stud_language;
}

public void setStud_language(String stud_language) {
this.stud_language = stud_language;
}

public StudentMarks getStudentMarks() {
return studentMarks;
}

public void setStudentMarks(StudentMarks studentMarks) {
this.studentMarks = studentMarks;
}
}

StudentMark.java

package com.myProj.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name="dbo.student_marks")
public class StudentMarks {
@Id
@GeneratedValue
@Column(name="id")
private int id;

@Column(name="english")
private int english;

@Column(name="physics")
private int physics;

@Column(name="chemistry")
private int chemistry;

@Column(name="biology")
private int biology;

@Column(name="maths")
private int maths;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getPhysics() {
return physics;
}
public void setPhysics(int physics) {
this.physics = physics;
}
public int getChemistry() {
return chemistry;
}
public void setChemistry(int chemistry) {
this.chemistry = chemistry;
}
public int getBiology() {
return biology;
}
public void setBiology(int biology) {
this.biology = biology;
}
public int getMaths() {
return maths;
}
public void setMaths(int maths) {
this.maths = maths;
}

}

hibernate 工具.java

package com.myProj.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import com.myProj.entity.StudentDetails;

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
try {
AnnotationConfiguration config = new AnnotationConfiguration();
config.addAnnotatedClass(StudentDetails.class);



config.configure("hibernate.cfg.xml");
//new SchemaExport(config).create(true,true);

sessionFactory = config.buildSessionFactory();

} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}

hibernate .cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url"> jdbc:sqlserver://<myIp>:1433;databaseName=<myDatabase></property>
<property name="hibernate.connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.pool_size">1</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.myProj.entity.StudentDetails" ></mapping>
<mapping class="com.myProj.entity.StudentMarks" ></mapping>
</session-factory>
</hibernate-configuration>

我在下面给出了我正在使用的 jar 文件列表:

commons-beanutils-1.8.0.jar
commons-chain-1.2.jar
commons-digester-2.1.jar
hibernate-3.5.3.jar
javax.servlet-3.0.jar
logback-classic-0.9.6.jar
logback-core-0.9.6.jar
ognl-3.0.6.jar
slf4j-api-2.0.99.jar
slf4j-log4j13-1.0.1.jar
sqljdbc-1.2.0.jar
struts-core-1.3.10.jar
struts-taglib-1.3.10-sources.jar
struts-taglib-1.3.5.jar
struts2-core-2.3.8.jar
struts2-json-plugin-2.3.8.jar
struts2-tiles-plugin-2.1.8.1.jar
xwork-2.0.4.jar
xwork-core-2.3.12.jar

另外,当我检查我的 apache 服务器时,我得到以下错误

Apache 服务器日志

java.lang.ExceptionInInitializerError
at com.myProj.util.HibernateUtil.<clinit>(HibernateUtil.java:27)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3744)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4252)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:448)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
Caused by: org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1586)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:1212)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:107)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1520)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:1194)
at com.myProj.util.HibernateUtil.<clinit>(HibernateUtil.java:19)
... 21 more
Caused by: org.dom4j.DocumentException: hibernate.org Nested exception: hibernate.org
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1576)
... 26 more

最佳答案

我终于发现了问题所在。但不确定为什么它不起作用。我更改了配置文件中的以下行

<!DOCTYPE hibernate-configuration  PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

 <!DOCTYPE hibernate-configuration SYSTEM "classpath://com/myProj/util/hibernate-configuration-3.0.dtd">

并将文件 hibernate-configuration-3.0.dtd 放在包 com.myProj.util 中。现在此配置文件作为 gem 运行并创建 session 。

关于java - org.dom4j.DocumentException : hibernate. 组织嵌套异常 : hibernate. 组织,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43799508/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com