9

Is it wrong idea to put annotation from spring @Component and JPA @Entity on the same class. 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.

Is this some anti-pattern ? Do you have any better solution ?

Smolda
  • 882
  • 5
  • 13
  • 34

3 Answers3

8

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

There's no pretty way of doing what you want since JPA doesn't use the Spring container to instantiate its entities. Think of JPA as a separate ORM container that instantiates and manages the lifecycle of entities (completely separate from Spring) and does DI based on entity relationships only. Ravi Thapliyal

Bean injection inside a JPA @Entity

Community
  • 1
  • 1
Vjeetje
  • 5,314
  • 5
  • 35
  • 57
-1

You should know that the spring annotation @Component by default has singleton scope and the entity shoudn't have this scope its totaly wrong.
Use:

private Entity entity;
@PostConstruct
    public void init() {
        entity = new Entity(); 
}

or in your entity:

@Entity
@Table
@Component
@Scope("request")

in your jsf spring bean :

@Autowired
private Entity entity;
//... getters and setters
Gladishmare
  • 333
  • 3
  • 11
  • 1
    Why not provide a better suggestion to not use `@Component` or similar or make sure it gets a better scope? Annotating entities with a scope annotation is imo bad practice – Kukeltje Dec 21 '15 at 13:56
  • It is not only bad practice IMO but more importantly according to @BalusC (it is even the basic question and the answer is 'bad practice') so your suggestion is even contradictory to the answer... remarkable – Kukeltje Dec 21 '15 at 14:29
  • @Kukeltje can you explain to us why @Scope("request") isn't good practice ? – Gladishmare Dec 21 '15 at 16:17
  • RequestScoped is not bad, it is bad on an entity which is also component. And what I meant is that if the scope of the component in the 'accepted and upvoted' answer is to broad, reduce the scope of **that** component. – Kukeltje Dec 21 '15 at 16:36