0

Problem:

1)Given an entity Teacher where a Teacher can teach many Subjects.

2)Teacher(name,Subject) is a Composite Key.

3)A Subject can have 1 or more ParentSubject.

Solution:

1)I used the following threads Thread1 Thread2 Thread3,Thread4 to find the solution to the problem and implemented the above solution but could not write proper implementation.

2)For a Composite key I used subjectId and teacherId because I was unable to map Subject with composite key

Q)Can anyone guide me how to find solution to the above problem.

Below is the following Code:

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

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
Jack
  • 81
  • 13

1 Answers1

1

It looks like your mixing up primary, foreign and composite keys a bit.

  • In your teacher mapping you declare the columns id and name as a composite key for the teacher entity. I'd call this odd, as you do have a numerical value "id" that might itself be unique. Is there any reason you don't use teacherID as primary key on teacher?

  • In the same mapping you declare a set of subjects that use the integer "forevenid" as a foreign key to store which teacher teaches that subject.

  • The exception tells you now, that the field "forevenid" can not refer to a composite key - you can't store in the int s1.forevenid that "101, Jalaj" teaches it.

  • Changing the composite key on teacher to a simple primary key should solve your problem (alternatively you'd have to make forevenid a composite foreign key...). If this doesn't help, just try to stick to a basic example from the hibernate documentation for set mappings.

sruetti
  • 532
  • 2
  • 7