Categories
Uncategorized

I really do not want to use the JPA

In the developer’s circle, not a technology when it comes to good or bad, will lead to intense or not intense debate until a developer came out and said PHP is the best language in the world, it will have receded Huoer continue to write code.

Today the question is not to say JPA would like to discuss or what caused the war of words, simply that I do not like JPA. Yes, that is so Real.

When it comes to Java development, involving database access, mainly on two frameworks, one MyBatis, the other is JPA. JPA is said to use more foreign and domestic MyBatis with more. Why use more domestic MyBatis it, because the whole legend Ali are using it.

JPA stands for Java Persistence API, its purpose is to help us to improve development efficiency, which is the core Java Persistence Query Language (JPQL), an entity stored in a relational database query. On the SQL query syntax is similar, but the operation is the solid object rather than a database table to operate. (Taken from wiki)

Using JPA development process is as follows:
    1, the mapping database tables to the project entity
    2, generates a corresponding Repository
    3, to achieve Service, Service call Repository
    JPA save you a thing in place in the Repository, for those relatively simple logic, such as single-table queries, the name can be achieved directly from the query logic. For most queries, it is really very provincial thing. But beginning with, I do feel a little strange.

Indeed, if you used the JPA, there are times when it does have a lot to enhance the development efficiency, JPA want to do is try to make you less write sql, not even write sql. Based on this idea, JPA implements its own set of syntax, annotation rules.

JPA annotations with a variety of use to achieve many among data entities, etc.-many relationship. Because of this, I think it becomes an entity is not simply physical, but doping logic in it, but also increases the complexity of the entity, for more complex business, it is very likely to cause direct or indirect circular references between entities .

If you want to use JPA, in addition to master a variety of notes, but for a slightly more complex queries, but also to grasp the set of its wording, this kind of code such as the following:

    Specification specification = (root, criteriaQuery, criteriaBuilder) -> {
            Predicate p = criteriaBuilder.equal(root.get("deleted").as(Boolean.class), false);
            Predicate news = criteriaBuilder.equal(root.get("cntntType"), ContentType.CNTNTTP_NEWS.name());
            Predicate salon = criteriaBuilder.equal(root.get("cntntType"), ContentType.CNTNTTP_SALON.name());
            Predicate type = criteriaBuilder.or(news, salon);
        ...

And you want to implement a join query is strenuous enough, in addition to the above set of codes to write, but also in the physical tricks, think want to cry, there are none. Do not you direct write sql, so why spoil yourself.

Another point, JPA annotations after some were using the database will affect the level of, say, the key to foreign key notes, if you use the default settings, this really will be the foreign key table to the database, built on the foreign key table . There are some other ORM framework, too, which is totally unacceptable to me, why, why change something in my database.

I would like to participate in the project no JPA. The company has a project to use the JPA, I also participated in part of the code written is not that much, so I have a headache in addition to outside, do not appreciate the slightest advantage of the JPA, which of course is probably due to my limited level, or I wrote the code JPA enough, or I did not understand the essence of the JPA. In short Anyway, I’m sorry, I will not be willing to come JPA.

Of course, to say definitely biased, some students might scoffed. Yes, a colleague is this to say: there is a reasonable thing, JPA so many years, if not by how so many people will use, but also many foreign JPA user, do people have problems.

If you simply project, the business is not complicated, is not complicated to join did not even have a project, you can use JPA, under other circumstances, it is really not the best. JPA with the feeling is like tied the hands and feet, loss of freedom. Liberty, or give me death. Even offer all kinds of good, less freedom, and I reject it. The MyBatis precisely to a framework developer freedom.

Again, no liberty, or give me death. This is the first, and probably the last project with the JPA.

Do not begrudge your “Recommended” Yo

欢迎关注,不定期更新本系列和其他文章
古时的风筝 ,进入公众号可以加入交流群

Leave a Reply