错误消息属性'class'必须出现在元素'generator'上

时间:2017-03-11 09:10:16

标签: java hibernate one-to-one

问题:

1)给予教师教师可以教授许多科目的实体教师。

2)教师(姓名,科目)是复合键。

3)受试者有另一个受试者作为父母受试者。

解决方案: 我已经实现了2分,但我在第3点得到了错误。

  

任何人都可以指导我如何纠正它吗?

以下是以下代码

public class Client {

public static void main(String args[])
{

Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");

SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
//parent object
Teacher t =new Teacher();
t.setTeacherId(101);
t.setTeacherName("A"); 
//creating 3 child objects
Subject s1=new Subject(); 
s1.setSubjectId(1);
s1.setSubjectName("JSE");
Subject s2=new Subject();
s2.setSubjectId(505);
s2.setSubjectName("JEE"); 
Subject s3=new Subject();
s3.setSubjectId(506);
s3.setSubjectName("Spring");
// adding child objects to set, as we taken 3rd property set in parent
Set s=new HashSet(); 
s.add(s1);
s.add(s2);
s.add(s3);
t.setSubjects(s);
Transaction tx = session.beginTransaction(); 
session.save(t);
tx.commit();
session.close();
System.out.println("One To Many is Done..!!");
factory.close(); 
}
}


public class Subject implements Serializable{

private int subjectId;
private String subjectName;
private int forevenId;
public int getSubjectId() {
return subjectId;
}
public void setSubjectId(int subjectId) {
this.subjectId = subjectId;
}
public String getSubjectName() {
return subjectName;
}
public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}
public int getForevenId() {
return forevenId;
}
public void setForevenId(int forevenId) {
this.forevenId = forevenId;
} 
}

public class Teacher implements Serializable{

private int teacherId;
private String teacherName;
private Set subjects;
public int getTeacherId() {
return teacherId;
}
public void setTeacherId(int teacherId) {
this.teacherId = teacherId;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
public Set getSubjects() {
return subjects;
}
public void setSubjects(Set subjects) {
this.subjects = subjects;
}
}

public class Parent {
private int subjectId;
private String parentSubjectName;
private Subject s;
public Subject getS() {
    return s;
}
public void setS(Subject s) {
    this.s = s;
}
public int getSubjectId() {
    return subjectId;
}
public void setSubjectId(int subjectId) {
    this.subjectId = subjectId;
}
public String getParentSubjectName() {
    return parentSubjectName;
}
public void setParentSubjectName(String parentSubjectName) {
    this.parentSubjectName = parentSubjectName;
}   
}

Subject.hbm.xml

Parent.hbm.xml

Teacher.hbm.xml

错误:

Exception in thread "main" org.hibernate.boot.InvalidMappingException:  Could not parse mapping document: Parent.hbm.xml (RESOURCE)
at org.hibernate.boot.jaxb.internal.InputStreamXmlSource.doBind(InputStreamXmlSource.java:46)
at org.hibernate.boot.jaxb.internal.UrlXmlSource.doBind(UrlXmlSource.java:36)
at org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:59)
at org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:274)
at org.hibernate.boot.cfgxml.spi.MappingReference.apply(MappingReference.java:70)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:413)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:723)
at str.OurLogic.main(OurLogic.java:19)
Caused by: org.hibernate.boot.MappingException: Unable to perform  unmarshalling at line number 9 and column 12. Message: cvc-complex-type.4:  Attribute 'class' must appear on element 'generator'. : origin(Parent.hbm.xml)
at org.hibernate.boot.jaxb.internal.AbstractBinder.jaxb(AbstractBinder.java:177)
at org.hibernate.boot.jaxb.internal.MappingBinder.doBind(MappingBinder.java:61)
at org.hibernate.boot.jaxb.internal.AbstractBinder.doBind(AbstractBinder.java:102)
at org.hibernate.boot.jaxb.internal.AbstractBinder.bind(AbstractBinder.java:57)
at org.hibernate.boot.jaxb.internal.InputStreamXmlSource.doBind(InputStreamXmlSource.java:43)
... 9 more
Caused by: javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 9; columnNumber: 12; cvc-complex-type.4: Attribute 'class' must appear on element 'generator'.]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:468)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:448)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:420)
at org.hibernate.boot.jaxb.internal.AbstractBinder.jaxb(AbstractBinder.java:171)
... 13 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 9; columnNumber: 12; cvc-complex-type.4: Attribute 'class' must appear on element 'generator'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:458)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3237)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.addDefaultAttributes(XMLSchemaValidator.java:2943)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:2060)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:746)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.startElement(ValidatorHandlerImpl.java:570)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(ValidatingUnmarshaller.java:86)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:60)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.handleStartElement(StAXEventConnector.java:246)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.bridge(StAXEventConnector.java:115)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:445)
... 15 more

1 个答案:

答案 0 :(得分:0)

我在课堂上使用了这个属性,它解决了@JB Nizet所指出的问题Parent.hbm.xml