Aug 29 2011
Generic Entity Locator for Request Factory in GWT
In 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.
One Comment
Leave a Reply












Apr 10, 2012 @ 23:15:21
Thanks for sharing