我有一个有效的“请求工厂”示例,我想对其进行重构,以便可以将“persist()”和“remove()”等通用方法从域对象中移出到通用定位器中。目前我有以下(工作)代码:
一个通用超类,包含所有域对象的 id 和版本:
@MappedSuperclass
public class EntityBase {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Version
@Column(name = "version")
private Integer version;
// setter & getter
}
域对象。它有 persist() 和 remove() 方法,我想从类中重构它们:
@Entity
@Table(name = "article")
public class Article extends EntityBase{
public static Article findArticle(Long id) {
//find article
}
public void persist() {
// persist
}
public void remove() {
// remove
}
}
域对象的代理对象:
@ProxyFor(value = Article.class)
public interface ArticleProxy extends EntityProxy {
// some getter and setters
}
我的域对象的请求对象:
@Service(value = Article.class)
public interface ArticleRequest extends RequestContext {
Request<ArticleProxy> findArticle(Long id);
InstanceRequest<ArticleProxy, Void> persist();
InstanceRequest<ArticleProxy, Void> remove();
}
我的请求工厂:
public interface MyRequestFactory extends RequestFactory {
ArticleRequest articleRequest();
}
--------------------------------------
现在我的重构代码不再工作了:
我从域对象中删除了 persist() 和 remove() 方法:
@Entity
@Table(name = "article")
public class Article extends EntityBase{
public static Article findArticle(Long id) {
//find article
}
}
我像这样创建了我的定位器,并在此处添加了方法“remove()”和“persist()”(以及其他默认方法):
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) {
return null;
}
@Override
public Class<EntityBase> getDomainType() {
return null;
}
@Override
public Long getId(EntityBase domainObject) {
return null;
}
@Override
public Class<Long> getIdType() {
return null;
}
@Override
public Object getVersion(EntityBase domainObject) {
return null;
}
public void persist(EntityBase domainObject){
// persist something
}
public void remove(EntityBase domainObject){
// remove
}
}
我的代理对象链接到定位器(locator=EntityLocator.class):
@ProxyFor(value = Article.class, locator=EntityLocator.class)
public interface ArticleProxy extends EntityProxy {
// getter and setters here
}
我的新请求对象如下所示。我将“InstanceRequests”改为“Requests”,根据我在定位器中的新方法更改了返回类型和参数:
@Service(value = Article.class)
public interface ArticleRequest extends RequestContext {
Request<ArticleProxy> findArticle(Long id);
Request<Void> persist(ArticleProxy article);
Request<Void> remove(ArticleProxy article);
}
但是现在我收到错误“找不到类似于 java.lang.Void persist() 的域方法”,用于 persist() 和 remove() 方法。为什么 EntityLocator 中的查找不起作用?我需要 ServiceLocator 吗?我没有完全理解谷歌教程,并且链接的示例不再可用。