如何在我的视图中调用我的命名查询
我正在努力弄清楚如何调用我的命名查询...
@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您给出的示例看起来像是属于 三层架构。
也就是说,您将拥有一个
ContentService
,它公开诸如findAll()
之类的方法。然后,您的ContentController
将在适当的时间调用此方法,并将生成的List
放入模型中,然后视图(最终!)可以使用该模型。以下是我编写
ContentService
实现的方式(假设已经定义了ContentService
接口):我知道与您所实现的相比,这似乎需要大量工作尝试(从视图中直接访问对象“上”的命名查询),但这是对 的严重违反分离关注点,这是当今每个人都使用的 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 likefindAll()
. Then yourContentController
will call this method at the appropriate time, and place the resultingList<Content>
into the model, which the view can then (finally!) use.Here's how I'd write your
ContentService
implementation (assuming aContentService
interface has already been defined):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 findContent
objects with a newvisible
flag settrue
. 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 yourContent
object, and then call that in thefindAllContent()
method. Nothing else changes.如果您不想使用 Spring,可以使用应用程序管理的 EntityManager。也就是说,只需编写 POJO 并在那里创建一个 EntityManager,如下所示:
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: