Asked  6 Months ago    Answers:  5   Viewed   25 times

Should you place the @Transactional in the DAO classes and/or their methods or is it better to annotate the Service classes which are calling using the DAO objects? Or does it make sense to annotate both "layers"?

 Answers

100

I think transactions belong on the Service layer. It's the one that knows about units of work and use cases. It's the right answer if you have several DAOs injected into a Service that need to work together in a single transaction.

Tuesday, June 1, 2021
 
daiscog
answered 6 Months ago
52

According to the Javadoc for Autowired, the annotation can be used on "a constructor, field, setter method or config method". See the full documentation for more details.

I personally prefer your first option (constructor injection), because the myDao field can be marked as final:

@Controller
public class MyControllear {
    private final MyDao myDao;

    @Autowired
    public MyController(MyDao myDao) {
      this.myDao = myDao;
    }

Constructor injection also allows you to test the class in a unit test without code that depends on Spring.

The second option would be better written as:

@Controller
public class MyControllear {
    @Autowired
    private MyDao myDao;

    MyController() {
    }

With field injection, Spring will create the object, then update the fields marked for injection.

One option you didn't mention was putting @Autowired on a setter method (setter injection):

@Controller
public class MyControllear {
    private MyDao myDao;

    MyController() {
    }

    @Autowired
    public void setMyDao(MyDao myDao) {
      this.myDao = myDao;
    }

You do not have to choose one or another. You can use field injection for some dependencies and constructor injection for others for the same object.

Saturday, June 12, 2021
 
Nate
answered 6 Months ago
84

Put .gitignore in the working directory. It doesn't work if you put it in the .git (repository) directory.

$ ls -1d .git*
.git
.gitignore
Saturday, July 31, 2021
 
some_bloody_fool
answered 4 Months ago
25

You're correct that they are two different approaches to the same problem. Which one is "better" is a matter of taste, I think. Using annotations has the benefit of avoiding Spring import dependencies in your code, and even the Spring JavaDoc for JpaDaoSupport suggests using them for new JPA projects. JpaDaoSupport is there to make Spring's support for JPA equivalent to its support for other ORM strategies (HibernateDaoSupport, JdbcDaoSupport, TopLinkDaoSupport, etc.). In those cases, annotation-based injection isn't an option.

Friday, November 12, 2021
 
gjok
answered 3 Weeks ago
31
  1. You are handling the transaction inside your actionPerformed() method. Its clearly defeating the purpose of DAO/Service layer
  2. Your UserService is accepting AbstractDAO, which means some other code may pass wrong DAO implementation to your UserService that will screw things up

Now, few suggestions.

  1. You can look into GenericDAO concept for this. That might suit your need
  2. Most of the time we ain't need all these layers like Service, DAO and BusinessDelegate. So, question yourself are any of these really answering some of your questions. If not, get rid of them. YAGNI
  3. Get rid of DAO completely, and treat your Hibernate API as your DAO. Handle database transaction in your business methods. You may like to read this question

[Edited]

After your edit my 3rd suggestion doesn't carry much weight. By the way, you name your DAOs as follows; UserJdbcDAO, UserMysqlDAO etc. Your 2nd name is not making much sense, as we use ORMs just to avoid DB vendor specific DAOs/queries. It might start making some sense if your UserMysqlDAO extends UserJdbcDAO.

Friday, November 12, 2021
 
Deyson
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share