客观化+ JSP:显示 1:N 关系

发布于 2024-12-16 15:30:42 字数 841 浏览 3 评论 0原文

我的bean看起来像这样:

@Entity
public class Fattura {

    @Id
    Long id;

    @NotEmpty
    String numero;

    @Min(value=0)
    Double importo;

    Key<User> utente;

    // gets & sets....
}

“utente”属性是我创建的另一个bean的关键:一个“Fattura”只能有一个“User”,一个“User”可以有许多“Fattura”

我的Spring MVC控制器将管理一个请求 Fattura 列表并在简单的 jsp 中显示它们:

@RequestMapping( value = "/fatture" , method = RequestMethod.GET )
    public ModelAndView leFatture() {

        ModelAndView mav = new ModelAndView("fatture");


        mav.addObject("fatture",fatturaService.listFatture());

        return mav;
    }

jsp 的代码非常简单:表中只有一个 foreach 循环

我的问题是:

如何显示“utente”?

我唯一拥有的是它的密钥,但我想在我的JSP中做类似${fattura.utente.firstName}的事情,我该怎么做?

My bean looks like that:

@Entity
public class Fattura {

    @Id
    Long id;

    @NotEmpty
    String numero;

    @Min(value=0)
    Double importo;

    Key<User> utente;

    // gets & sets....
}

The "utente" property is the key of another bean I created: a "Fattura" can have only one "User", one "User" can have many "Fattura"s

My Spring MVC controller will manage a request for a list of Fattura and display them in a simple jsp:

@RequestMapping( value = "/fatture" , method = RequestMethod.GET )
    public ModelAndView leFatture() {

        ModelAndView mav = new ModelAndView("fatture");


        mav.addObject("fatture",fatturaService.listFatture());

        return mav;
    }

the code of the jsp is really simple: only a foreach cycle in a table

My question is:

how can I display the "utente"?

The only thing I have is its key, but I'd like to do something like ${fattura.utente.firstName} in my JSP, how can I do it?

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

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

发布评论

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

评论(3

半暖夏伤 2024-12-23 15:30:42

不幸的是,您必须在 DAO 类中手动获取“utente”。 Objectify 中没有像 Twig 那样的自动获取功能。在我的 POJO 中,我有以下字段,

@Transient private Organization sender;             // Pickup location (for client RPC)
transient private Key<Organization> senderKey;      // Pickup location (for Datastore)

我从数据存储区加载实体,然后使用 senderKey 手动加载组织。

在新的 Objectify4 中,您将能够执行您想要的操作,如下所示:

class Beastie {
   @Parent
   @Load
   ParentThing parent;

   @Id Long id;

   @Load({"bigGroup", "smallGroup"})
   SomeThing some;

   @Load("bigGroup")
   List<OtherThing> others;

   @Load
   Ref<OtherThing> refToOtherThing;

   Ref<OtherThing> anotherRef;  // this one is never fetched automatically
}

这里是新版本的演变设计文档。

2011 年 11 月 17 日更新:这是一个大新闻。 Twig 作者 John Patterson 今天加入了 Objectify 项目。

Unfortunately you would have to manually fetch "utente" in your DAO class. There is no automatic fetching in Objectify like in Twig. In my POJOs I have following fields

@Transient private Organization sender;             // Pickup location (for client RPC)
transient private Key<Organization> senderKey;      // Pickup location (for Datastore)

I load entity from Datastore and then load manually Organization using senderKey.

In new Objectify4 you'll be able to do what you want like this:

class Beastie {
   @Parent
   @Load
   ParentThing parent;

   @Id Long id;

   @Load({"bigGroup", "smallGroup"})
   SomeThing some;

   @Load("bigGroup")
   List<OtherThing> others;

   @Load
   Ref<OtherThing> refToOtherThing;

   Ref<OtherThing> anotherRef;  // this one is never fetched automatically
}

Here is evolving design document of new version.

Update at Nov 17, 2011: This is big news. Twig author, John Patterson, joined Objectify project today.

熊抱啵儿 2024-12-23 15:30:42

我知道您必须手动获取两个对象听起来很烦人,但实际上知道您要加倍工作和时间来执行此操作非常有用 - 每个“获取”调用都需要一段时间,而第二个调用不会启动直到第一个完成。在典型的 NoSQL 环境中,您不应该经常需要拥有两个独立的实体 - 您这样做有什么原因吗?

我很容易想到的原因只有两个:

  1. 该类引用了同一类型的另一个对象 - 这是 Objectify 文档中的示例,其中一个人引用了其配偶,而配偶也是一个人。< /p>

  2. 您将另一个嵌入到的类(在您的情况下为“Fattura”)中包含大量数据,您不希望在想要获取“用户”的同时获取这些数据 - 并且您需要用户自己比需要“Fattura”和“用户”更频繁。当您确实想要“Fattura”时,需要相当多的数据才值得额外的数据存储调用。

I know it sounds annoying that you have to manually fetch the two objects, but it's actually very useful to know that you're doubling your work and time to do this - each "get" call take a while and the second won't start until the first is complete. It a typical NoSQL environment, you shouldn't often need to have two separate entities - is there a reason that you do?

There are only two reasons I can easily think of:

  1. The class references another object of the same type - this is the example in the Objectify documentation, where a person has a reference to their spouse, who is also a person.

  2. The class that you're embedding the other into ("Fattura" in your case) has masses of data in it that you don't want fetched at the same time as you want to fetch the "User" - and you need the user on it's own more often than you need the "Fattura" and the "User". It would need to be quite a lot of data to be worth the extra datastore call when you DO want the "Fattura".

落日海湾 2024-12-23 15:30:42

您不一定必须使用临时字段来获取对象。

这是可行的:

public User getUtente() {
    Objectify ofy = ObjectifyService.begin();
    return ofy.get(utenteKey); 
}

这当然会在每次调用 getter 时执行数据存储 get() 。您可以通过在 User 实体上使用 @Cached 来改进这一点,以便它们在第一次调用后变成 memcache 调用。 Memcache 很好,但我们可以使用会话缓存做得更好:

public User getUtente() {
    Objectify ofy = myOfyProvider.get();
    return ofy.get(utenteKey); 
}

这里的关键是您需要(通过 myOfyProvider)提供一个绑定到当前请求/线程并且具有会话缓存的 Objectify 实例已启用。 (即,对于任何给定的请求,myOfyProvider.get() 应该返回相同的 Objectify 实例)

在此设置中,每次调用 getter 时都会从会话缓存中返回完全相同的 User 实例,并且不会向数据存储/内存缓存将在该实体初始加载后创建。

You don't necessarily have to use temporary field for just getting a object.

This works:

public User getUtente() {
    Objectify ofy = ObjectifyService.begin();
    return ofy.get(utenteKey); 
}

This will of course do a datastore get() each time the getter is called. You can improve this by using @Cached on your User entity, so they turn into memcache calls after the first call. Memcache is good, but we can do a little better using the session cache:

public User getUtente() {
    Objectify ofy = myOfyProvider.get();
    return ofy.get(utenteKey); 
}

The key thing here is that you need to provide (through myOfyProvider) an instance of Objectify that is bound to the current request/thread, and that has the session cache enabled. (ie, for any given request, myOfyProvider.get() should return the same instance of Objectify)

In this setup, the exact same instance of User will be returned from the session cache each time the getter is called, and no requests to the datastore/memcache will be made after from the initial load of this Entity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文