I want to use interface instead of mapper XML file in MyBatis. In the MyBatis configuration file I define mapper
<mapper class="aa.B" /> where B contains methods annotated with MyBatis annotations. When I try to create a mapper instance session.getMapper(B.class); I get an error:
org.xml.sax.SAXParseException: Attribute "class" must be declared for element type "mapper".
How can I use interfaces to map SQL statements here?
The exception is occurred by absence of the attribute 'class' in 'mapper' element in DTD.
For example, in MyBatis 3.0.1, 'mapper' doesn't have the 'class' attribute in DTD like this:
<!ATTLIST mapper
resource CDATA #IMPLIED
url CDATA #IMPLIED
>
You need to upgrade your MyBatis library to latest one like 3.1.1.
In the version, the attribute definition of 'mapper' is as follows:
<!ATTLIST mapper
resource CDATA #IMPLIED
url CDATA #IMPLIED
class CDATA #IMPLIED
>
You can find your DTD file in the following path in your MyBatis library:
org/apache/ibatis/builder/xml/mybatis-3-config.dtd
You can't add Mapper Interface via the sqlconfig xml file, you've to use the Java API
Configuration.addMapper(B.class)
The node in sqlconfig xml is for definding sqlmap XMLs, not Mapper Interfaces.
Or you could use mybatis-spring, which has support to auto add all the mappers in a said package, to the sqlsessionfactory's configuration.
Related
I have the following XML file:
<node name="FIXED_NAME">
...
</node>
Note that all elements in my XML are node (same name) and I can not change them for some reasons.
I am using java xml validator and xsd file for validation.
I want to validate that the name property for the root node must be FIXED_NAME.
How my xsd file should be?
I cannot think of any solution to your problem, for these reasons:
XSD 1.0 identifies the type of a tag by matching the hierarchy of tags in the XML document against the element/type definitions in the XSD. In your case, there is no hierarchy of names (because all tag names are the same).
XSD 1.1 provides some extra flexibility via xs:alternative and xs:assert - but they do not allow references to the parent axis.
For these reasons, I cannot think of any way to use the features of XML Schema to describe your XML.
If NO annotation is specified in a class and all metadata is specified in an XML descriptor file, how is the access strategy(field/method) determined?
From the book Pro JPA 2:
The access element that is defined in the persistence-unit-defaults
section is used to set the access type for all the managed classes in
the persistence unit that have XML entries but are not annotated. Its
value can be either “FIELD” or “PROPERTY”
<entity-mappings>
<persistence-unit-metadata>
<persistence-unit-defaults>
<access>PROPERTY</access>
</persistence-unit-defaults>
</persistence-unit-metadata>
</entity-mappings>
The default can be overridden via the access attribute on any entity, mapped-superclass, or embeddable element.
I generate XML schema (XSD) from Java classes with JAXB. I wonder how to specify the value of a static attribute by using annotations.
For example I define an attribute like this
#XmlAttribute(name="tooltip")
private static final String TOOLTIP = "A string";
And I want to get in my XSD
<attribute name="tooltip" type="string" fixed="A string">
So, how can I force the generation of static attributes in XSD with JAXB?
Thanks !
As of JAXB 2.2, there isn't standard JAXB (JSR-222) metadata that can add to your model to cause the fixed attribute to appear in the generated XML Schema. The schema generation errs on the side of being too permissive rather than too restrictive. This means you can't do the following:
Mark a fixed value for an attribute
Mark a maxOccurs other than 1 or unbounded
etc.
<ITEM>
<PRODUCT_NAME>IT'S ALL ABOUT YOU-LG:9"H RUBY GATHERNG VASE,RD ROSE,LIME GRN CARN,PURP STOCK,LAV POMS,ATHOS POM,SEAFOAM STATICE,SALAL</PRODUCT_NAME>
<PRODUCT_CODE>90949L</PRODUCT_CODE>
<PRODUCT_TYPE>FPT</PRODUCT_TYPE>
<PRODUCT_CAT>Floral</PRODUCT_CAT>
<ALIAS_NAME>IT'S ALL ABOUT YOU(TM) - LARGE</ALIAS_NAME>
<DELIVERY_DATE>10/11/2012</DELIVERY_DATE>
<FLEX_DATE></FLEX_DATE>
<FLEX_TEXT></FLEX_TEXT>
<QUANTITY>1</QUANTITY>
<PRICE>69.99</PRICE>
<CARD_MESSAGE>IT'S ALL ABOUT YOU...
when i dont add #XmlCDATA annotation i am getting only one product name element. But when i use cdata annotation i am getting duplicate.One with cdata and without cdata
By default a JAXB (JSR-222) implementation will expect annotations to be on the get (or set) methods. If you annotate a field this can cause a property to appear multiple times in the XML representation.
For More Information
http://blog.bdoughan.com/search/label/XmlAccessorType
http://blog.bdoughan.com/2010/07/cdata-cdata-run-run-data-run.html
I figure this will be easy for someone who really understands JAXB binding files...
Basic Question
How do you configure JAXB to unmarshal multiple elements into the same class?
Note: I want to avoid adding another dependency to my project (like MOXy). Ideally, this can be accomplished with annotations or a custom bindings file.
Background
I have an XML document that contains lots of variations of the same element--each with the exact same properties. Using my example below, all I care about is "Employees" but the XML specifies "directors, managers and staff." For our purposes, these are all subclasses of the same parent and we only need to work with the parent type (Employee) and our object model doesn't have or need instances of the subclasses.
I want JAXB to bind any instance of director, manager, or staff elements into an Employee object.
Example
input:
<organization>
<director>
<fname>Dan</fname>
<lname>Schman</lname>
</director>
<manager>
<fname>Joe</fname>
<lname>Schmo</lname>
</manager>
<staff>
<fname>Ron</fname>
<lname>Schwan</lname>
</staff>
<staff>
<fname>Jim</fname>
<lname>Schwim</lname>
</staff>
<staff>
<fname>Jon</fname>
<lname>Schwon</lname>
</staff>
</organization>
output:
After unmarshalling this example, I would end up with an Organization object with one property: List<Employees> employees where each employee only has a firstName and lastName.
(Note: each employee would be of type Employee NOT Director/Manager/Staff. Subclass information would be lost when unmarshalling. We also don't care about marshaling back out--we only need to create objects from XML)
Can this be done without extensions like MOXy? Can a custom bindings.xjb file save the day?
This corresponds to a choice structure. You could use an #XmlElements annotation for this use case:
#XmlElements({
#XmlElement(name="director", type=Employee.class),
#XmlElement(name="manager", type=Employee.class)
})
List<Employee> getEmployees() {
return employees;
}
If you are starting from an XML schema the following will help:
http://blog.bdoughan.com/2011/04/xml-schema-to-java-xsd-choice.html