Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it wrong to have annotations Entity and Component on the same class

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 ?

like image 818
Smolda Avatar asked Aug 25 '13 07:08

Smolda


People also ask

Can we use both @component and @service annotation in same class?

We can use @Component across the application to mark the beans as Spring's managed components. Spring will only pick up and register beans with @Component, and doesn't look for @Service and @Repository in general. @Service and @Repository are special cases of @Component.

Can an interface be annotated with @component?

Interfaces are annotated with @Component annotation in spring IoC/DI.

Can I replace @service with @component?

@Repository @Service and @Controller are serves as specialization of @Component for more specific use on that basis you can replace @Service to @Component but in this case you loose the specialization.

What is the difference between @component and ComponentScan?

@Component and @ComponentScan are for different purposes. @Component indicates that a class might be a candidate for creating a bean. It's like putting a hand up. @ComponentScan is searching packages for Components.


2 Answers

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:

  • Passing a JSF2 managed pojo bean into EJB or putting what is required into a transfer object
like image 165
BalusC Avatar answered Nov 10 '22 00:11

BalusC


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

like image 40
Vjeetje Avatar answered Nov 09 '22 23:11

Vjeetje