In the last post I created an EJB Module project for the tutorial/project Car Rental Enterprise Application. In this post, I will add an Entity Bean Class to the Enterprise Java Bean project created earlier.

An “Entity Bean” is a type of Enterprise JavaBean, a server-side Java EE component, that represents persistent data maintained in a database. An entity bean can manage its own persistence (Bean managed persistence) or can delegate this function to its EJB Container (Container managed persistence).

To Create an Entity:

  • Open the EJBModule Project (CarRental-ejb) and create a new Java package, I named mine com.rental.entity.
  • Right-Click on the project name from Netbeans projects tab, go to New and click on Create Entity from Database.
  • On the next window, select datasource from the combo box if it is there. I created a new datasource from the rentalDB postgres database. Incase it is not amongst the options, refresh or restart the server or Netbeans IDE. Incase you missed the post where I created the datasource refer to it here.
  • In the Available tables area, all the tables associated with the datasource will appear. In my case, I should be expecting to see the name info. Highlight the table and click on add, the table will be moved to the selected tables area. Click on next.

Entity bean class - geekshelm

  • In the next window, An area with the options: Database Table: info, Class name: Info (I left mine like that but you can choose to change yours.), Generation Type: new. Make sure Generate Persistence Unit and generate named Query boxes are ticked, others can be uncheck if you don’t need their functionality. Click on next.

Entity bean Class - geekshelm

  • In the next window, leave association fetch field as default, Collection type: list (other options which includes Collection and Set can also be chosen). Other boxes can be left un-ticked. Click on Finish.

Entity bean Class - geekshelm

EJB 3.0, entity bean used in EJB 2.0 is largely replaced by persistence mechanism. Now entity beanis a simple plain old java objects (POJO) having mapping with table.

The Entity bean class will be created with alot of lines of codes and I like leaving my codes simple and straight. Below is my own Entity Bean Class Source Code:

package com.rental.entity;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author STEINACOZ-PC
 */
@Entity
@Table(name = "info")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Info.findAll", query = "SELECT i FROM Info i"),
    @NamedQuery(name = "Info.findById", query = "SELECT i FROM Info i WHERE i.id = :id"),
    @NamedQuery(name = "Info.findByCarname", query = "SELECT i FROM Info i WHERE i.carname = :carname"),
    @NamedQuery(name = "Info.findByCarmodel", query = "SELECT i FROM Info i WHERE i.carmodel = :carmodel"),
    @NamedQuery(name = "Info.findByManufacturer", query = "SELECT i FROM Info i WHERE i.manufacturer = :manufacturer"),
    @NamedQuery(name = "Info.findByYear", query = "SELECT i FROM Info i WHERE i.year = :year"),
    @NamedQuery(name = "Info.findByCategory", query = "SELECT i FROM Info i WHERE i.category = :category"),
    @NamedQuery(name = "Info.findByImage", query = "SELECT i FROM Info i WHERE i.image = :image"),
    @NamedQuery(name = "Info.findByPrice", query = "SELECT i FROM Info i WHERE i.price = :price"),
    @NamedQuery(name = "Info.findByAddeddate", query = "SELECT i FROM Info i WHERE i.addeddate = :addeddate"),
    @NamedQuery(name = "Info.findByHiredate", query = "SELECT i FROM Info i WHERE i.hiredate = :hiredate"),
    @NamedQuery(name = "Info.findByExpecteddate", query = "SELECT i FROM Info i WHERE i.expecteddate = :expecteddate"),
    @NamedQuery(name = "Info.findByAvailable", query = "SELECT i FROM Info i WHERE i.available = :available"),
    @NamedQuery(name = "Info.findByRef", query = "SELECT i FROM Info i WHERE i.ref = :ref")})
public class Info implements Serializable {

    private static final long serialVersionUID = 1L;
    
  
  
    private Short id;
    
    private String carname;
    
    private String carmodel;
  
    private String manufacturer;
  
    private String year;
    
    private String category;
    
    private String image;
    
    private Float price;
   
    private Date addeddate;
    
    private Date hiredate;
    
    private Date expecteddate;
   
    private String available;
    
    private String ref;

   

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name="id")
    public Short getId() {
        return id;
    }

    public void setId(Short id) {
        this.id = id;
    }

    public String getCarname() {
        return carname;
    }

    public void setCarname(String carname) {
        this.carname = carname;
    }

    public String getCarmodel() {
        return carmodel;
    }

    public void setCarmodel(String carmodel) {
        this.carmodel = carmodel;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public Date getAddeddate() {
        return addeddate;
    }

    public void setAddeddate(Date addeddate) {
        this.addeddate = addeddate;
    }

    public Date getHiredate() {
        return hiredate;
    }

    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }

    public Date getExpecteddate() {
        return expecteddate;
    }

    public void setExpecteddate(Date expecteddate) {
        this.expecteddate = expecteddate;
    }

    public String getAvailable() {
        return available;
    }

    public void setAvailable(String available) {
        this.available = available;
    }

    public String getRef() {
        return ref;
    }

    public void setRef(String ref) {
        this.ref = ref;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Info)) {
            return false;
        }
        Info other = (Info) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.rental.entity.Info[ id=" + id + " ]";
    }
    
}

The full source code really contains lots of unimportant lines, you can trim it down more. The Class name has an annotation above it signifying that it is an Entity Bean. The Entity Bean class almost looks like a MVC Model class or it is a Model class that models records in the database.

The declared variable names are the same with the corresponding table column names in the database:

private Short id;
    
    private String carname;
    
    private String carmodel;
  
    private String manufacturer;
  
    private String year;
    
    private String category;
    
    private String image;
    
    private Float price;
    
    private Date addeddate;
    
    private Date hiredate;
    
    private Date expecteddate;
   
    private String available;
    
    private String ref;

postgreSQL database - geekshelm

A named query is a statically defined query with a predefined unchangeable query string. Using named queries instead of dynamic queries may improve code organization by separating the Java persistence Query language (JPQL) query strings from the Java code. It also enforces the use of query parameters rather than embedding literals dynamically into the query string and results in more efficient queries.

Because I checked the generate named Query checkbox when creating the Entity bean class, Netbeans will generated named queries to access all the columns in the table Info. You can add your own named query with this convention.

@NamedQueries({
    @NamedQuery(name = "Info.findAll", query = "SELECT i FROM Info i"),
    @NamedQuery(name = "Info.findById", query = "SELECT i FROM Info i WHERE i.id = :id"),
    @NamedQuery(name = "Info.findByCarname", query = "SELECT i FROM Info i WHERE i.carname = :carname"),
    @NamedQuery(name = "Info.findByCarmodel", query = "SELECT i FROM Info i WHERE i.carmodel = :carmodel"),
    @NamedQuery(name = "Info.findByManufacturer", query = "SELECT i FROM Info i WHERE i.manufacturer = :manufacturer"),
    @NamedQuery(name = "Info.findByYear", query = "SELECT i FROM Info i WHERE i.year = :year"),
    @NamedQuery(name = "Info.findByCategory", query = "SELECT i FROM Info i WHERE i.category = :category"),
    @NamedQuery(name = "Info.findByImage", query = "SELECT i FROM Info i WHERE i.image = :image"),
    @NamedQuery(name = "Info.findByPrice", query = "SELECT i FROM Info i WHERE i.price = :price"),
    @NamedQuery(name = "Info.findByAddeddate", query = "SELECT i FROM Info i WHERE i.addeddate = :addeddate"),
    @NamedQuery(name = "Info.findByHiredate", query = "SELECT i FROM Info i WHERE i.hiredate = :hiredate"),
    @NamedQuery(name = "Info.findByExpecteddate", query = "SELECT i FROM Info i WHERE i.expecteddate = :expecteddate"),
    @NamedQuery(name = "Info.findByAvailable", query = "SELECT i FROM Info i WHERE i.available = :available"),
    @NamedQuery(name = "Info.findByRef", query = "SELECT i FROM Info i WHERE i.ref = :ref")})

That is it for Creating Entity Bean Class, if any changes should be made to the class. it will be rapidly be updated here. I will add the Interface and Session Bean class next.

LEAVE A REPLY

Please enter your comment!
Please enter your name here