我需要填充Employee表,其中Gender字段的值可能为1 - >男性,2 - >女
表的Hibernate Mapping:
package test.hib.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import test.hib.enumtest.GenderConverter;
@Entity
@Table(name="Employee")
public class Employee implements Serializable {
/**
*
*/
private static final long serialVersionUID = -2531499686831259894L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int employeeId;
@Column
private String employeeName;
@Column
@Convert(converter=GenderConverter.class)
private Gender gender;
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
}
性别枚举:
package test.hib.entity;
public enum Gender {
MALE(1),
FEMALE(2);
private int value;
private Gender(int value){
this.value = value;
}
public int getValue() {
return value;
}
public static Gender getGender(Integer id) {
if (id == null)
return null;
for (Gender g : values()) {
if (g.getValue() == id)
return g;
}
return null;
}
}
转换器:
package test.hib.enumtest;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import test.hib.entity.Gender;
@Converter
public class GenderConverter implements AttributeConverter<Gender, Integer>{
@Override
public Integer convertToDatabaseColumn(Gender arg0) {
if(arg0 != null) return arg0.getValue();
return null;
}
@Override
public Gender convertToEntityAttribute(Integer arg0) {
// TODO Auto-generated method stub
return Gender.getGender(arg0);
}
}
我使用以下代码修改实体
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springConfig.xml");
HibEnumRepo repository = (HibEnumRepo)applicationContext.getBean(HibEnumRepo.class);
Employee employee = new Employee();
employee.setEmployeeName("Renjith");
employee.setGender(Gender.MALE);
repository.insertEmployee(employee);
Employee employee2 = new Employee();
employee2.setEmployeeName("Renju");
employee2.setGender(Gender.FEMALE);
repository.insertEmployee(employee2);
在存储库bean中。
package test.hib.enumtest;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import test.hib.entity.Employee;
@Repository
public class HibEnumRepo {
@Autowired
private SessionFactory sessionFactory;
@Transactional
public void insertEmployee(Employee employee){
Session session = sessionFactory.getCurrentSession();
session.save(employee);
}
}
但是在将值插入表时,Enum正在设置Male&amp;的默认值。女性即。 0&amp;分别为1。
mysql> select * from Employee;
+------------+--------------+--------+
| employeeId | employeeName | gender |
+------------+--------------+--------+
| 1 | Renjith | 0 |
| 2 | Renju | 1 |
+------------+--------------+--------+
2 rows in set (0.00 sec)
如何设置值1&amp; 2而不是?
答案 0 :(得分:1)
最好将enum
作为String
存储在数据库中,并使用enum
从应用程序中检索其ID。
您持续{0}从{0}开始的EnumType.ORDINAL
,这就是因为0和1在DB中持久存在的原因
如果您想要保留枚举的ID,可以使用enum
注释
在JPA2.1(Hibernate 4.3 +)中添加了转换
实体
Coverter
转换器
@Entity
public class Employee {
@Id
@GeneratedValue
Long id;
@Column
String name;
@Column
int age;
@Column
@Convert(converter = GenderConverter.class)
Gender gender;
}
枚举
public class GenderConverter implements AttributeConverter<Gender, Integer> {
@Override
public Integer convertToDatabaseColumn(Gender gender) {
if (gender == null)
return null;
return gender.getId();
}
@Override
public Gender convertToEntityAttribute(Integer id) {
return Gender.getGender(id);
}
}
DDL由hibernate创建
public enum Gender {
MALE(1), FEMALE(2);
int id;
private Gender(int id) {
this.id = id;
}
public int getId() {
return id;
}
public static Gender getGender(Integer id) {
if (id == null)
return null;
for (Gender g : values()) {
if (g.getId() == id)
return g;
}
return null;
}
}