在捆绑类中使用EntityManager是否合适?

时间:2013-10-31 19:01:40

标签: database jsf jpa bundle resourcebundle

我想知道JSF在一个应该从数据库中读取消息的bundle类中定义EntityManagerTypedQuery是否有效? 如果我创建一个@Stateless bean的实例并使用其在bundle类中返回查询结果的函数会怎么样?

更新:包含一些代码:

protected class DBControl extends Control{
@Override
public ResourceBundle newBundle
        (String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
        throws IllegalAccessException, InstantiationException, IOException
{            
    return new ArticleResources(locale);
}       

protected class ArticleResources extends ListResourceBundle{
    private Locale locale;

    public ArticleResources (Locale locale){
        this.locale = locale;
    }            
    String language = locale.getLanguage();

    @Override
    protected Object[][] getContents(){                  
        TypedQuery<ArticleLcl> query = em.createNamedQuery("ArticleLcl.findForLocale", ArticleLcl.class);
        query.setParameter("lang", language);
        List<ArticleLcl> articles = query.getResultList();
        Object[][] allArticles = new Object[articles.size()][3];                
        int i = 0;
        for(Iterator<ArticleLcl> it = articles.iterator(); it.hasNext();){
            ArticleLcl article = it.next();
            allArticles[i] = new Object[]{article.getArticleId().getArticleId().toString(),article.getArticleTitle()};
            messages.put(article.getArticleId().getArticleId().toString(),article.getArticleTitle());                    
            i++;
        }
        return allArticles;
    }            
}

顺便说一下,这段代码不起作用,我的实体管理器为空。但我不知道我做对了吗?

0 个答案:

没有答案