What are JPA entities?

别来无恙 提交于 2019-12-10 17:27:25

问题


I am starting to use JPA and I always get confused with the term of entities and their usage, I have read a lot but I still don't quite get it. I read the Oracle documentation of it but it does not really explain its role in the transaction.

What are JPA enities? does they actually hold the data for each row, I mean, are they stored instances that hold the row data? or they just map tables of the db and then insert and delete in them?

for example if I use this:

 entity.setUserName("michel");

Then persisting it, then changing the user name, and persisitig it again (i.e merging it)

Does this change the previously entered user name? or does it create a new row in the db?


回答1:


An Entity is roughly the same thing as an instance of a class when you are thinking from a code perspective or a row in a table (basically) when you are thinking from a database perspective.

So, it's essentially a persisted / persistable instance of a class. Changing values on it works just like changing values on any other class instance. The difference is that you can persist those changes and, in general, the current state of the class instance (entity) will overwrite the values the row for that instance (entity) had in the database, based on the primary key in the database matching the "id" or similar field in the class instance (entity).

There are exceptions to this behavior, of course, but this is true in general.




回答2:


It's a model. It's a domain object that can be persisted. Don't over think it. Akin to a Rails model. And remember, models (in this paradigm) are mutable!



来源:https://stackoverflow.com/questions/9778659/what-are-jpa-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!