将spring @Component和JPA @Entity中的注释放在同一个类上是错误的想法.
这是控制器和模型的紧密耦合.
为什么需要在JSF页面上使用这个类,它还描述了表结构.原因是避免将Entity对象映射到将作为表示层的某个值对象.
你的事情太复杂了.您不需要将它映射到一个全新的值对象类左右,您可以只将该实体作为控制器的属性.
例如
@Component // Or CDI @Named or JSF @ManagedBean
public class Controller {
private Entity entity;
@AutoWired // Or CDI @Inject or JSF @EJB
private EntityService service;
@PostConstruct
public void init() {
entity = new Entity(); // In case "new entry" is required.
}
public void save() {
service.save(entity);
}
public Entity getEntity() { // No setter required.
return entity;
}
}
Run Code Online (Sandbox Code Playgroud)
并在JSF组件中引用如下:
<h:inputText value="#{controller.entity.name}" />
<h:inputText value="#{controller.entity.value}" />
<h:inputText value="#{controller.entity.something}" />
...
<h:commandButton value="save" action="#{controller.save}" />
Run Code Online (Sandbox Code Playgroud)
请参阅,无需复制控制器中的所有实体属性.