我正在使用 Mate、Blaze DS、Tomcat 服务器创建一个 Flex 项目。所有这些似乎都在起作用。甚至我的 Hibernate 调用都可以正常工作。但是,它们似乎没有以正确的方式创建。我正在使用 java 中的以下代码块登录用户:
public AbstractUser login(String username, String password){
//Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
//SessionFactory sessionFactory= cfg.buildSessionFactory();
SessionFactory sessionFactory;
sessionFactory = new AnnotationConfiguration()
.addAnnotatedClass(AbstractUser.class)
.addAnnotatedClass(CourseSession.class)
.addAnnotatedClass(Course.class)
.addAnnotatedClass(Message.class)
.addAnnotatedClass(Material.class)
.addAnnotatedClass(TopicSession.class)
.addAnnotatedClass(Step.class)
.addAnnotatedClass(Section.class)
.addAnnotatedClass(Topic.class)
.addAnnotatedClass(Subtopic.class)
.configure("hibernate.cfg.xml")
.buildSessionFactory();
sessionFactory.openSession();
emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
em = emf.createEntityManager();
logger.debug("** username:"+username);
logger.debug("** password:"+password);
Query q = em.createNamedQuery("users.byUnPass");
q.setParameter("username", username);
q.setParameter("password", password);
AbstractUser user;
try{
user = (AbstractUser) q.getSingleResult();
}catch(javax.persistence.NoResultException e){
user = new AbstractUser();
}
return user;
}
这种方法有两个问题 1. entityManager 和 SessionFactory 都需要在按下第一个按钮时自行初始化,从而造成相当大的时间延迟,等待它全部初始化。2.我阅读了hibernate SessionFactory的文档:
我们还推荐一个小的包装类来在静态初始化块中启动 Hibernate,称为 HibernateUtil。您可能已经在 Hibernate 文档的其他区域中以各种形式看到过此类。
现在我有关于如何编写类的文档,但是我应该将它放在我的 flex/java 文件格式中的什么位置?目前在我的 Java 文件结构中: com pegasus tms- 放置第一级服务材料 - 放置我正在使用的材料 pojo 另外,设置 SessionFactory 以使 EntityManager 快速运行是必要的 - 因为每当我有一个打开的函数时我都会注意到EntityManager 然后关闭它,初始化它需要一段时间。
更普遍的问题是,如何以优化的方式设置我的 SessionFactory 和 EntityManager?
提前致谢。
托德