Red Hat Training

A Red Hat training course is available for Red Hat JBoss Developer Studio

2.6. Add an Entity Using Hibernate Tools

A new entity class can be added to the TicketMonster project using Hibernate Tools. In the procedure below a Venue entity class is created to hold information about the venues where events will be held. This Venue entity has id, name, description and capacity fields.

Procedure 2.6. Add an Entity Using Hibernate Tools

  1. In the Project Explorer view, expand ticket-monsterJava Resourcessrc/main/java.
  2. Right-click org.jboss.jdf.example.ticketmonster.model and click NewClass.
  3. In the Name field, type Venue and click Finish. This creates a Venue.java file that is automatically opened in a Java editor.
    In the Name field, type Venue and click Finish.

    Figure 2.9. New Venue Class using Java Class Wizard

  4. Add fields to the entity by adding the following lines in between the braces of the Venue class
    private Long id;
    private String name;
    private String description;
    private int capacity;
  5. Save the Venue.java file by pressing Ctrl+S (Cmd+S).
  6. Add get and set methods by right-clicking on a new line after private int capacity; and clicking SourceGenerate Getters and Setters.
  7. Click Select All and click OK.
    Click Select All and click OK.

    Figure 2.10. List of Attributes Selected for Creating Get and Set Methods

  8. Save the Venue.java file.
  9. Make the class an entity by right-clicking anywhere in the Venue class and clicking SourceGenerate Hibernate/JPA annotations.
  10. Ensure org.jboss.jdf.example.ticketmonster.model.Venue is selected and click Next.
    Ensure org.jboss.jdf.example.ticketmonster.model.Venue is selected and click Next.

    Figure 2.11. Venue Class Selected for Adding JPA Annotations

  11. Click Finish and save the Venue.java file.