获取org.hibernate.AnnotationException:未知的实体名称:int

时间:2015-02-21 09:37:32

标签: java hibernate orm

我正在开发一个使用Hibernate来映射数据库表的项目。 但我不断收到以下错误:

  

引起:org.hibernate.AnnotationException:未知的实体名称:int

我发布了所有实体类,因为找不到错误。

以下是execption的完整堆栈跟踪:

    Caused by: org.hibernate.AnnotationException: Unknown entity name: int
    at org.hibernate.cfg.PkDrivenByDefaultMapsIdSecondPass.doSecondPass(PkDrivenByDefaultMapsIdSecondPass.java:50)
    at org.hibernate.cfg.Configuration.processSecondPassesOfType(Configuration.java:1470)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1417)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
    at control.DAO.<clinit>(DAO.java:21)
    ... 32 more
]]

DAO.java类是

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import java.util.logging.Level;
import java.util.logging.Logger;

public class DAO {

    private static final Logger log = Logger.getAnonymousLogger();
    private static final ThreadLocal session = new ThreadLocal();
    private static final SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    public DAO(){
    }

    public static Session getSession(){
        Session session = (Session) DAO.session.get();
        if (session == null){
            session = sessionFactory.openSession();
            DAO.session.set(session);
        }
        return session;
    }
    public void begin(){
        getSession().beginTransaction();
    }

    public void commit(){
        getSession().getTransaction().commit();
    }

    public void rollback(){
        try {
            getSession().getTransaction().rollback();
        }
        catch (HibernateException he){
            log.log(Level.WARNING,"Cannot close",he);
        }
        try {
            getSession().close();
        }
        catch (HibernateException he){
            log.log(Level.WARNING,"Cannot close",he);
        }
        DAO.session.set(null);
    }

    public static void close(){
        getSession().close();
        DAO.session.set(null);
    }
}

Department.java

@Entity
@Table(name = "department")
public class Department implements Serializable {
    @Id
    @GeneratedValue
    private int dept_id;
    @Column(name = "name")
    private String dept_name;
    @Column(name = "head")
    private String head;
    @Column(name = "email")
    private String email;

    public Department() {
    }

}

User.java

@MappedSuperclass
public class User implements Serializable {
    @Id
    @GeneratedValue
    private int id;
    @Column(name = "username", nullable = false)
    private String username;
    @Column(name = "password", nullable = false)
    private String password;

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public User() {
    }

}

Student.java

@Entity
@Table(name = "student")
public class Student extends User implements Serializable{

    @Column(name = "student_id", unique = true)
    private String student_id;
    @Column
    private String full_name;
    @Column
    private String email;
    @ManyToOne
    @JoinColumn(name = "dept_id", nullable = false)
    private Department dept_id;
    @ManyToMany(cascade = CascadeType.PERSIST, targetEntity = Course.class)
    @JoinTable(name = "course_stud")
    private List<Course> courses;

    public Student(String username, String password) {

        super(username, password);
    }

    public Student(String username, String password, String full_name, String email, Department dept_id) {
        super(username, password);
        this.full_name = full_name;
        this.email = email;
        this.dept_id = dept_id;
    }

    public Student() {
    }


    public Department getDept_id() {
        return dept_id;
    }

    public void setDept_id(Department dept_id) {
        this.dept_id = dept_id;
    }


    public List<Course> getCourses() {
        return courses;
    }

    public void setCourses(List<Course> courses) {
        this.courses = courses;
    }


    public String getFull_name() {
        return full_name;
    }

    public void setFull_name(String full_name) {
        this.full_name = full_name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getStudent_id() {
        return student_id;
    }

    public void setStudent_id(String student_id) {
        this.student_id = student_id;
    }
}

Instructor.java

@Entity
@Table(name = "instructor")
public class Instructor implements Serializable {
    private static final long serialID = 1L;
    @Id
    @GeneratedValue
    private int pk;
    @Column(unique = true)
    private String id;
    @Column
    private String name;
    @Column
    private String email;

    public Instructor(String id, String name, String email) {
        this.id = id;
        this.name = name;
        this.email = email;
    }

    public Instructor() {
    }
    public int getPk() {
        return pk;
    }

    public void setPk(int pk) {
        this.pk = pk;
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

Course.java

@Entity
@Table(name = "course")
public class Course implements Serializable {

    @Id
    @GeneratedValue
    private int cid;
    @Column(unique = true)
    private String course_id;
    @Column(name = "course_name")
    private String course_name;
    @Column(name = "description", nullable = true)
    private String description;
    @ManyToOne
    @JoinColumn(name = "dept_id")
    private Department dept_id;
    @ManyToMany(cascade = CascadeType.PERSIST, targetEntity = Instructor.class)
    @JoinTable(name = "course_inst",
            joinColumns = {
                    @JoinColumn(name = "course")},
            inverseJoinColumns = {@JoinColumn(name = "instructor")}

    )
    private List<Instructor> instructors;

    public Course(String course_id, String course_name, String desc) {
        this.course_id = course_id;
        this.course_name = course_name;
        this.description = desc;
    }

    public Course() {
    }


    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }


    public String getCourse_id() {
        return course_id;
    }

    public void setCourse_id(String course_id) {
        this.course_id = course_id;
    }

    public String getCourse_name() {
        return course_name;
    }

    public void setCourse_name(String course_name) {
        this.course_name = course_name;
    }

    public String getDesc() {
        return description;
    }

    public void setDesc(String desc) {
        this.description = desc;
    }


    public Department getDept_id() {
        return dept_id;
    }

    public void setDept_id(Department dept_id) {
        this.dept_id = dept_id;
    }

    public List<Instructor> getInstructors() {
        return instructors;
    }

    public void setInstructors(List<Instructor>instructor) {
        this.instructors = instructor;
    }
}

GroupId.java

@Embeddable
public class GroupId implements Serializable{

    private static final long serialVersionUID = 3686950547855931594L;
    private Integer id;
    private Integer cid;
    private Integer pk;

    public GroupId() {
    }

    public GroupId(int student_id, int course_id, int instructor) {
        this.id = student_id;
        this.cid = course_id;
        this.pk = instructor;
    }

//    @ManyToOne(targetEntity = Student.class)
    public int getId() {
        return id;
    }

    public void setId(int student_id) {
        this.id = student_id;
    }
//    @ManyToOne(targetEntity = Course.class)
    public int getCid() {
        return cid;
    }

    public void setCid(int course_id) {
        this.cid = course_id;
    }
//    @ManyToOne(targetEntity = Instructor.class)
    public int getPk() {
        return pk;
    }

    public void setPk(int instructor) {
        this.pk = instructor;
    }

    @Override
    public String toString() {
        return super.toString();
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }


}

Evaluation.java

@Entity
@Table(name = "evaluation")
@IdClass(GroupId.class)
public class Evaluation implements Serializable{
    @Id
    @GeneratedValue
    private int eval_id;
    @Column
    private int evaluated;
    @Id
    @ManyToOne(targetEntity = Student.class)
    private int id;
    @Id
    @ManyToOne(targetEntity = Course.class)
    private int cid;
    @Id
    @ManyToOne(targetEntity = Instructor.class)
    private int pk;

    public Evaluation(int id, int cid, int pk) {
        this.id = id;
        this.cid = cid;
        this.pk = pk;
    }

    public Evaluation() {
    }


    public int getEval_id() {
        return eval_id;
    }
    public void setEval_id(int eval_id) {
        this.eval_id = eval_id;
    }


//    @JoinColumn(name = "id_id", referencedColumnName = "id")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }


//    @JoinColumn(name = "cid_cid", referencedColumnName = "cid")
    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }


//    @JoinColumn(name = "pk_pk", referencedColumnName = "pk")
    public int getPk() {
        return pk;
    }

    public void setPk(int pk) {
        this.pk = pk;
    }

    public int getEvaluated() {
        return evaluated;
    }

    public void setEvaluated(int evaluated) {
        this.evaluated = evaluated;
    }
}

Answer.java

@Entity
@Table(name = "answer")
public class Answer implements Serializable{

    @Id
    @GeneratedValue
    private int id;
    @Column(name = "answer")
    private int answer;
    @ManyToOne(targetEntity = Question.class)
    private Question question;
    @ManyToOne(targetEntity = Evaluation.class)
    private Evaluation eval;

    public Answer(Evaluation eval, Question question, int answer) {
        this.eval = eval;
        this.question = question;
        this.answer = answer;
    }

    public Answer() {
    }
}

Question.java

    @Entity
    @Table(name = "question")
    public class Question implements Serializable{

        @Id
        private int id;
        @Column(name = "qstring")
        private String qstring;
        @Column(name = "category")
        private int category;

        public Question() {
        }

        public Question(int id) {
            this.id = id;
        }
}

将查询插入评估表:

Query save = DAO.getSession().createQuery("insert into Evaluation(id,cid,pk,evaluated) select s,c, i, 1 from Student s,Course c, Instructor i where s.id=:sid and c.cid=:cid and i.pk=:inst");
            //student,course and instructor are all integer values
            save.setInteger("sid", student);
            save.setInteger("cid", course);
            save.setInteger("inst", instructor);
            int result =  save.executeUpdate();
            dao.commit();

1 个答案:

答案 0 :(得分:1)

@ManyToOne关系定位实体:

@ManyToOne(targetEntity = Student.class)
private int id;

@ManyToOne()
private Student student;