简体   繁体   English

什么是JPA实体?

[英]What are JPA entities?

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. 我开始使用JPA,我总是对实体的术语及其用法感到困惑,我已经阅读了很多,但我仍然不太明白。 I read the Oracle documentation of it but it does not really explain its role in the transaction. 我阅读了它的Oracle文档,但它并没有真正解释它在事务中的作用。

What are JPA enities? 什么是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 (ie merging it) 然后保持它,然后更改用户名,并再次persisitig(即合并它)

Does this change the previously entered user name? 这是否会更改先前输入的用户名? or does it create a new row in the db? 还是在db中创建一个新行?

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). 不同之处在于您可以持久保存这些更改,通常,类实例(实体)的当前状态将根据数据库匹配中的主键覆盖该实例(实体)的行在数据库中的值。类实例(实体)中的“id”或类似字段。

There are exceptions to this behavior, of course, but this is true in general. 当然,这种行为有例外,但总的来说这是正确的。

It's a model. 这是一个模型。 It's a domain object that can be persisted. 它是一个可以持久化的域对象。 Don't over think it. 不要过度思考。 Akin to a Rails model. 类似于Rails模型。 And remember, models (in this paradigm) are mutable! 请记住,模型(在这个范例中)是可变的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM