Confused about inserting a single entity as explained in Murach's Java Servlets/JSP 3rd edition

Lisa Austin

Ranch Hand

Posts: 158

3

posted 1 year ago

Hi,
In the book Murach's Java Servlets/JSP 3rd edition pg. 444 - 445 it explains how to insert , update , delete a single entity. I need some clarification please. My instructor has us using examples from the book. The example below is what is on pg 445 except two additional lines which I added based on my instructor's feedback .

I don't understand how the value "user" in the code knows what column to insert "user" into. I am use to SQL insert into statements so I'm having trouble with this.

The examples are this.

I understand that the line is to insert a single entity but I don't think I don't understand what exactly is being inserted and what table . I have the below persistence.xml .

Am I correctly understanding :

that the database schema " JPA_Test " will be created.

The table "User" will be created and the columns are based on the values I have in my JPA Entity ( code pasted below the persistence.xml code ).

If I am correct then is the value being inserted by just " root" ? If it's "root" then how does this specify WHAT column "root" should be inserted into?

Thank You for your help.

JPA entity

Dave Tolls

Ranch Hand

Posts: 2846

30

posted 1 year ago

The value passed into the persist() method must be an Entity.
It's the Entity (via its mappings) that is persisted.

In the case of your User entity, it will use the default values, so the table will be "User", and all the columns will match the other data and will be mapped to a relevant column datatype.

Lisa Austin

Ranch Hand

Posts: 158

3

posted 1 year ago

Dave Tolls wrote:The value passed into the persist() method must be an Entity.
It's the Entity (via its mappings) that is persisted.

In the case of your User entity, it will use the default values, so the table will be "User", and all the columns will match the other data and will be mapped to a relevant column datatype.

Hey Dave,
I'm sorry to be a pain.
I thought entity is a single value? As in if I have a table which has the columns User, Name, UserID. Entity would be one single value in the column "User"? Is that wrong?

Dave Tolls

Ranch Hand

Posts: 2846

30

posted 1 year ago

3

At it's most basic, and Entity is the Java class that represents a table in the database.
The @Entity annotation tells JPA that the class that's been annotated is represented by a table.

It's not a value in a column, it's the whole row (give or take).

So, to take your User class above, JPA will expect there to be a table in the database called User, looking something like (depending on database):
userId IDENTITY
firstName VARCHAR2
lastName VARCHAR2
email VARCHAR2

Hibernate can even generate that for you, though I've never used the auto generation thing.

As i seems you are an ORM (JPA) greenhorn, it might be useful to have a few other (free) resources besides the Murach book to use as complementary resources when you need some additional clarification.

I really like the Java Persistence API WikiBook. When I'm having an issue with JPA, JPQL, or something related it's the first resource I'll check to find a solution. It's really an excellent resource (in my humble opinion)! It's not a step-by-step tutorial, but you'll find everything required in this wiki book: from basic entity mappings to complex JPQL queries.