Generic Entity Locator for Request Factory in GWT

gwt logoIn my old Gwt projects i have previously used RPC with Dozer for database communications. Now i am using RequestFactory in my new Gwt project.

I’ve written a generic entity locator for all my entities. (thanks to Gwt community)

For generic entity locator we need a entity base class. All entities can extend EntityBase, that provides getId() and getVersion(). And you write in proxy @ProxyFor(value = Category.class, locator = EntityLocator.class)

/**
 * EntityBase, base entity for all entities.
 */
@MappedSuperclass
public abstract class EntityBase implements Serializable {

  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  @Column(name = "id", nullable = false, columnDefinition = "BIGINT UNSIGNED")
  protected Long id;

  @Version
  @Column(name = "version", nullable = false, columnDefinition = "BIGINT UNSIGNED DEFAULT 0")
  private Long version;

  public Long getId() {
    return id;
  }

  public Long getVersion() {
    return version;
  }
}
/**
 * Category entity
 */
@Entity
@Table(name = "category")
@AttributeOverride(name = "id", column = @Column(name = "category_id",
nullable = false, columnDefinition = "BIGINT UNSIGNED"))
public class Category extends EntityBase {

  private static final long serialVersionUID = 1L;

  @Basic(optional = false)
  @Column(name = "name")
  private String name;

  public Category() {
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
  ...
}
/**
 * Entity Locator for all entities
 */
public class EntityLocator extends Locator<EntityBase, Long> {

  @Override
  public EntityBase create(Class<? extends EntityBase> clazz) {
    try {
      return clazz.newInstance();
    } catch (InstantiationException e) {
      throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
      throw new RuntimeException(e);
    }
  }

  @Override
  public EntityBase find(Class<? extends EntityBase> clazz, Long id) {
    // TODO : create your entity manager !
    return entityManager.find(clazz, id);
  }

  /**
   * it's never called
   */
  @Override
  public Class<EntityBase> getDomainType() {
    throw new UnsupportedOperationException();
    // or return null;
  }

  @Override
  public Long getId(EntityBase domainObject) {
    return domainObject.getId();
  }

  @Override
  public Class<Long> getIdType() {
    return Long.class;
  }

  @Override
  public Object getVersion(EntityBase domainObject) {
    return domainObject.getVersion();
  }
}
/**
 * DTO Proxy for Category entity.
 */
@ProxyFor(value = Category.class, locator = EntityLocator.class)
public interface CategoryProxy extends EntityProxy {

  Long getId();

  String getName();
  void setName(String name);

}

This generic entity locator you can use with JPA. But David Chandler has written a sample project “Using Gwt RequestFactory with Objectify“. Perhaps this is also interesting for you.

Happy coding.

My Selected Gwt Applications

I will develop a complex browser-based application therefore i am searching good Gwt Applications in various business areas.  I look the layout designs and well designed gwt widgets in application demos.

There is good Gwt components / frameworks such as ExtGwt, SmartGwt, Vaadin with commercial support. But i will use Gwt widgets with UiBinder if necessary i develop my own widgets so i can have my own developed widgets for future projects.

Below i have listed good Gwt applications :

Google Analytics – The enterprise-class web analytics solution.

google analytics

google analytics

google analytics More »