Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6947661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:43:18+00:00 2026-05-27T13:43:18+00:00

I see a lot of examples marking beans as entity beans (@Entity) & named

  • 0

I see a lot of examples marking beans as entity beans (@Entity) & named beans (CDI), so as to avoid creating 2 classes (managed bean & entity bean) and also to make use of Bean Validation so that validation can be performed on both client & server.

So should I be using a single class or not, are there any issues or should I be having my managed beans or service layer create entity beans using the data from managed beans ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:43:18+00:00Added an answer on May 27, 2026 at 1:43 pm

    The @Named or @ManagedBean annotations are typically used to let the bean container (CDI/JSF) create an instance of a bean on demand when referenced by expression language in JSF.

    For an @Entity bean, it often doesn’t make that much sense to just get an arbitrary new instance. An @Entity is very strongly connected to a persistent identity. Such an entity is therefor requested from the Entity Manager and not from a bean container.

    The typical pattern is to have a (slim) backing bean that’s named making a call to a service (which is in turn typically @Stateless in Java EE). The service then returns entities.

    In some very trivial systems people sometimes do make the service named and thus directly available to EL. However, eventually you often want to let the “backing code” generate faces messages or handle (table) selections, which are all things that should not be the concern of a pure business service.

    Another common shortcut is letting the backing bean contain business code directly (e.g. the entity manager that retrieves the entities). This makes the business code hard to re-use, but if the app is trivial and there’s no need for re-use you might get away with it.

    But letting the entity -be- the backing bean is rare and anti to the common Java EE patterns.

    Just note that the backing bean can return the entity directly, so bean-validation can still be used. There is no need whatsoever for the strange ‘scatter/gather’ pattern that crept up a long time ago (See the second example in this question).

    E.g.

    @ViewScoped
    @ManagedBean
    public class BackingBean {
    
         private SomeEntity myEntity; // + getter
    
         @EJB  
         private Service service;
    
         @PostConstruct
         public void init() {
             myEntity = service.getSomeEntity();
         }
    
         public void save() {
             service.save(myEntity);
             FacesContext.getCurrentInstance().addMessage(..., ...);
         }
     }
    

    Assuming SomeEntity in an @Entity annotated bean, bean validation can now be used on a Facelet like:

    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
    >    
        <h:body>   
            <h:form>      
                <h:inputText value="#{backingBean.myEntity.name}" />                        
                <h:commandButton value="Save" action="#{backingBean.save}" />
            </h:form>            
        </h:body>
    </html>
    

    If there’s a constraint on SomeEntity.name it will be validated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I see a lot of examples of how to use StructureMap in a asp.net
I've see a lot of MVC examples where domain-objects are passed directly to views,
I see there are lot's of examples in Ext JS where instead of actually
I see a lot of example code for C# classes that does this: public
I see a lot of examples how to write String objects like that: String
I see lot of example opening popup with jquery but don't see anything that
I see lot of new machines and laptops now having 64 bit hardware and
I see a lot of talk on here about functional languages and stuff. Why
I see a lot of IoC frameworks for .Net and Java. Does anyone know
I see a lot of code like this: function Base() {} function Sub() {}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.