Model In Spring Framework For Beginners


Model in programming language is a representative of an object, so if you have a car object, you can define a car model, for example, car brand, car speed, car price, etc. In pseudo code you can create a car model, something like.

Class Car{
String brand;
Integer speed;
Float price;
}

That's called a model. Now in spring framework, you can add some annotation to your model, so that it can be integratted to you database, those model usually will be call as an entity, so the first thing is to set annotation to car class an @Entity annotation.

@Entity
Class car{
String brand;
Integer speed;
Float price;
}

But, that class should have at least an id it is recommended to have an id to whatever model you want create, so you must have an id field.

@Entitypublic class Car{
    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String brand;        private Integer speed;        private Float price;}

Now, your entity is ready, but it useless since it doesn't have any public field to assign any value to our model, so we need to create a getter and setter to our model.

@Entity
public class Car{
    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String brand;        private Integer speed;        private Float price;
    public Long getId() {
        return id;    }

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

    public String getBrand() {
        return brand;    }

    public void setBrand(String brand) {
        this.brand = brand;    }

    public Integer getSpeed() {
        return speed;    }

    public void setSpeed(Integer speed) {
        this.speed = speed;    }

    public Float getPrice() {
        return price;    }

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

That way now you have a car model that can be use for your application. Car is real world object, fortunately it is not used by the database system for example MySQL as their reserved keyword, or any of the fields is a reserved MySQL keywords, so that will be valid model. But what happen if we create an entity that will collide with MySQL keyword, we can avoid that by defining for example our database name, or column name so that it will be valid and not collide with any database reserved keywords. Or maybe you just want your tables or fields name much more cool, or the technical reason is sometimes you have to change the class name but not the database schema.

@Entity@Table(name = "cars")
public class Car{
    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "car_id")
    private Long id;
    @Column(name = "car_brand")
    private String brand;        @Column(name = "car_speed")
    private Integer speed;        @Column(name = "car_price")
    private Float price;
    public Long getId() {
        return id;    }

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

    public String getBrand() {
        return brand;    }

    public void setBrand(String brand) {
        this.brand = brand;    }

    public Integer getSpeed() {
        return speed;    }

    public void setSpeed(Integer speed) {
        this.speed = speed;    }

    public Float getPrice() {
        return price;    }

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

With that entity, if you look at the database schema our table would have name cars, and fields car_id, car_brand, car_speed and car_price.



Comments

Popular posts from this blog

Flixify Video Streaming Platform

ASUS Battery Health Charging Software To Make Your Battery Life Longer

Push Notification Cordova, FCM And PHP