如何映射具有(主题,名称)等属性的复合键与Hibernate?

时间:2017-03-10 14:52:35

标签: java hibernate

问题:

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

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

3)主题可以有1个或更多个ParentSubject。

解决方案:

1)我使用了以下线程Thread1 Thread2 Thread3Thread4 找到问题的解决方案并实现上述解决方案,但无法编写正确的实现。

2)对于复合键,我使用了subjectId和teacherId,因为我无法使用复合键映射Subject

  

问)任何人都可以指导我如何找到解决上述问题的方法。

以下是以下代码:

public class OurLogic {

 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("Jalaj"); 
 //creating 3 child objects
 Subject s1=new Subject(); 
 s1.setSubjectId(504);
 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 TeachSubject  implements Serializable{
private Long subjectId;
private Long teacherId;
// an easy initializing constructor
public TeachSubject(Long testId, Long customerId){
this.subjectId = subjectId;
this.teacherId = teacherId;
}

public Long getSubjectId() {
return subjectId;
}

public void setSubjectId(Long subjectId) {
this.subjectId = subjectId;
}

public Long getTeacherId() {
return teacherId;
}

public void setTeacherId(Long teacherId) {
this.teacherId = teacherId;
}

@Override
public boolean equals(Object arg0) {
if(arg0 == null) return false;
if(!(arg0 instanceof TeachSubject)) return false;
TeachSubject arg1 = (TeachSubject) arg0;
return (this.subjectId.longValue() == arg1.getSubjectId().longValue())  && (this.teacherId.longValue() == arg1.getTeacherId().longValue());

}
@Override
public int hashCode() {
int hsCode;
hsCode = subjectId.hashCode();
hsCode = 19 * hsCode+ teacherId.hashCode();
return hsCode;
}
}

ERROR:

Exception in thread "main" org.hibernate.boot.InvalidMappingException:   Could not parse mapping document: TeachSubject.hbm.xml (RESOURCE)
at  org.hibernate.boot.jaxb.internal.InputStreamXmlSource.doBind(InputStreamXml Source.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: Error accessing stax  stream : origin(TeachSubject.hbm.xml)
at org.hibernate.boot.jaxb.internal.AbstractBinder.seekRootElementStartEvent(AbstractBinder.java:141)
at org.hibernate.boot.jaxb.internal.AbstractBinder.doBind(AbstractBinder.java:101)
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.stream.XMLStreamException: ParseError at [row,col]:[5,2]
Message: The markup in the document preceding the root element must be well-formed.
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:601)
at com.sun.xml.internal.stream.XMLEventReaderImpl.peek(XMLEventReaderImpl.java:276)
at javax.xml.stream.util.EventReaderDelegate.peek(EventReaderDelegate.java:104)
at org.hibernate.boot.jaxb.internal.stax.BufferedXMLEventReader.peek(BufferedXMLEventReader.java:96)
at org.hibernate.boot.jaxb.internal.AbstractBinder.seekRootElementStartEvent(AbstractBinder.java:137)
... 12 more

SubjectMappingFile

TeacherMappingFile

TeachSubjectMaappingFile

1 个答案:

答案 0 :(得分:1)

看起来你把主键,外键和复合键混合了一下。

  • 在您的教师映射中,您将列ID和名称声明为教师实体的复合键。我称这是奇怪的,因为你有一个数值" id"这可能本身就是独特的。您是否有任何理由不使用teacherID作为教师的主键?

  • 在同一个映射中,您声明了一组使用整数" forevenid"作为存储哪个老师教授该主题的外键。

  • 该例外现在告诉你,该字段" forevenid"不能引用复合键 - 你不能存储在int s1.forevenid中" 101,Jalaj"教它。

  • 将教师的复合键更改为简单的主键可以解决您的问题(或者您必须使复合外键...)。如果这没有帮助,只需尝试从hibernate文档中坚持设置映射的基本示例。