Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 501
engma Avatar asked Dec 12 '22 04:12

engma


1 Answers

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.

like image 89
cdeszaq Avatar answered Jan 01 '23 10:01

cdeszaq