• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Lisa Austin
Ranch Hand
Posts: 118
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 2091
    15
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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: 118
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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: 2091
    15
    • Likes 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Lisa Austin wrote: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.

    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.

    If you are looking for a step-by-step tutorial, I would definitely have a look at the Hibernate Getting Started Guide.

    Hope it helps!
    Kind regards,
    Roel
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic