2

Some days ago I heard about spring-boot. So I started to setup my project from zero, include jpa and dont use older setups from existing projects. But now there is an understanding problem between what I've learned and what I've read about the "easy setup" with spring boot and jpa.

Usually my projects have this structur.

  1. Model (for excample Car)
  2. ModelDao (CarDao with the following code-example)

    @Component
    public class CarDao {
        /** The Hibernate session factory. */
        @Autowired
        private SessionFactory sessionFactory;
        @Transactional
        public void save(Car car) {
            sessionFactory.getCurrentSession().saveOrUpdate(car);
        }
    
  3. CarServiceImpl thats works with DAO´s (includes methods like findAll(), getCarById() or saveCar(Car car))

But now I only read about @Entity and JPA-repositorys. Is it right to say that I dont need models and dao's because I have the @Entity-Annotation? And my ServiceImples and JPA-repositorys have the same functionality? What happend with the SessionFactory? Is all managed automatically?

2 Answers 2

1

You dont need DAO if you are going to use JPA-Repositories.
As well Session-Factory also not required.
Just you need one Class as model and one interface as repository and you all done.

example:

@Entity
@Table(name="COUNTRY")
public class Country {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="COUNTRY_ID", nullable = false)
    private Integer country_id;

    @Column(name="COUNTRY_CODE")
    private String country_code;
    //Add getter and setter
}

interface

public interface CountryRepository  extends PagingAndSortingRepository<Country, Integer> {

}

Yes you need to configure in spring.xml about where your above repository is located

 <jpa:repositories base-package="com.repository" />

create transactionManager in spring.xml

and access it by using below code

ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "spring.xml");
        countryRepository = (CountryRepository) ctx.getBean("countryRepository");
        Country country = countryRepository.findOne(1);
Sign up to request clarification or add additional context in comments.

Comments

0
Is it right to say that I dont need models and dao's because I have the @Entity-Annotation?

@Entity annotation is used on models your POJOs. @Entity maps the POJO i;e class properties to the db table. How would you write your business logic if you get rid of models.

Service layer, DAO layer are all components of application design. They have their specific role. ServiceImpl proverbially manages the transactions whereas DAO/Repository layer manages the communication with the db.

Here your CarDao class should be annotated with @Repository annotation. It is a DAOImpl class.

And all your transactional methods should move to the Service layer.

And my ServiceImples and JPA-repositorys have the same functionality? 

No, as I've already stated they have specific respective functionality. They are not same.

What happend with the SessionFactory? Is all managed automatically?

SessionFactory is always injected into the DAO layer. You can either manage the sessions yourself or let hibernate manage the sessions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.