gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:41:27 27 4
gpt4 key购买 nike

问题:

1)给定一个实体教师,其中教师可以教授许多科目。

2)Teacher(name,Subject) 是一个复合键。

3)一个Subject可以有1个或多个ParentSubject。

解决方案:

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

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

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

下面是以下代码:

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;
}
}

错误:

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

最佳答案

看起来您有点混淆了主键、外键和复合键。

  • 在教师映射中,您将列 id 和 name 声明为教师实体的复合键。我认为这很奇怪,因为您确实有一个数值“id”,它本身可能是唯一的。您有什么理由不使用 TeacherID 作为教师的主键吗?

  • 在同一个映射中,您声明一组科目,这些科目使用整数“forevenid”作为外键来存储哪个老师教授该科目。

  • 异常现在告诉您,字段“forevenid”不能引用复合键 - 您无法将“101,Jalaj”教导它的内容存储在 int s1.forevenid 中。

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

关于java - 如何使用 Hibernate 映射具有(主题,名称)等属性的复合键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42721253/

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