我只是使用IDEA来创建一个休眠的项目,然后使用mysql模式生成持久性映射。 而且我想运行这些东西(它们是自动生成的),但是有一个错误和一个异常,我无法解决这些问题...
java.lang.ExceptionInInitializerError
at HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:31)
Caused by: java.lang.IllegalArgumentException: expecting IdClass mapping
休眠5.4.2 MySQL 8.0.15
//----------------------------------------------------------
// GradesEntity.java
public class HibernateSessionFactory {
private static final SessionFactory ourSessionFactory;
static {
try {
Configuration configuration = new Configuration();
configuration.configure();
configuration.addClass(CourseEntity.class)
.addClass(GradesEntity.class)
.addClass(GradesTypeEntity.class)
.addClass(GroupEntity.class)
.addClass(PersonResponsibleEntity.class)
.addClass(ProfessorEntity.class)
.addClass(ResponsibleEntity.class)
.addClass(StudentEntity.class)
.addClass(TeachingEntity.class);
ourSessionFactory = configuration.buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static void main(final String[] args) throws Exception {
final Session session = getSession();
try {
System.out.println("querying all the managed entities...");
final Metamodel metamodel = session.getSessionFactory().getMetamodel();
for (EntityType<?> entityType : metamodel.getEntities()) {
final String entityName = entityType.getName();
final Query query = session.createQuery("from " + entityName);
System.out.println("executing: " + query.getQueryString());
for (Object o : query.list()) {
System.out.println(" " + o);
}
}
} finally {
session.close();
}
}
public static Session getSession() throws HibernateException {
return ourSessionFactory.openSession();
}
}
//----------------------------------------------------------
// GradesEntity.java
@Entity
@Table(name = "grades", schema = "bdd2_projet", catalog = "")
@IdClass(GradesEntityPK.class)
public class GradesEntity {
private int idStudent;
private int codeCourse;
private Integer grades;
private int idProfessor;
@Id
@Column(name = "id_student", nullable = false)
public int getIdStudent() {
return idStudent;
}
public void setIdStudent(int idStudent) {
this.idStudent = idStudent;
}
@Id
@Column(name = "code_course", nullable = false)
public int getCodeCourse() {
return codeCourse;
}
public void setCodeCourse(int codeCourse) {
this.codeCourse = codeCourse;
}
@Basic
@Column(name = "grades", nullable = true)
public Integer getGrades() {
return grades;
}
public void setGrades(Integer grades) {
this.grades = grades;
}
@Id
@Column(name = "id_professor", nullable = false)
public int getIdProfessor() {
return idProfessor;
}
public void setIdProfessor(int idProfessor) {
this.idProfessor = idProfessor;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GradesEntity that = (GradesEntity) o;
return idStudent == that.idStudent &&
codeCourse == that.codeCourse &&
idProfessor == that.idProfessor &&
Objects.equals(grades, that.grades);
}
@Override
public int hashCode() {
return Objects.hash(idStudent, codeCourse, grades, idProfessor);
}
}
//----------------------------------------------------------
// GradesEntityPK.java
public class GradesEntityPK implements Serializable {
private int idStudent;
private int codeCourse;
private int idProfessor;
@Id
@Column(name = "id_student", nullable = false)
public int getIdStudent() {
return idStudent;
}
public void setIdStudent(int idStudent) {
this.idStudent = idStudent;
}
@Id
@Column(name = "code_course", nullable = false)
public int getCodeCourse() {
return codeCourse;
}
public void setCodeCourse(int codeCourse) {
this.codeCourse = codeCourse;
}
@Id
@Column(name = "id_professor", nullable = false)
public int getIdProfessor() {
return idProfessor;
}
public void setIdProfessor(int idProfessor) {
this.idProfessor = idProfessor;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GradesEntityPK that = (GradesEntityPK) o;
return idStudent == that.idStudent &&
codeCourse == that.codeCourse &&
idProfessor == that.idProfessor;
}
@Override
public int hashCode() {
return Objects.hash(idStudent, codeCourse, idProfessor);
}
}
针尖上的错误和兴奋:
java.lang.ExceptionInInitializerError
at HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:31)
Caused by: java.lang.IllegalArgumentException: expecting IdClass mapping
at org.hibernate.metamodel.internal.AttributeFactory$3.resolveMember(AttributeFactory.java:977)
at org.hibernate.metamodel.internal.AttributeFactory$5.resolveMember(AttributeFactory.java:1035)
at org.hibernate.metamodel.internal.AttributeFactory.determineAttributeMetadata(AttributeFactory.java:450)
at org.hibernate.metamodel.internal.AttributeFactory.buildIdAttribute(AttributeFactory.java:139)
at org.hibernate.metamodel.internal.MetadataContext.buildIdClassAttributes(MetadataContext.java:389)
at org.hibernate.metamodel.internal.MetadataContext.applyIdMetadata(MetadataContext.java:319)
at org.hibernate.metamodel.internal.MetadataContext.wrapUp(MetadataContext.java:222)
at org.hibernate.metamodel.internal.MetamodelImpl.initialize(MetamodelImpl.java:274)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:294)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:462)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:29)
Exception in thread "main"