This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Learn Java with Math: Using Fun Projects and Games and have Ron Dai on-line!
See this thread for details.
Win a copy of Learn Java with Math: Using Fun Projects and Games this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

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

 
Ranch Hand
Posts: 301
6
  • 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



     
    Master Rancher
    Posts: 4369
    47
    • 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: 301
    6
    • 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
    Master Rancher
    Posts: 4369
    47
    • 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.
     
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • 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
     
    girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!