Creating An Entity Object (POJO)

  1. Select File > New > Other... from the Eclipse menu.

  2. Browse to Java > Class and click the Next button.

    New Java Class

  3. In the New Java Class dialog, enter the following:

    The Package field: by.iba.domain

    The Name field: Operation

    The Interfaces list: java.io.Serializable (click the Add... button to add the interface).

    NOTE: make sure Source folder is Calculator/src.

    Click the Finish button.

    Entity Object - Finish

  4. Open the new Operation.java file and add the @Entity annotation to the class:

    @Entity
    public class Operation implements Serializable {
    	...								
    								

    The class is annotated with @Entity to denote that it is an Entity Object.

  5. Add the following instance variables (future persistent fields) to the class:

    private int id;
    
    private Timestamp timestamp;
    
    private double operandA;
    
    private double operandB;
    
    private String operation;
    
    private double result;
    								

  6. Click the Source > Generate Getters and Setters... to generate getters and setters.

    Generate Getters and Setters

  7. Add the following annotations to the getter methods:

    The getId() method is annotated with @Id to denote that id is the primary key of the entity (by default, the mapped column for the primary key of the entity is assumed to be the primary key of the primary table), the method is also annotated with @GeneratedValue to denote that the persistence provider must assign primary keys for the entity.

    @Id
    @GeneratedValue
    @Column(name="OPERATION_ID")	
    public int getId() {
    	return id;
    }
    								
    @Column(name="OPERAND_A")	
    public double getOperandA() {
    	return operandA;
    }
    								
    @Column(name="OPERAND_B")
    public double getOperandB() {
    	return operandB;
    }
    								
    @Column(length=8)
    public String getOperation() {
    	return operation;
    }
    								

    Override the toString() method:

    public String toString() {
    	return " \n" + getTimestamp() + " : " + 
    		getOperandA() + " " + getOperation() + " " + getOperandB() +  " is " + getResult();
    }
    								

    Click the Ctrl + Shift + O to organize imports and Ctrl + S to save.

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine