我跟着这个快速开始:
http://docs.jboss.org/hibernate/orm/5.0/quickstart/html/
问题:我得到了
sessionFactory cannot be resolved to variable
在这一行:
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory(); why?
这是课程的其余部分:
import javax.imageio.spi.ServiceRegistry;
import javax.transaction.Transaction;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import ch.makery.model.Employee;
public class HelloWorld {
protected void setUp() throws Exception {
}
public static void main(String[] args) {
// A SessionFactory is set up once for an application!
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure() // configures settings from hibernate.cfg.xml
.build();
try {
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
}
catch (Exception e) {
// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
// so destroy it manually.
StandardServiceRegistryBuilder.destroy( registry );
}
Session session = sessionFactory.openSession();
session.beginTransaction();
//session.save( new Employee() );
//session.save( new Employee() );
session.getTransaction().commit();
session.close();
}
}
答案 0 :(得分:1)
尝试像这样创建一个变量SessionFactory sessionFactory
:
public static void main(String[] args) {
SessionFactory sessionFactory = null;
try{
...
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
...