gpt4 book ai didi

java - 获取 org.hibernate.InvalidMappingException : unable to read xml when I try to run my program

转载 作者:行者123 更新时间:2023-12-01 11:39:38 25 4
gpt4 key购买 nike

我是 Hibernate 的初学者,无法理解我的程序的哪一部分出错了。谁能告诉我什么可能导致这种类型的错误:

org.hibernate.InvalidMappingException: unable to read xml

ManyToManyDemo.java——它包含持久类和用于插入数据的测试类。

import java.util.*;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.Transaction;

class Student {
private int sid = 0;
private String sname = null;
private Set<Course> courses = null;

public int getSid() {
return this.sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return this.sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Set<Course> getCourses() {
return this.courses;
}
public void setcourses(Set<Course> courses) {
this.courses = courses;
}

Student(int sid, String sname) {
setSid(sid);
setSname(sname);
}
}

class Course {
private int cid = 0;
private String cname = null;
private Set<Student> students = null;

public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCname() {
return this.cname;
}
public void setCname(String cname) {
this.cname=cname;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}

Course(int cid, String cname) {
setCid(cid);
setCname(cname);
}
}

class Test {
private SessionFactory factory = null;

public void initSessionFactory() {
Configuration config = new Configuration().configure("hibernate.cfg.xml");
ServiceRegistry registry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
factory = config.buildSessionFactory(registry);
}

public void createCourseAndStudents() {
Session session = factory.getCurrentSession();
Transaction tx = null;

Set<Student> studentset = new HashSet<Student>();
Course course = null;

Student s1 = new Student(5, "Halle Price");
Student s2 = new Student(3, "William Wick");
studentset.add(s1);
studentset.add(s2);

course = new Course(24, "Django");
course.setStudents(studentset);

tx = session.beginTransaction();
session.save(course);
tx.commit();
}
}

public class ManyToManyDemo {
public static void main(String[] args) {
Test t = new Test();
t.initSessionFactory();
t.createCourseAndStudents();
}
}
<小时/>

类(class).hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Course" table="coursetab">
<id name = "cid" column = "cid">
<!--<generator class="native"/>-->
</id>
<property name= "cname" column = "cname"/>
</class>
<set name="students" inverse="false" lazy="false" table="student_course" cascade="all">
<key column="cid" not-null="true"/>
<many-to-many column="sid" class="Student"/>
</set>
</hibernate-mapping>
<小时/>

student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Student" table="studenttab">
<id name = "sid" column = "sid">
<generator class="native"/>
</id>
<property name= "sname" column = "sname"/>
</class>
<set name="courses" inverse = "true" lazy="false" table="student_course" cascade="all">
<key column="sid" not-null="true"/>
<many-to-many column="cid" class="Course"/>
</set>
</hibernate-mapping>
<小时/>

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="connection.url">jdbc:mysql://localhost:3306/demodb</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">dbuser</property>
<property name="connection.password">dbpassword</property>
<property name="connection.pool_size">0</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="current_session_context_class">thread</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="student.hbm.xml" />
<mapping resource="course.hbm.xml" />
</session-factory>
</hibernate-configuration>
<小时/>

链接表

create table student_course (cid int not null, sid int not null, primary key(cid, sid), constraint fk_cid foreign key(cid) references coursetab(cid), constraint fk_sid foreign key(sid) references studenttab(sid));

最佳答案

您的 XML 中似乎有错误。如果您使用 Eclipse(或其他一些 IDE),通常会有一个“验证”选项(在 Eclipse 中,右键单击文本编辑器并选择“验证”)

当我对你的 course.hbm.xml 文件执行此操作时,它说 hibernate-mapping 标记的内容是错误的。

看起来你已经在类之外定义了“集合”?

当我将你的文件更改为这个时...

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Course" table="coursetab">
<id name="cid" column="cid">
<!--<generator class="native"/>-->
</id>
<property name="cname" column="cname"/>
<set name="students" inverse="false" lazy="false" table="student_course" cascade="all">
<key column="cid" not-null="true"/>
<many-to-many column="sid" class="Student"/>
</set>
</class> <!-- I moved this tag here to be below the set element -->
</hibernate-mapping>

我没有收到验证错误。

关于java - 获取 org.hibernate.InvalidMappingException : unable to read xml when I try to run my program,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29648008/

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