Using user-type-mappings for Enums
ansancle Nov 28, 2005 10:26 AMIm using JBoss 4.0.2, Eclipse 3.1.0, JBoss-IDE 1.5.0.RC1, MySQL 4.1.12, mysql-connector-java-3.1.7-bin.jar on Red Hat Enterprise 3 Linux.
I have an entity bean with 2 enumerated types that I want to store in the database as integers. I have followed the instructions in the WIki ; http://wiki.jboss.org/wiki/Wiki.jsp?page=UserTypeMapping
1. Created mapping classes that implement the Mapper interface
2. Added the following to the jbosscmp-jdbc.xml file :
<user-type-mappings> <user-type-mapping> <java-type>com.digitalhome.msg.DHActionType</java-type> <mapped-type>java.lang.Integer</mapped-type> <mapper>com.digitalhome.db.DHActionTypeMapper</mapper> </user-type-mapping> <user-type-mapping> <java-type>com.digitalhome.msg.DHMessageType</java-type> <mapped-type>java.lang.Integer</mapped-type> <mapper>com.digitalhome.db.DHMessageTypeMapper</mapper> </user-type-mapping> </user-type-mappings>
Here is the code for one of the mapper classes :
/** * DigitalHome copyright 2005 * * Filename : DHActionTypeMapper.java * Package : com.digitalhome.db * Create Date : Nov 18, 2005 * Author : root * */ package com.digitalhome.db; import org.jboss.ejb.plugins.cmp.jdbc.Mapper; import com.digitalhome.msg.DHActionType; /** * @author root * */ public class DHActionTypeMapper implements Mapper { /** * Constructor */ public DHActionTypeMapper() { super(); // TODO Auto-generated constructor stub } /** * This method is called when CMP field is stored. * @param fieldValue - CMP field value * @return column value. */ public Object toColumnValue(Object fieldValue) { DHActionType type = (DHActionType)fieldValue; return new Integer(type.getType()); } /** * This method is called when CMP field is loaded. * @param columnValue - loaded column value. * @return CMP field value. */ public Object toFieldValue(Object columnValue) { int ordinal = ((Integer)columnValue).intValue(); return DHActionType.getActionType(ordinal); } }
I have breakpoints set in the mapper classes which are never hit, so it appears the mapper classes are not being called, and the values in the database for the enum columns are not being entered properly, the default of 0 is being entered.
Does anyone have any ideas on what Im doing wrong or missing?
Thanks
-Andy