我正在使用Hibernate 4.3.0.Final(JPA 2.1)和MySQL 5.5。我想将我的Java枚举映射到具有相应值的MySQL表。所以我的枚举是
public enum Roles
{
STUDENT("Student"),
TEACHER("Teacher"),
…
Roles(final String name)
{
this.name = name;
}
private final String name;
@Override
public String toString()
{
return getName();
}
public String getName()
{
return this.name;
}
}
我的表包含
之类的值+------------------------+------------------------+-------------+
| ID | NAME | ADMIN_LEVEL |
+------------------------+------------------------+-------------+
| Student | Student | 1 |
| Teacher | Teacher | 10 |
然后,在代表上表的实体类中,我有
@GenericGenerator(name = "uuid-strategy", strategy = "org.mainco.subco.core.util.subcoUUIDGenerator")
@Entity(name = "sb_role")
public class Role implements Serializable, Comparable<Role>
{
@Id
@NotNull
@GeneratedValue(generator = "uuid-strategy")
@Column(name = "ID")
/* the database id of the role */
private String id;
@Enumerated(value=EnumType.STRING)
@Column(name = "NAME")
/* the String name of the role */
private Roles name;
然而,当我运行我的应用程序时,我收到如下错误。如何在枚举值和数据库中的列之间强制执行完整性?
java.lang.IllegalArgumentException: Parameter value [Student] did not match expected type [org.mainco.subco.user.domain.Roles (n/a)]
at org.hibernate.jpa.spi.BaseQueryImpl.validateBinding(BaseQueryImpl.java:885)
at org.hibernate.jpa.internal.QueryImpl.access$000(QueryImpl.java:80)
at org.hibernate.jpa.internal.QueryImpl$ParameterRegistrationImpl.bindValue(QueryImpl.java:248)
at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:631)
at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:179)
at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:49)
at org.hibernate.jpa.criteria.compile.CriteriaCompiler$1$1.bind(CriteriaCompiler.java:126)
at org.hibernate.jpa.criteria.CriteriaQueryImpl$1.buildCompiledQuery(CriteriaQueryImpl.java:386)
at org.hibernate.jpa.criteria.compile.CriteriaCompiler.compile(CriteriaCompiler.java:147)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:736)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:241)
at com.sun.proxy.$Proxy71.createQuery(Unknown Source)
at org.mainco.subco.core.repo.AbstractHibernateDAO.findByUniqueConstraint(AbstractHibernateDAO.java:104)
at org.mainco.subco.user.repo.RoleDaoImpl.findRoleByName(RoleDaoImpl.java:26)
at org.mainco.subco.user.service.RoleServiceImpl.findByName(RoleServiceImpl.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at com.sun.proxy.$Proxy91.findByName(Unknown Source)
at org.mainco.subco.resource.service.ResourceServiceIT.testFindById(ResourceServiceIT.java:95)
我对写一个@Converter不感兴趣(有很多值要考虑),除非这是绝对最简单的解决方案。
答案 0 :(得分:0)
@Enumerated(值= EnumType.STRING)
使用ENUM的值 学生 教师
不是括号中的那个,在数据库中,您有以小写字母开头的值 在数据库中进行更新以使其大写该列中的所有值,这是最简单的解决方案,它应该工作,并且不会影响数据完整性。 XD。