【问题标题】:GWT with JPA and Gilead exampleGWT 与 JPA 和 Gilead 示例
【发布时间】:2011-03-21 05:08:27
【问题描述】:

谁能给我一个 GWT + JPA + Gilead 的例子,我似乎在谷歌上找不到任何关于这个主题的东西。

谢谢


谢谢马克西姆,

我没有在 EJB 服务器中使用它,而是在 Tomcat 中使用它。我了解您在上面指出的步骤,但不确定如何执行下一步,即设置 PersistentBeanManager 并通过网络发送我的对象。

这是我迄今为止所拥有的,但我还没有机会测试这是否有效。如果您发现这有问题,请告诉我,谢谢。

私有 HibernateJpaUtil gileadUtil = new HibernateJpaUtil();

私有静态最终 EntityManagerFactory 工厂 = Persistence.createEntityManagerFactory("MyPersistentUnit");

public MyServlet() {

    gileadUtil.setEntityManagerFactory(factory);

    PersistentBeanManager pbm = new PersistentBeanManager();
    pbm.setPersistenceUtil(gileadUtil);
    pbm.setProxyStore(new StatelessProxyStore());

    setBeanManager(pbm);

    Book book = new Book();
    Book cloned = (Book) pbm.clone(book);               

            //send the cloned book over the wire

}

【问题讨论】:

标签: gwt jpa gilead


【解决方案1】:

实体:

//imports
@Entity
public class Book extends LightEntity implements Serializable {

    private static final long serialVersionUID = 21L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String title;

    @Lob
    private String description;

    @ManyToMany(cascade = CascadeType.ALL)
    private List<Author> author;

    // Getters and setters
    @Override
    public int hashCode() {
       int hash = 0;
       hash += (getId() != null ? getId().hashCode() : 0);
       return hash;
    }

    @Override
    public boolean equals(Object object) {
       // TODO: Warning - this method won't work in the case the id fields are not set
       if (!(object instanceof Book)) {
           return false;
       }
       Course other = (Book) object;
       if ((this.getId() == null && other.getId() != null) || (this.getId() != null && !this.id.equals(other.id))) {
           return false;
       }
       return true;
   }
}

Book 对象看起来相同。

然后将其用作服务器上的常规 EJB 和客户端上的常规 DTO。 不要忘记将 Gilead 的库添加到您的项目中。

【讨论】:

    【解决方案2】:

    我尝试将我的项目设置为非常相似,但也遇到了休眠异常。我发现在使用 JPA 时,我需要用EntityManagerFactory 初始化HibernateJPAUtil。当我这样做时,它起作用了。这会将您的前两行代码更改为:

    public class MyServiceImpl extends PersistentRemoteService implements MyService {
    
      public MyServiceImpl() {
        final EntityManagerFactory emf = Persistence.createEntityManagerFactory("MA");    
        final PersistentBeanManager persistentBeanManager = new PersistentBeanManager();
        persistentBeanManager.setPersistenceUtil(new HibernateJpaUtil(emf)); // <- needs EMF here
        persistentBeanManager.setProxyStore(new StatelessProxyStore());
        setBeanManager(persistentBeanManager);
      }
    
      @Override // from MyService
      public Stuff getStuff() {
        // no need for clone/merge here, as Gilead's GWT PersistentRemoteService does this for us
        ...
        return stuff;
      }
    }
    

    我还使用net.sf.gilead.pojo.java5.legacy.LightEntity 作为我所有实体的基类(注意java5.legacy 包)。

    【讨论】:

    • 谢谢,我会试一试的。
    【解决方案3】:

    希望这个博客对您有所帮助。
    http://zawoad.blogspot.com/2010/06/google-app-engine-jdo-and-gxtext-gwt.html

    这不是您想要的直接示例,但方法应该是这样的。我们在 GWT+JPA+EJB 项目中采用了相同的方法。要通过网络发送您的对象,您需要一个数据传输对象 (DTO)。将此 DTO 转换为 Entity 对象,然后做任何你想做的事情。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 2010-10-16
    • 2013-12-27
    • 2011-06-17
    • 1970-01-01
    相关资源
    最近更新 更多