1

I have recently started studying JSF and have stumbled accross the following two ways to include entities into a Controller Managed bean. One by direct injection with the entity as a ManagedBean, the other with the entity as a non-managed bean simply as an instance variable which is initialized at @PostConstruct.

What are the advantages/disadvantages of one way or another? The second is usually showed as the "right way" however it seems that it's more complicated to maintain.

Non-Managed Entity

@Entity
public class Book {
 //...attributes
}

@ManagedBean
public class BookController {

   private Book book;

   @PostConstruct
   public void init() {
     book = new Book();

   }


}

Managed Entity

@Entity
@ManagedBean
public class Book implements Serializable {
      //...attributes
}

@ManagedBean
public class BookController {

     @ManagedProperty(name="#{customer}")
     private Book book;

}
4

1 回答 1

2

实体有自己的生命周期,并且已经由 JPA 管理。不建议(CDI 规范强烈禁止)将实体声明为托管 bean。最好在您的控制器中保留对实体的引用 - 在您的情况下为非托管实体。

于 2013-05-19T10:57:07.453 回答