Finally I can complain and express my opinions for those who have stopped using ORM for a long time.
I often see people who write Java the backend write a lot of model objects, and various inheritance and combinations are used, as @chanjarster said, one-to-one, one-to-many, many-to-many, single item and two-way , I was immediately confused...
In fact, I personally think ORM 是一个很糟糕的发明,ORM 唯一的好处就是不用考虑 SQL,简直哭晕,SQL 是一个多么好的东西,现在对于大部分的 Java 程序员来说,写个 SQL is a very bad invention. The only advantage of ORM is that you don’t need to consider SQL. I just cry. SQL is such a good thing. Now for everyone For some
programmers, writing SQL shouldn’t be a big problem. It’s much simpler than memorizing various object mapping rules here...
ORMSummarize the shortcomings
The performance is not high. Even with a mature framework, sometimes I only need to access a property and still load the entire object's data. (PS: It may be configurable, but the learning cost is also high)
Not flexible enough. Many business needs require table lookups, function calls, stored procedures...
Your question is so big. To put it simply, it is to figure out the relationship between Model and Model, which has three dimensions
"Reference relationship" or "Owning relationship". If A owns B, then when A is deleted, B will also be deleted. If A refers to B, then if A deletes B, it does not need to be deleted. This depends on the business.
Single or two-way. This depends on the business, you can make your own judgment, but remember one thing, under the "reference relationship", try to keep it one-way. Under "owning relationships", bidirectionality is OK.
One-to-one, one-to-many, many-to-one, many-to-many. I won’t go into details about this, but there is one thing to note: try to avoid many-to-many. If you use many-to-many, ask yourself whether you must do this.
If you must ask about the methodology, you can look at the Aggregate Root concept in DDD (Domain Driven Design).
Finally I can complain and express my opinions for those who have stopped using
ORM
for a long time.I often see people who write
Java
the backend write a lot of model objects, and various inheritance and combinations are used, as @chanjarster said, one-to-one, one-to-many, many-to-many, single item and two-way , I was immediately confused...In fact, I personally think
programmers, writingORM
是一个很糟糕的发明,ORM 唯一的好处就是不用考虑SQL
,简直哭晕,SQL
是一个多么好的东西,现在对于大部分的Java
程序员来说,写个SQL
is a very bad invention. The only advantage of ORM is that you don’t need to considerSQL
. I just cry.SQL
is such a good thing. Now for everyone For someSQL
shouldn’t be a big problem. It’s much simpler than memorizing various object mapping rules here...ORM
Summarize the shortcomingsThe performance is not high. Even with a mature framework, sometimes I only need to access a property and still load the entire object's data. (PS: It may be configurable, but the learning cost is also high)
Not flexible enough. Many business needs require table lookups, function calls, stored procedures...
Abstraction is too cumbersome and I get confused
To sum up, if you agree with it, then give up
ORM
Alternatives:
Use
MyBaits
直接写SQL
for flexible controlUse
NOSQL
数据库,把对象转换为JSON
数据,可以直接存储,如MongoDb
Your question is so big. To put it simply, it is to figure out the relationship between Model and Model, which has three dimensions
"Reference relationship" or "Owning relationship". If A owns B, then when A is deleted, B will also be deleted. If A refers to B, then if A deletes B, it does not need to be deleted. This depends on the business.
Single or two-way. This depends on the business, you can make your own judgment, but remember one thing, under the "reference relationship", try to keep it one-way. Under "owning relationships", bidirectionality is OK.
One-to-one, one-to-many, many-to-one, many-to-many. I won’t go into details about this, but there is one thing to note: try to avoid many-to-many. If you use many-to-many, ask yourself whether you must do this.
If you must ask about the methodology, you can look at the Aggregate Root concept in DDD (Domain Driven Design).