Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

"Online Tweeter Enterprise Application" : Creating an Entity Class in NetBeans EJB module - Part 3


Lets continue building "Online Tweeter Enterprise Application" in NetBeans. In this section of tutorial, you will create an Entity Class by name "Tweet.java". An Entity class is a simple POJO with annotations. It uses annotations like @Entity, @Id ... etc, which makes it persist in database as tables.

Step 1: Open "tweeter-ejb" project and right click Source Packages and then select New and than Other as shown in fig below:





Step 2: On clicking Other a dialog box appears by name New File. In the Categories: list select Persistence and in the File Types: select Entity Class as shown in fig below.




Step 3: Click

.
New Entity Class dialog box gets open. It prompts us to enter values for Class Name: , Project: , Location: , Package: , Created File: , Primary Key Type:. Enter the values as per fig below or enter what suits you.





and than just click
.

In the package provided at the time of creating Entity Class in the tweeter-ejb module Tweet.java gets created. It has most of the source code already generated by NetBeans. Kindly add or remove additional code provided below.

package com.hubberspot.ejb;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

// @Entity annotation is used to make a 
// Java class an Entity

@Entity
public class Tweet implements Serializable {

    private static final long serialVersionUID = 1L;

    // @Id annotation is used over the field which is
    // to be marked as primary key. Each entity class
    // needs to have this primary key.
    
    // @GeneratedValue annotation provides information
    // to the persistence provider to generate a way to
    // uniquely identify each instance of this entity class.
    // In order to do that it uses strategies for creating
    // new and unique id based on the strategy type.
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    private String username;
    private String tweet;

    public String getTweet() {
        return tweet;
    }

    public void setTweet(String tweet) {
        this.tweet = tweet;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }    

    public Long getId() {
        return id;
    }

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

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

    @Override
    public boolean equals(Object object) {
        
        if (!(object instanceof Tweet)) {
            return false;
        }
        Tweet other = (Tweet) 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.hubberspot.ejb.Tweet[ id=" + id + " ]";
    }
    
}



Tweet.java has few properties such as tweet and username, which will be persisted in the database through getters and setters.

In the next section of this blog (part 4) you will learn how to create a Message Driven Bean in NetBeans for this application.

 
© 2021 Learn Java by Examples Template by Hubberspot