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

  • Home
  • SEARCH
  • 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 6603357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:59:54+00:00 2026-05-25T18:59:54+00:00

The code below is what Spring Roo generates by default, the EntityManager is injected

  • 0

The code below is what Spring Roo generates by default, the EntityManager is injected in your domain model POJOs with the rest of the methods to manage the entity (save, update, delete, findXXX, …).

Maybe it is a more object-oriented approach (in contrast to the anemic domain model), but what I don’t understand is:

  • Is there any performance issue when EntityManager is injected in every entity (imagine you retrieve 1000 entities from a database)

  • Shouldn’t the transactional management (@Transactional annotations) go in a service layer? (imagine you want to operate with two different entities in an atomic way).

  • Can you think about other pros/cons of this code against a classical DAO layer?

The code looks like this (some methods are removed for clarity):

@Configurable
@Entity
@RooJavaBean
@RooToString
@RooEntity
public class Answer {

    @PersistenceContext
    transient EntityManager entityManager;

    @Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

    @Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Answer attached = Answer.findAnswer(this.id);
            this.entityManager.remove(attached);
        }
    }

    @Transactional
    public Answer merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Answer merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

    public static final EntityManager entityManager() {
        EntityManager em = new Answer().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

    public static long countAnswers() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Answer o", Long.class).getSingleResult();
    }

    public static List<Answer> findAllAnswers() {
        return entityManager().createQuery("SELECT o FROM Answer o", Answer.class).getResultList();
    }

    ...
}
  • 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-25T18:59:55+00:00Added an answer on May 25, 2026 at 6:59 pm
    1. You will find more on this at the link in the third point.

    2. You don’t get a Service layer in a typical Roo application. Your service methods are contained within the entity itself, hence it is possible to use @Transactional within your entity to ensure the particular method involves in a transaction. However, you will be able to get a separate service layer with the latest 1.2 version of Spring Roo, which will make it possible.

    3. ADM vs. DDD : A separate question on SO would help on this. Anyways, you can gain a lot of insight with this thread on SpringSource Roo forum. http://forum.springsource.org/showthread.php?77322-Spring-Roo-with-a-services-DAO-architecture

    Cheers and all the best with Roo! 🙂

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

Sidebar

Related Questions

We use the code below to inject Spring beans with properties from a properties
I'm trying to understand Spring 3.0 authentication. In the code below, why is user.getRole()
Working with spring, below, the code of applicationContext-service.xml: <bean id=mediaObjectService class=path.MediaObjectServiceImpl> <property name=mediaObjectDao >
In the code below I use mpf_add to add the string representation of two
I tried the following code in LINQPad and got the results given below: List<string>
I'm writing code to do Xml serialization. With below function. public static string SerializeToXml(object
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
Code below is not working as expected to detect if it is in design
The code below shows a sample that I've used recently to explain the different
The code below gives me this mysterious error, and i cannot fathom it. I

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.