报错原因:
1,类字段与表字段不一样,或者类中的字段在表中没有,可以在类上加注解@Transient注
2,@Transient注解和@column不在同样的位置:
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Transient; @Entity public class StudentInfo { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private int age; // 假设年龄字段类型为int // 体重字段不与数据库映射 @Transient //此处应该跟@column一样放下面get方法,保持一致 private double weight; // 假设体重字段类型也为double public Long getId() { return id; } public void setId(Long id) { this.id = id; } // 年龄字段的getter和setter方法 // 年龄字段与数据库映射 @Column(name = "age") public int getAge() { return age; } public void setAge(int age) { this.age = age; } // 身高字段的getter和setter方法 public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } // 体重字段的getter和setter方法 public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } }
如代码中,如果@column注解在get方法上,而@Transient注解在字段属性上就会报错,应该将@Transient放到对应get方法上