Passing a JSF2 managed pojo bean into EJB or putting what is required into a transfer object

后端 未结 2 1548
花落未央
花落未央 2020-12-04 03:35

Currently i am calling EJB 3 Session Beans from JSF 2. However, i am not sure if i should be passing JSF managed beans into EJB?

Assuming t

2条回答
  •  遥遥无期
    2020-12-04 04:26

    It sounds like as if you've tight-coupled the model with the controller like as shown in most basic JSF tutorials. You should decouple the model from the controller into its own class. As you're using EJBs, the chance is big that you're also using JPA (how else would EJBs be really useful for persistence?), you can just use the existing JPA @Entity class as model.

    E.g.

    @Entity
    public class Product {
    
        @Id
        private Long id;
        private String name;
        private String description;
        private Category category;
    
        // ...
    }
    

    with

    @ManagedBean
    @ViewScoped
    public class ProductController {
    
        private Product product;
    
        @EJB
        private ProductService service;
    
        public void save() {
            service.save(product);
        }
    
        // ...
    }
    

    which is to be used as

    
        
        
        
            
        
        
    
    

提交回复
热议问题