GWT 应用程序引擎中的代码共享

发布于 2024-08-11 19:36:04 字数 1409 浏览 6 评论 0原文

我有一个 Employee 类,

 @PersistenceCapable(identityType = IdentityType.APPLICATION)
    public class Employee {
        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key key;

        @Persistent
        private String firstName;

        @Persistent
        private String lastName;

        @Persistent
        private Date hireDate;

        public Employee(String firstName, String lastName, Date hireDate) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.hireDate = hireDate;
        }

        // Accessors for the fields.  JDO doesn't use these, but your application does.

        public Key getKey() {
            return key;
        }

        public String getFirstName() {
            return firstName;
        } 
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        } 

        public String getLastName() {
            return lastName;
        } 
        public void setLastName(String lastName) {
            this.lastName = lastName;
        } 

        public Date getHireDate() {
            return hireDate;
        } 
        public void setHireDate(Date hireDate) {
            this.hireDate = hireDate;
        } 
    }

我已将 JDO 用于应用程序引擎。现在我想在服务器和客户端之间共享此代码。我应该把这个放在哪个包裹里。事实上我两种方式都尝试过。都没有成功。如果您已经完成了此类代码,请分享。

I have an Employee class

 @PersistenceCapable(identityType = IdentityType.APPLICATION)
    public class Employee {
        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key key;

        @Persistent
        private String firstName;

        @Persistent
        private String lastName;

        @Persistent
        private Date hireDate;

        public Employee(String firstName, String lastName, Date hireDate) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.hireDate = hireDate;
        }

        // Accessors for the fields.  JDO doesn't use these, but your application does.

        public Key getKey() {
            return key;
        }

        public String getFirstName() {
            return firstName;
        } 
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        } 

        public String getLastName() {
            return lastName;
        } 
        public void setLastName(String lastName) {
            this.lastName = lastName;
        } 

        public Date getHireDate() {
            return hireDate;
        } 
        public void setHireDate(Date hireDate) {
            this.hireDate = hireDate;
        } 
    }

I have used the JDO for the app engine. Now I want to share this code between server and client. In which package should I keep this. In fact I have tried both way. Neither worked out. Please share if you have already done this type of codes.

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

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

发布评论

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

评论(4

小鸟爱天空丶 2024-08-18 19:36:04

如果您正在寻找的是在客户端和服务器中实例化实体,那么将类放在“client”包下就可以了。

但是,如果您尝试通过 RPC 传递持久实体,那么这可能无法开箱即用。 DataNucleus“增强”了字节码,那么 RPC 就无法序列化。 Hibernate也有类似的问题,请看一下这篇文章,它解释了很好地解决了问题并提出了替代方案。

我正在创建 DTO 来解决这个问题。这需要更多的工作,但这实际上取决于您拥有多少个实体。

If what you are looking for is instantiating of your entities in both client and server, putting the classes under the "client" package will do the trick.

But if you are trying to pass your persistent entities through RPC, that probably wont work out of the box. DataNucleus "enhaces" the bytecode, and RPC can't serialize then. Hibernate has a similar problem, please take a look at this article, it explains the problem very well and presents alternatives.

I am creating DTOs to workaround this problem. It is a little more work, but it really depends on how many Entities you have.

狠疯拽 2024-08-18 19:36:04

我以前做过这个,但只是在一个小型测试应用程序中。假设您正在使用 GWT-RPC,它应该工作得非常顺利。您必须做两件事:

  1. 将代码放在“客户端”命名空间中,即放在由 GWT 编译的目录中。您仍然可以在服务器上使用此代码。
  2. 点击编译并开始修复错误。您会发现的主要问题是“Key”类型在 GWT 领域中不可用。您可以改用字符串编码的密钥。请参阅相关文档。

如果您不使用 GWT-RPC,那么您就得靠自己了。 JSON 对于此目的很有吸引力,但需要大量的跑腿工作。这在 GWT 2.0 中应该会更好,但不会完全消失。

I've done this before, but just in a small test app. Assuming you're using GWT-RPC, it should work pretty smoothly. You'll have to do two things:

  1. Put the code in a 'client' namespace, i.e. in a directory that's getting compiled by GWT. You can still use this code on the server.
  2. Hit compile and start fixing errors. The main one you'll find is that the 'Key' type isn't available in GWT land. You can use a string-encoded key instead. See the "key as encoded string" section in the relevant documentation.

If you're not using GWT-RPC, you're on your own. JSON is attractive for this purpose but requires significant legwork. This should be better in GWT 2.0 but won't entirely go away.

面如桃花 2024-08-18 19:36:04

我们可能需要更多详细信息,因为您可能会遇到许多问题,但这里有一些提示:

  • 只要 GWT 编译器和 javac 都能看到它,包并不重要。我将共享代码保存在一个适当命名的包中......“共享”。 :)

  • 密钥在 GWT 中不可用,因此请使用 编码字符串密钥

  • JDO 很棘手,但可行。较新版本的 GWT(Java AppEngine 发布后)已经能够处理 DataNucleus 的 JDO 增强功能。我会确保您正在使用主干或最近的快照,以防 DataNucleus 成为您的问题。

  • 确保在将对象发送到客户端之前将其分离。

We probably need more detail, as you could be hitting a number of problems, but here's some tips:

  • The package doesn't matter so much as long as both the GWT compiler and javac can see it. I keep shared code in a package appropriately named... "shared". :)

  • Key is not available in GWT, so use an encoded string Key.

  • JDO is tricky, but workable. Newer versions of GWT (after Java AppEngine was released) have been able to handle DataNucleus' JDO enhancement. I'd make sure you are working off of trunk or a recent snapshot, in case DataNucleus is your problem.

  • Make sure you detach your objects before sending them to the client.

↘紸啶 2024-08-18 19:36:04

这就是我使用低级 api 的原因。我编写了一个辅助类,用于将实体转换为 pojo 并返回。这样,我就得到了实体,该实体被转换为我想要的 POJO,然后发送给客户端。从客户端,相同的 POJO 返回到服务器,由我的帮助器类转换为实体,然后一个简单的“put”调用就可以完成任务。您不需要分离/附加任何东西...如果您愿意,我可以分享一些代码。

That's why I am using the low-level api. I wrote a helper class that converts an entity to a pojo and back. This way, I get the Entity which gets converted into my desired POJO that then goes to the client. From the client, the same POJO goes back to the server gets converted into an entity by my helper class and then a simple "put" call does the trick. You don't need to dettach/attach anything... I can share some code if you want.

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