jpa @Enumerated(EnumType.ORDINAL)处理枚举类型
程序员文章站
2022-04-23 15:45:02
...
PhoneType代码:
public enum PhoneType {
LAND_LINE,
MOBILE;
}
Phone代码:
@Data
@Entity(name = "Phone")
public class Phone {
@Id
private Long id;
@Column(name = "phone_number")
private String number;
@Enumerated(EnumType.ORDINAL)
@Column(name = "phone_type")
private PhoneType type;
}
测试代码:
Phone phone = entityManager.find(Phone.class, 1l);
System.out.println(phone.getType());
entityManager.flush();
发现存入的是枚举值的1。 如果find的时候,返回的1对应的PhoneType.
上一篇: Spring Boot整合Mybatis