|
I know this has been asked before, but I wasn't able to implement a solution based on the information I found so far. so perhaps someone can explain it to me.
I have a table "status". It has two columns:id and name. id is a PK. Instead of using a POJO Status, I would like to use an enum. I created such an enum as follows: public enum Status { NEW(1), READY(2), CLOSED(3); private int id; public void setId(int id) { this.id = id; } public int getId() { return this.id; } Status(int id) { this.id = id; } } here is my mapper <mapper namespace="com.mine.StatusMapper"> <select id="getStatusByName" resultType="Status" parameterType="String"> SELECT ls.id, ls.name FROM status AS ls WHERE ls.name = #{name} </select> </mapper> but for some reason, when I try to retrieve an enum, something breaks, but no exception is thrown. |
|
You should add a typemapper for the enum
On Wed, Apr 18, 2012 at 3:38 PM, Ramy Vilensky <[hidden email]> wrote: I know this has been asked before, but I wasn't able to implement a solution based on the information I found so far. so perhaps someone can explain it to me. |
|
sry i meant typehandler :P
org.apache.ibatis.type.TypeHandler On Wed, Apr 18, 2012 at 4:05 PM, Tim <[hidden email]> wrote: You should add a typemapper for the enum |
|
Based on the documentation, mybatis already has two enum type handlers. Regular that goes by enum value (String), and ordinal.
What I can't figure out is how I modify my mapper to include it.
On Wed, Apr 18, 2012 at 2:08 PM, Tim <[hidden email]> wrote: sry i meant typehandler :P |
|
Sorry I'm confused by what you are saying. The enum type handler in mybatis does not handle converting off your id value.
You can use it as a reference to create your own to handle id though. On Wed, Apr 18, 2012 at 4:20 PM, Ramy Vilensky <[hidden email]> wrote: Based on the documentation, mybatis already has two enum type handlers. Regular that goes by enum value (String), and ordinal. |
|
OK, I can technically use Status.ordinal() to achieve the same goal. In that case I can get rid of the whole getter setter for ID's.
So, how do I use the out-of-the-box enum handler? Could you provide a code example? On Wed, Apr 18, 2012 at 3:56 PM, Tim <[hidden email]> wrote: Sorry I'm confused by what you are saying. The enum type handler in mybatis does not handle converting off your id value. |
|
Oh ok you want to get rid of the ids. That is easy to do.
You need to specify a typeHandler. If you look at the docs (http://www.mybatis.org/core/sqlmap-xml.html)
It is:
#{age,javaType=int,jdbcType=NUMERIC,typeHandler=MyTypeHandler}On Wed, Apr 18, 2012 at 6:03 PM, Ramy Vilensky <[hidden email]> wrote: OK, I can technically use Status.ordinal() to achieve the same goal. In that case I can get rid of the whole getter setter for ID's. |
|
Still doesn't work... here are the relevant code fragments:
MyBatis config: <typeAliases> <typeAlias type="com.mine.Status" alias="Status"/> <typeAlias type="org.apache.ibatis.type.EnumTypeHandler" alias="EnumHandler"/> </typeAliases> Status Mapping: <mapper namespace="com.mine.mybatis.StatusMapper"> <select id="getStatusByName" resultType="Status" parameterType="String"> SELECT ls.id, ls.name FROM status AS ls WHERE ls.name = #{name,typeHandler=EnumHandler} </select> </mapper> DAO Code: Status newStatus = null; log.error("new status 1 = " + newStatus); try { newStatus = (Status)session.selectOne("com.mine.mybatis.StatusMapper.getStatusByName","NEW"); <- FAILS SOMEWHERE HERE, THE NEXT LOG STATEMENT DOESN'T EXECUTE log.error("new status 2 = " + newStatus); session.commit(); } finally { session.close(); } log.error("new status 3 = " + newStatus); return newStatus.ordinal(); On Wed, Apr 18, 2012 at 4:20 PM, Tim <[hidden email]> wrote: Oh ok you want to get rid of the ids. That is easy to do. |
|
Why don't you catch the exception and print the stack trace to find out the location of the problem? Richard Sent from my iPhone
|
|
Hi Ramy, please tell us what version are using and what is the log/stactrace.
El día 21 de abril de 2012 06:01, Richard Yee <[hidden email]> escribió: > Why don't you catch the exception and print the stack trace to find out the > location of the problem? > > Richard > > Sent from my iPhone > > On Apr 20, 2012, at 3:29 PM, Ramy Vilensky <[hidden email]> wrote: > > Still doesn't work... here are the relevant code fragments: > > MyBatis config: > <typeAliases> > <typeAlias type="com.mine.Status" alias="Status"/> > <typeAlias type="org.apache.ibatis.type.EnumTypeHandler" > alias="EnumHandler"/> > </typeAliases> > > Status Mapping: > > <mapper namespace="com.mine.mybatis.StatusMapper"> > > <select id="getStatusByName" resultType="Status" > parameterType="String"> > SELECT ls.id, ls.name > FROM status AS ls > WHERE ls.name = #{name,typeHandler=EnumHandler} > </select> > > </mapper> > > DAO Code: > > Status newStatus = null; > > log.error("new status 1 = " + newStatus); > > try { > newStatus = > (Status)session.selectOne("com.mine.mybatis.StatusMapper.getStatusByName","NEW"); > <- FAILS SOMEWHERE HERE, THE NEXT LOG STATEMENT DOESN'T EXECUTE > > log.error("new status 2 = " + newStatus); > > session.commit(); > } finally { > session.close(); > } > > log.error("new status 3 = " + newStatus); > > return newStatus.ordinal(); > > > On Wed, Apr 18, 2012 at 4:20 PM, Tim <[hidden email]> wrote: >> >> Oh ok you want to get rid of the ids. That is easy to do. >> You need to specify a typeHandler. If you look at the docs >> (http://www.mybatis.org/core/sqlmap-xml.html) >> >> It is: >> >> #{age,javaType=int,jdbcType=NUMERIC,typeHandler=MyTypeHandler} >> >> >> On Wed, Apr 18, 2012 at 6:03 PM, Ramy Vilensky <[hidden email]> >> wrote: >>> >>> OK, I can technically use Status.ordinal() to achieve the same goal. In >>> that case I can get rid of the whole getter setter for ID's. >>> >>> So, how do I use the out-of-the-box enum handler? Could you provide a >>> code example? >>> >>> >>> On Wed, Apr 18, 2012 at 3:56 PM, Tim <[hidden email]> wrote: >>>> >>>> Sorry I'm confused by what you are saying. The enum type handler in >>>> mybatis does not handle converting off your id value. >>>> You can use it as a reference to create your own to handle id though. >>>> >>>> >>>> >>>> On Wed, Apr 18, 2012 at 4:20 PM, Ramy Vilensky <[hidden email]> >>>> wrote: >>>>> >>>>> Based on the documentation, mybatis already has two enum type handlers. >>>>> Regular that goes by enum value (String), and ordinal. >>>>> What I can't figure out is how I modify my mapper to include it. >>>>> >>>>> >>>>> On Wed, Apr 18, 2012 at 2:08 PM, Tim <[hidden email]> wrote: >>>>>> >>>>>> sry i meant typehandler :P >>>>>> >>>>>> org.apache.ibatis.type.TypeHandler >>>>>> >>>>>> On Wed, Apr 18, 2012 at 4:05 PM, Tim <[hidden email]> wrote: >>>>>>> >>>>>>> You should add a typemapper for the enum >>>>>>> >>>>>>> >>>>>>> On Wed, Apr 18, 2012 at 3:38 PM, Ramy Vilensky >>>>>>> <[hidden email]> wrote: >>>>>>>> >>>>>>>> I know this has been asked before, but I wasn't able to implement a >>>>>>>> solution based on the information I found so far. so perhaps someone can >>>>>>>> explain it to me. >>>>>>>> >>>>>>>> I have a table "status". It has two columns:id and name. id is a PK. >>>>>>>> >>>>>>>> Instead of using a POJO Status, I would like to use an enum. I >>>>>>>> created such an enum as follows: >>>>>>>> >>>>>>>> public enum Status { >>>>>>>> NEW(1), READY(2), CLOSED(3); >>>>>>>> >>>>>>>> private int id; >>>>>>>> >>>>>>>> public void setId(int id) { >>>>>>>> this.id = id; >>>>>>>> } >>>>>>>> >>>>>>>> public int getId() { >>>>>>>> return this.id; >>>>>>>> } >>>>>>>> >>>>>>>> Status(int id) { >>>>>>>> this.id = id; >>>>>>>> } >>>>>>>> } >>>>>>>> >>>>>>>> here is my mapper >>>>>>>> <mapper namespace="com.mine.StatusMapper"> >>>>>>>> >>>>>>>> <select id="getStatusByName" resultType="Status" >>>>>>>> parameterType="String"> >>>>>>>> SELECT ls.id, ls.name >>>>>>>> FROM status AS ls >>>>>>>> WHERE ls.name = #{name} >>>>>>>> </select> >>>>>>>> >>>>>>>> </mapper> >>>>>>>> >>>>>>>> but for some reason, when I try to retrieve an enum, something >>>>>>>> breaks, but no exception is thrown. >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>> >>>>>> >>>>> >>>> >>> >> > |
|
That's the thing... there doesn't appear to be an exception thrown.
On Fri, Apr 20, 2012 at 10:15 PM, Eduardo Macarron <[hidden email]> wrote: Hi Ramy, please tell us what version are using and what is the log/stactrace. |
|
An exception is being thrown. You just don't have a catch block for it. Richard Sent from my iPhone
|
| Powered by Nabble | Edit this page |
