其他分享
首页 > 其他分享> > JPA创建实体

JPA创建实体

作者:互联网

Java类可以很容易地转换成实体。 对于实体转换,基本要求是 -

在这里,我们将学习如何通过示例,学习将常规Java类转换为实体类 -

简单的一个学生类(Student),代码如下 -


public class Student {

    private int id;
    private String name;
    private long fees;

    public Student() {
    }

    public Student(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getFees() {
        return fees;
    }

    public void setFees(long fees) {
        this.fees = fees;
    }
}
Java

上面的类是一个常规的java类,有三个属性: id, namefees 。要将此类转换为实体,请在此类中添加@Entity@Id注解。

简单的实体类


import javax.persistence.*;

@Entity
public class Student {

    @Id
    private int id;
    private String name;
    private long fees;

    public Student() {
    }

    public Student(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getFees() {
        return fees;
    }

    public void setFees(long fees) {
        this.fees = fees;
    }
}

标签:name,JPA,创建,实体,id,int,Student,fees,public
来源: https://www.cnblogs.com/borter/p/12423933.html