hibernate:实体有2个ManyToOne外键到2个不同的表不起作用

时间:2017-02-25 16:47:08

标签: hibernate foreign-keys

以下是我的Score实体的一部分。 属性student链接到表格student,属性exam链接到表格exam

@Entity
@Table(name = "score")
public class Score implements Serializable {
    private Student student;
    private Exam exam;
    private int score;

    @ManyToOne
    @JoinColumn(name="sid")  
    public Student getStudent() {
        return student;
    }
    public void setStudent(Student student) {
        this.student=student;
    }

    @ManyToOne
    @JoinColumn(name="eid")  
    public Exam getExam() { return exam;}

    public void setExam(Exam exam) {
        this.exam = exam;
    }

    @Basic
    @Column(name = "score")
    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

表格Score的格式为sideidscore

我用工厂创建了sessionfactory

public class ConfigureFactory {
    private static Configuration config;
    private static SessionFactory sessionFactory;
    public static Configuration getConfiguration(){
        if(config==null){
            config = new Configuration().configure();
            config.addAnnotatedClass(Exam.class);
            config.addAnnotatedClass(Score.class);
            config.addAnnotatedClass(Course.class);
            config.addAnnotatedClass(Teacher.class);
            config.addAnnotatedClass(SelectC.class);
            config.addAnnotatedClass(Student.class);
            config.addAnnotatedClass(User.class);
        }
        return config;
    }
    public static SessionFactory getSessionFactory(){
        if(sessionFactory==null){
            getConfiguration();
            sessionFactory=config.buildSessionFactory();
        }
        return sessionFactory;
    }
    public static void closeSessionFactory(){
        if(sessionFactory!=null){
            sessionFactory.close();
        }
    }
}

即使我对数据库一无所知,如下面的代码,它仍然会引发错误。

ServiceRegistry  serviceRegistry =new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
SessionFactory sf = config.buildSessionFactory(serviceRegistry); 
Session sess = sf.openSession();  
sess.close();  
sf.close();  

the error message in console 而且我确定配置没什么,因为我有另外一个带有一个外键的实体,我成功地从数据库中获取数据。 那有什么不对??

0 个答案:

没有答案