Any chances you could switch the member id of Child to java.lang.Integer ?
Werner > -----Ursprüngliche Nachricht----- > Von: SuperMMX [mailto:[EMAIL PROTECTED] > Gesendet: Montag, 19. März 2007 03:16 > An: [email protected] > Betreff: [castor-user] Map mapping with Integer as the key > > > hi, all, > > I use the an Integer as the key in map collection in my mapping file, > but it is always String. So I can't retrieve the value by providing an int > key. > > Am I missing something here? Thanks for all your help. > > I am not subscribed to this list, please CC me. > > Enclosed are my testing files. > > mapping:: > > <?xml version="1.0"?> > > <mapping> > <class name="com.Parent"> > <map-to xml="parent" /> > <field name="children" type="com.Child" collection="map"> > <bind-xml name="child"> > </bind-xml> > </field> > </class> > > <class name="com.Child" identity="id"> > <map-to xml="child" /> > <field name="id" type="integer"> > <bind-xml name="id" node="attribute"> > </bind-xml> > </field> > </class> > </mapping> > > xml file: > > <?xml version="1.0"?> > > <parent> > <child id="1"/> > <child id="2"/> > </parent> > > Parent.java > > package com; > > import java.util.HashMap; > import java.util.Map; > > public class Parent { > > private HashMap<Integer, Child> children; > > public Map<Integer, Child> getChildren() { > return children; > } > > public void setChildren(Map<Integer, Child> children) { > this.children = new HashMap<Integer, Child>(children); > } > } > > Child.java > > package com; > > public class Child { > private int id; > > public int getId() { > return id; > } > > public void setId(int id) { > this.id = id; > } > } > > My unit test : > > @Test public void test() { > try { > InputStream is = > getClass().getResourceAsStream("/mapping.xml"); > Mapping mapping = new Mapping(getClass().getClassLoader()); > mapping.loadMapping(new InputSource(is)); > unmar = new Unmarshaller(mapping); > unmar.setIgnoreExtraElements(true); > > InputStream stream = > getClass().getResourceAsStream("/test.xml"); > DocumentBuilder builder = dbf.newDocumentBuilder(); > Document doc = builder.parse(stream, "parent"); > doc.getDocumentElement().normalize(); > > String exprStr = "/parent"; > XPathExpression expr = xpath.compile(exprStr); > Node node = (Node)expr.evaluate(doc, XPathConstants.NODE); > > Parent parent = (Parent)unmar.unmarshal((Element)node); > System.err.println(parent.getChildren()); > for (Map.Entry entry : parent.getChildren().entrySet()) { > System.err.println("key class = " + > entry.getKey().getClass()); > } > } catch (Exception e) { > e.printStackTrace(); > } > } > > The Result is : > > [junit] ------------- Standard Error ----------------- > [junit] [EMAIL PROTECTED], [EMAIL PROTECTED] > [junit] key class = class java.lang.String > [junit] key class = class java.lang.String > [junit] ------------- ---------------- --------------- > > -- > A. Because it makes the logic of the discussion difficult to follow. > Q. Why shoudn't I top post? > A. No. > Q Should I top post? > > A: Because it destroys the flow of the conversation > Q: Why is it bad? > A: No, it's bad. > Q: Should I top post in replies to mailing lists? > > --------------------------------------------------------------------- > To unsubscribe from this list please visit: > > http://xircles.codehaus.org/manage_email --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email

