AnnotationConfiguration的方法调用不明确

时间:2019-02-13 13:37:48

标签: java hibernate intellij-idea

在构建sessionFactory时,IntelliJ显示“方法调用是不明确的”,并引用了AnnotationConfiguration和Configuration类中存在的名为addAnnotationClass()的方法。

package com.dendrowen.controllers;

import com.dendrowen.models.User;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * Created by dendrowen on 2/11/17.
 */

@Controller
public class IndexController {

    private static SessionFactory factory;

    @GetMapping("/")
    public String index(Model m) {
        try{
            factory = new AnnotationConfiguration()
                    .configure()
                    .addAnnotatedClass(User.class) //The error underlines ' User.class'
                    .buildSessionFactory();
        }
        catch (Throwable ex){
            System.err.println("Failed to create sessionFactory object." + ex);
            throw new ExceptionInInitializerError(ex);
        }

        Session session = factory.openSession();

        Transaction tx = null;
        Integer id = null;

        try{
            tx = session.beginTransaction();
            User user = new User("John", "Doe", 30000);
            id = (Integer) session.save(user);
            tx.commit();
        }
        catch (HibernateException ex){
            if(tx != null) tx.rollback();
            ex.printStackTrace();
        }
        finally {
            session.close();
        }



        m.addAttribute("someAttribute", id);
        return "index";
    }
}

当我从AnnotationConfiguration类调用该方法时,问题对我来说毫无意义。我尝试先定义一个变量,然后从该变量调用该方法,但这会产生相同的错误。见下文:

        AnnotationConfiguration conf = new AnnotationConfiguration();
        conf.configure();
        conf.addAnnotatedClass(User.class);
        factory = conf.buildSessionFactory();

0 个答案:

没有答案