如何在我的视图中调用我的命名查询

发布于 2024-12-24 04:06:15 字数 918 浏览 2 评论 0原文

我正在努力弄清楚如何调用我的命名查询...

@NamedQueries({
    @NamedQuery(name = "Content.findAll", query = "SELECT c FROM Content c"),
    @NamedQuery(name = "Content.findById", query = "SELECT c FROM Content c WHERE c.id = :id"),
    @NamedQuery(name = "Content.findByUserId", query = "SELECT c FROM Content c WHERE c.userId = :userId")})
public class Content implements Serializable {
...

在我看来,我已经尝试了一些变体,但似乎无法确定正确的用法。

<ol> <%
    List<Content> contentList = model.Content.findAll();
    if (contentList != null) {
        for (Content content : contentList) { %>
            <li> <%= content %> </li> <%
        }
    } %>
</ol>

在 Google 上,我不断找到人们使用的结果,例如:

List results = em.createNamedQuery("findAll").getResultList();

我应该在视图中引用 em,还是在模型中引用?我似乎找不到一个可靠的例子来帮助我了解全貌。

I'm struggling to figure out how to call my named query...

@NamedQueries({
    @NamedQuery(name = "Content.findAll", query = "SELECT c FROM Content c"),
    @NamedQuery(name = "Content.findById", query = "SELECT c FROM Content c WHERE c.id = :id"),
    @NamedQuery(name = "Content.findByUserId", query = "SELECT c FROM Content c WHERE c.userId = :userId")})
public class Content implements Serializable {
...

In my view, I've tried a few variations of this, but can't seem to pinpoint the correct usage.

<ol> <%
    List<Content> contentList = model.Content.findAll();
    if (contentList != null) {
        for (Content content : contentList) { %>
            <li> <%= content %> </li> <%
        }
    } %>
</ol>

On Google I keep finding results where people have usage like:

List results = em.createNamedQuery("findAll").getResultList();

Should I have a reference to em within my view, or is this referring to within a model? I can't seem to find a solid example to help me see the full picture.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷乱花海 2024-12-31 04:06:15

您给出的示例看起来像是属于 三层架构

也就是说,您将拥有一个 ContentService,它公开诸如 findAll() 之类的方法。然后,您的 ContentController 将在适当的时间调用此方法,并将生成的 List 放入模型中,然后视图(最终!)可以使用该模型。

以下是我编写 ContentService 实现的方式(假设已经定义了 ContentService 接口):

public class ContentServiceImpl implements ContentService {

    @Autowired
    private EntityManager em; // Gets wired in by Spring

    public List<Content> findAllContent() {
        return em.createNamedQuery("Content.findAll").getResultList();
    }

    ...
}

我知道与您所实现的相比,这似乎需要大量工作尝试(从视图中直接访问对象“上”的命名查询),但这是对 的严重违反分离关注点,这是当今每个人都使用的 MVC 模式背后的主要思想。

想象一下,如果您被告知 findAll 应该实际上只查找设置了新 visible 标志的 Content 对象,会发生什么情况。使用您当前的方法(如果它有效的话),您必须更改您的视图 - 当更改应该隔离到更低的层时。

现在想想上面的实现需要做什么。在 Content 对象上编写一个新的 @NamedQuery,然后在 findAllContent() 方法中调用 that。其他什么都没有改变。

The example you've given looks like it belongs in the Service Layer of a Three-Tier Architecture.

That is, you will have a ContentService which exposes methods like findAll(). Then your ContentController will call this method at the appropriate time, and place the resulting List<Content> into the model, which the view can then (finally!) use.

Here's how I'd write your ContentService implementation (assuming a ContentService interface has already been defined):

public class ContentServiceImpl implements ContentService {

    @Autowired
    private EntityManager em; // Gets wired in by Spring

    public List<Content> findAllContent() {
        return em.createNamedQuery("Content.findAll").getResultList();
    }

    ...
}

I know it seems like a lot of work when compared to what you've attempted (directly accessing a named query "on" an object, from the view), but it's a pretty major violation of separation of concerns, which is the main idea behind the MVC pattern that everyone uses these days.

Think about what happens if you get told that findAll should actually only find Content objects with a new visible flag set true. With your current approach (if it even worked) you'd have to change your view - when the change should be isolated to a much lower layer.

Now think of what has to be done with the above implementation. Write a new @NamedQuery on your Content object, and then call that in the findAllContent() method. Nothing else changes.

情痴 2024-12-31 04:06:15

如果您不想使用 Spring,可以使用应用程序管理的 EntityManager。也就是说,只需编写 POJO 并在那里创建一个 EntityManager,如下所示:

private EntityManagerFactory emf;
private EntityManager em;

public List findAll(){
   em.Persistence.createEntityManagerFactory("your-persistence-unit-name");
   return results = em.createNamedQuery("findAll").
}

You can use Application managed EntityManager if you don't want to use Spring. That is, just write POJO and create an EntityManager there like this:

private EntityManagerFactory emf;
private EntityManager em;

public List findAll(){
   em.Persistence.createEntityManagerFactory("your-persistence-unit-name");
   return results = em.createNamedQuery("findAll").
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文