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类,有三个属性: id
, name
和 fees
。要将此类转换为实体,请在此类中添加@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; } }