什么是JPA实体? [英] What are JPA entities?

查看:227
本文介绍了什么是JPA实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用JPA,我总是与实体的术语和他们的使用混淆,我已经阅读了很多,但我还是不太明白。
我读了它的Oracle文档,但它没有真正解释它在事务中的作用。



什么是JPA enities?它们实际上是否保存每行的数据,我的意思是,它们是存储行数据的实例吗?或者他们只是映射db的表,然后插入和删除它们?



例如,如果我使用这个:

  entity.setUserName(michel); 

然后持久化,然后更改用户名,并再次进行persisitig p>

这是否会更改先前输入的用户名?或者是否在db中创建一个新行?

解决方案

实体与类的实例大致相同从数据库的角度来看,你从代码的角度或者表中的一行(基本上)。



因此,它本质上是一个持久化/持久化的实例一类。更改值上的值就像更改任何其他类实例上的值。不同之处在于,您可以保留这些更改,一般来说,类实例(实体)的当前状态将覆盖数据库中该实例(实体)所在行的值,基于数据库中的主键匹配类实例(实体)中的id或类似字段。



当然,这种行为有例外,但一般情况下是这样。


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?

解决方案

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.

这篇关于什么是JPA实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆