💾Thananjhayan
← Back to notes

Spring Boot — The Item Entity Class

July 21, 2025

Part (iii). The entity represents one row of the item table — each field is one column. Spring Data JPA reads @Entity and links the class to the table.

Create Item.java in a package like com.example.IndexNo.model:

package com.example.IndexNo.model;

import jakarta.persistence.*;
import java.util.Date;

@Entity
@Table(name = "item")
public class Item {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int itemCode;

    private String itemName;
    private double unitPrice;

    @Temporal(TemporalType.DATE)
    private Date updatedDate;

    // Default (empty) constructor - required by JPA
    public Item() {
    }

    // Parameterized constructor
    public Item(String itemName, double unitPrice, Date updatedDate) {
        this.itemName = itemName;
        this.unitPrice = unitPrice;
        this.updatedDate = updatedDate;
    }

    // Getters and Setters
    public int getItemCode() { return itemCode; }
    public void setItemCode(int itemCode) { this.itemCode = itemCode; }

    public String getItemName() { return itemName; }
    public void setItemName(String itemName) { this.itemName = itemName; }

    public double getUnitPrice() { return unitPrice; }
    public void setUnitPrice(double unitPrice) { this.unitPrice = unitPrice; }

    public Date getUpdatedDate() { return updatedDate; }
    public void setUpdatedDate(Date updatedDate) { this.updatedDate = updatedDate; }
}

What the annotations mean

  • @Entity — "this class is a database table".
  • @Table(name = "item") — the table is called item.
  • @IditemCode is the primary key (unique id of each row).
  • @GeneratedValue(IDENTITY) — the database auto-numbers the id for new rows.
  • @Temporal(DATE) — store updatedDate as a plain date.

If your restored table uses different column names (like item_code), add @Column(name = "item_code") on the line above that field so Java maps to the correct column.

Next: [[spring-boot-repository-controller]]. Prereq: [[spring-boot-mysql-setup]].

💬 Comments