Is it wrong idea to put annotation from spring @Component and JPA @Entity on the same class.
This is tight-coupling of the controller and the model.
Why It is needed is to use this class on JSF page and also It describes the table structure. The reason is to avoid mapping Entity object to some value object which will be the presentation layer.
You're overcomplicating things. You do not need to map it to a whole new value object class or so, you can just make the entity a property of the controller.
E.g.
@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;
}
}
and reference it as follows in JSF components:
<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}" />
See, no need to copy all entity's properties in the controller.
See also: