java - JPA Mapping Enum from DB -
i have db table this:
+----+-------------+ | id | cola | +----+-------------+ | 1 | black cola | | 2 | red cola | | 3 | blue cola | | 4 | purple cola | +----+-------------+
and entity + enum this:
@entity @table(schema = "dbo", name = "cola") public class colaentity implements serializable { @id @generatedvalue(strategy = generationtype.identity) @column(name = "id") private integer id; @enumerated(enumtype.string) @column(name = "cola") private cola cola; public integer getid() { return id; } public void setid(integer id) { this.id = id; } public cola getcola() { return cola; } public void setcola(cola cola) { this.cola = cola; } } public enum cola { black_cola("black cola"), red_cola("red cola"), blue_cola("blue cola"), purple_cola("purple cola") private final string cola; causecode(string cola) { this.cola = cola; } @override public string tostring() { return this.cola; } }
but every time run code error:
caused by: java.lang.illegalargumentexception: unknown name value [black cola] enum class [com.cola.cola] @ org.hibernate.type.enumtype$namedenumvaluemapper.fromname(enumtype.java:433) @ org.hibernate.type.enumtype$namedenumvaluemapper.getvalue(enumtype.java:417) @ org.hibernate.type.enumtype.nullsafeget(enumtype.java:231) @ org.hibernate.type.customtype.nullsafeget(customtype.java:111) @ org.hibernate.type.abstracttype.hydrate(abstracttype.java:85) @ org.hibernate.persister.entity.abstractentitypersister.hydrate(abstractentitypersister.java:2790) @ org.hibernate.loader.loader.loadfromresultset(loader.java:1729) @ org.hibernate.loader.loader.instancenotyetloaded(loader.java:1655) @ org.hibernate.loader.loader.getrow(loader.java:1544) @ org.hibernate.loader.loader.getrowfromresultset(loader.java:727) @ org.hibernate.loader.loader.processresultset(loader.java:972) @ org.hibernate.loader.loader.doquery(loader.java:930) @ org.hibernate.loader.loader.doqueryandinitializenonlazycollections(loader.java:336) @ org.hibernate.loader.loader.dolist(loader.java:2617) @ org.hibernate.loader.loader.dolist(loader.java:2600) @ org.hibernate.loader.loader.listignorequerycache(loader.java:2429) @ org.hibernate.loader.loader.list(loader.java:2424) @ org.hibernate.loader.hql.queryloader.list(queryloader.java:501) @ org.hibernate.hql.internal.ast.querytranslatorimpl.list(querytranslatorimpl.java:371) @ org.hibernate.engine.query.spi.hqlqueryplan.performlist(hqlqueryplan.java:216) @ org.hibernate.internal.sessionimpl.list(sessionimpl.java:1326) @ org.hibernate.internal.queryimpl.list(queryimpl.java:87) @ org.hibernate.jpa.internal.queryimpl.list(queryimpl.java:606) @ org.hibernate.jpa.internal.queryimpl.getresultlist(queryimpl.java:483) @ org.hibernate.jpa.criteria.compile.criteriaquerytypequeryadapter.getresultlist(criteriaquerytypequeryadapter.java:50) @ org.springframework.data.jpa.repository.support.simplejparepository.findall(simplejparepository.java:329) @ com.jpacolarepository.getall(jpacolarepository.java:27) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:498) @ org.springframework.aop.support.aoputils.invokejoinpointusingreflection(aoputils.java:333) @ org.springframework.aop.framework.reflectivemethodinvocation.invokejoinpoint(reflectivemethodinvocation.java:190) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:157) @ org.springframework.dao.support.persistenceexceptiontranslationinterceptor.invoke(persistenceexceptiontranslationinterceptor.java:136) ... 88 more
i know jpa trying match db value black cola
literal enum value can't since it's named black_cola
underscore. how tell jpa match string representation? or better way of doing this?
Comments
Post a Comment