org.hibernate.hql.internal.ast.QuerySyntaxException: Employees is not mapped - jpa-2.0

we have an application where mapping is done through orm.xml files and now trying add annotaitons. Is it possible to have both combined.
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="disc" transaction-type="JTA">
<jta-data-source>disc.jdbc.DataSource.OLTP</jta-data-source>
<!-- List of Mapping files -->
<mapping-file>META-INF/discCommon.orm.xml</mapping-file>
<mapping-file>META-INF/discTerminology.orm.xml</mapping-file>
<class>com.model.correspondence.Employees</class>
<class>com.model.correspondence.AbstractEmployees</class>
</persistence-unit>
</persistence>
in entity class
#Entity
#Table(name = "TEST", schema="company")
public class Employees extends AbstractEmployees{
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = IDENTITY)
#Column(name = "EMPLOYEE_ID", unique=true, nullable = false)
private long employeeId;
#Column(name = "DESCRIPTION", nullable = false)
private String description;
Serivceclass
private static final String SEARCH_FROM_ALL_EMPLOYEES= " select o from " + Employees.class.getSimpleName()+ " o WHERE " +
" upper(o.description) LIKE :desc";
Query query = entityManager.createQuery(SEARCH_FROM_ALL_EMPLOYEES);
query.setParameter("desc", "%" + desc.toUpperCase() + "%");
#SuppressWarnings("rawtypes")
List employeeList= query.getResultList();
orm.xml files are working but the annotation entity class are not getting mapped. Below is error
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Employees is not mapped [ select o from Employees o WHERE upper(o.description) LIKE :desc]*
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:324)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3420)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3309)
Thanks

Maybe you can try adding this line in your persistence.xml
<exclude-unlisted-classes>false</exclude-unlisted-classes>
Sometimes, because of different configuration at our projects, the classes generated weren't ubicated in a path were JPA (and the implementation you used) can auto-scanning.
So, in my case, I used that line to tell to the JPA implementation that must map that classes I put in the persistence.xml and does't wait to the auto-scanning.
In that case, a possibility for your persistence.xml will be
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="disc" transaction-type="JTA">
<jta-data-source>disc.jdbc.DataSource.OLTP</jta-data-source>
<!-- List of Mapping files -->
<mapping-file>META-INF/discCommon.orm.xml</mapping-file>
<mapping-file>META-INF/discTerminology.orm.xml</mapping-file>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<class>com.model.correspondence.Employees</class>
<class>com.model.correspondence.AbstractEmployees</class>
</persistence-unit>
</persistence>
I hope, this works for you...

Related

Hot to access principalResult of the transformation? [saxonJS]

I migrate from saxon-CE to saxonJS (v1.2.0)
The output of the XSLT transformation need to be captured as an XML Document object as it was in saxon-CE:
var xslPath = './*.xsl';
var xsl = Saxon.requestXML(xslPath);
var proc = Saxon.newXSLT20Processor(xsl);
var xmlDoc;
var xmlDocTransformed;
var xmlStr;
xmlDoc = Saxon.parseXML(app.getLoadedMEI());
xmlDocTransformed = proc.transformToDocument(xmlDoc);
It tried to apply SaxonJS this way:
var result;
result = SaxonJS.transform({
stylesheetLocation: "./*.sef.xml",
sourceLocation: "./*.xml",
destination: "application"
});
and expected to get a transformation results object where I can access the principalResult property as described in the official documentation (#destination) and in this presentation.
When running the code I obtain the following:
There is no problem with transformation itself: when destination is set to replaceBody it works as expected.
Eventually I solved my task with the following code (Saxon JS 2.3):
var options = {
stylesheetLocation: xslPath,
sourceText: inputXmlStr,
stylesheetParams: params,
destination: "document"
};
var result = SaxonJS.transform(options);
var transformedXmlStr = SaxonJS.serialize(result.principalResult);
The SEF could be produced by xslt3 tool.
Note that you might use the alternative command for this (windows 10 power shell):
node node_modules/xslt3/xslt3.js "-t" "-xsl:stylesheet.xsl" "-export:stylesheet.sef.json" "-nogo"
One way is, of course, to use the fn:transform function with "normal" XSLT code:
const xml = `<root>
<item>a</item>
</root>`;
const xslt = `<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:output method="xml"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="/" name="xsl:initial-template">
<xsl:next-match/>
<xsl:comment>Run with {system-property('xsl:product-name')} {system-property('xsl:product-version')} {system-property('Q{http://saxon.sf.net/}platform')}</xsl:comment>
</xsl:template>
</xsl:stylesheet>`;
const resultString = SaxonJS.XPath.evaluate(`transform(map { 'source-node' : parse-xml($xml), 'stylesheet-text' : $xslt, 'delivery-format' : 'serialized' })?output`, null, { params : { 'xml' : xml, 'xslt' : xslt } });
console.log(resultString);
<script src="https://martin-honnen.github.io/Saxon-JS-2.3/SaxonJS2.js"></script>
Not recommended for performance reason but perhaps a workaround that avoid the hassle of creating an SEF. If you want to create an SEF, note, that the Node.js xslt3 tool from Saxonica can also do that, you don't need a current version of Saxon EE, just xslt3 -t -export:my-sheet.sef.json -nogo -xsl:my-sheet.xsl.

How to transform XML document with CDATA using JDOM2?

Source document:
<content><![CDATA[>&< test]]></content>
XSLT document (cdata-transformation.xslt):
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" cdata-section-elements="transformed" />
<xsl:template match="/content">
<transformed>
<xsl:value-of select="." />
</transformed>
</xsl:template>
</xsl:stylesheet>
Wanted result:
<?xml version="1.0" encoding="UTF-8"?>
<transformed><![CDATA[>&< test]]></transformed>
Actual result:
<?xml version="1.0" encoding="UTF-8"?>
<transformed>>&< test</transformed>
Code used to test using JDOM2:
import java.io.IOException;
import java.io.InputStream;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import org.jdom2.CDATA;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;
import org.jdom2.transform.JDOMResult;
import org.jdom2.transform.JDOMSource;
import org.junit.Test;
public class CdataTransformationTest {
#Test
public void learning_cdataTransformationWithJdom() throws Exception {
Document xslt = loadResource("xslt/cdata-transformation.xslt");
Document source = new Document(new Element("content")
.addContent(new CDATA(">&< test")));
Document transformed = transform(source, xslt);
XMLOutputter outputter = new XMLOutputter();
System.out.println(outputter.outputString(transformed));
}
private static Document transform(Document sourceDoc, Document xsltDoc) throws TransformerException {
JDOMSource source = new JDOMSource(sourceDoc);
JDOMResult result = new JDOMResult();
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(new JDOMSource(xsltDoc));
transformer.transform(source, result);
return result.getDocument();
}
private static Document loadResource(String resource) throws IOException, JDOMException {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classloader.getResourceAsStream(resource);
if (inputStream != null) {
try {
SAXBuilder builder = new SAXBuilder();
return builder.build(inputStream);
} finally {
inputStream.close();
}
} else {
return null;
}
}
}
JDOM version used:
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.6</version>
</dependency>
XSLT processor used:
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>2.7.1</version>
</dependency>
I have searched around for ways to do this, and the best answers say that what is needed to wrap content in CDATA is to add the tag name in the cdata-section-elements attribute. I cannot get this to work with JDOM and not when using the Free online XSL Transformer either. I have also tried using saxon instead of xalan, but with the same result.
Why doesn't this work? What am I missing/doing wrong here? Is JDOM ignoring the cdata-section-elements attribute?
I have also tried wrapping the content like this:
<xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text>
<xsl:value-of select="." />
<xsl:text disable-output-escaping="yes">]]></xsl:text>
But this produces an unwanted result in JDOM which makes it difficult to work with. Visible when you set the outputer.getFormat().setIgnoreTrAXEscapingPIs(true); and it looks really ugly when using pretty format.
<?xml version="1.0" encoding="UTF-8"?>
<transformed>
<?javax.xml.transform.disable-output-escaping?>
<![CDATA[
<?javax.xml.transform.enable-output-escaping?>
>&< test
<?javax.xml.transform.disable-output-escaping?>
]]>
<?javax.xml.transform.enable-output-escaping?>
</transformed>
You are transforming to a JDOMResult, that is, a tree representation, and not to a stream or file. Output directives like cdata-section-elements are only used when the XSLT processor serializes the result to a stream or file, but not when building a result tree in memory. So I think if you want to construct CDATA sections as the result of XSLT with cdata-section-elements, you need to make sure you write to a file or stream or at least a StringWriter, then you could load the JDOM result from that file or stream respectively created String.
Rewriting the transform method to:
private static Document transform(Document sourceDoc, Document xsltDoc) throws JDOMException, IOException, TransformerException {
StringWriter writer = new StringWriter();
JDOMSource source = new JDOMSource(sourceDoc);
Result result = new StreamResult(writer);
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(new JDOMSource(xsltDoc));
transformer.transform(source, result);
SAXBuilder builder = new SAXBuilder();
return builder.build(new StringReader(writer.toString()));
}

Apache CXF client namespace is in element instead of envelope

The problem I'm having is with Apache CXF putting the namespace inside the element instead of soap envelope.
I used codegen maven plugin to generate the client code from WSDL.
Here are the namespaces in the WSDL:
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
When I fire a SOAP request I can see in logs that the following message has been sent:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns1:userRegistrationRequest xmlns:ns1="http://some.com/namespace1">
<InputParameter xmlns:ns2="http://some.com/namespace1">
<ns2:Message>
<ns2:Value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<ns2:HashTable>
<ns2:Item key="action">
<ns2:Value>some_action</ns2:Value>
</ns2:Item>
</ns2:HashTable>
</ns2:Message>
</InputParameter>
</ns1:userRegistrationRequest>
</soap:Body>
The problem is that the Value element was null in my code when I constructed the message, but here it appears with XSI namespace.
What I was expecting to get is something like this (Value element should not be present and XSI should be in the envelope element):
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<ns1:userRegistrationRequest xmlns:ns1="http://some.com/namespace1">
<InputParameter xmlns:ns2="http://some.com/namespace1">
<ns2:Message>
<ns2:HashTable>
<ns2:Item key="action">
<ns2:Value>some_action</ns2:Value>
</ns2:Item>
</ns2:HashTable>
</ns2:Message>
</InputParameter>
</ns1:userRegistrationRequest>
</soap:Body>
Does anyone have an idea how to prevent the CXF from generating that empty Value element and put the namespace inside the soap envelope element?
Ok, so to answer my question. There are two aspects of this problem.
First, adding namespaces to the envelope element. It can be done by writting a custom interceptor and registering it in some early phase. Something like this:
import java.util.Map;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.Phase;
public class MyInterceptor extends AbstractSoapInterceptor {
private Map<String, String> nsMap;
public MyInterceptor(Map<String, String> nsMap) {
super(Phase.USER_LOGICAL);
this.nsMap = nsMap;
}
public void handleMessage(SoapMessage message) throws Fault {
message.put("soap.env.ns.map", nsMap);
message.put("disable.outputstream.optimization", Boolean.TRUE);
}
}
I found the above solution here
Secondly, to remove empty value element an xsd should be changed to that the element has minOccurs="0" and nillable = "false".

JAXP pipeline ignores xsl:comment's (using Saxon HE 9.5.1-4)

When using JAXP pipelin (using Saxon HE), the comments created with don't appear in the resulted .xml.
First I set the system properties to get info and use Saxon and define the input/output:
System.setProperty("jaxp.debug", "1");
System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
StreamSource xsl = ...
StreamResult output = ...
InputSource input = ...
Then I have the following construction with dummy Pre and Post filters:
TransformerFactory factory = TransformerFactory.newInstance();
SAXTransformerFactory saxFactory = (SAXTransformerFactory) factory;
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
parserFactory.setNamespaceAware(true);
XMLReader parser = parserFactory.newSAXParser().getXMLReader();
XMLFilter pre = new XMLFilterImpl(parser);
XMLFilter xslFilter = saxFactory.newXMLFilter(xsl);
xslFilter.setParent(pre);
XMLFilter post = new XMLFilterImpl(xslFilter);
TransformerHandler serializer = saxFactory.newTransformerHandler();
serializer.setResult(output);
Transformer trans = serializer.getTransformer();
trans.setOutputProperty(OutputKeys.METHOD, "xml");
post.setContentHandler(serializer);
post.parse(input);
When run with the following stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:comment>Nice comment</xsl:comment>
<test>[<xsl:value-of select="system-property('xsl:vendor')" />]
(<xsl:value-of select="system-property('xsl:version')" />
)[<xsl:value-of select="system-property('xsl:vendor-url')" />]</test>
</xsl:template>
</xsl:stylesheet>
I get the follwing output.xml without the comment:
<?xml version="1.0" encoding="UTF-8"?>
<test>[Saxonica]
(2.0
)[http://www.saxonica.com/]
</test>
And the following console log:
JAXP: find factoryId =javax.xml.transform.TransformerFactory
JAXP: found system property, value=net.sf.saxon.TransformerFactoryImpl
JAXP: created new instance of class net.sf.saxon.TransformerFactoryImpl using ClassLoader: null
JAXP: find factoryId =javax.xml.parsers.SAXParserFactory
JAXP: loaded from fallback value: com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
JAXP: created new instance of class com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl using ClassLoader: null
JAXP: find factoryId =javax.xml.parsers.SAXParserFactory
JAXP: loaded from fallback value: com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
JAXP: created new instance of class com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl using ClassLoader: null
When run without the whole pipeline, I do get the coment:
javax.xml.transform.TransformerFactory tFactory = javax.xml.transform.TransformerFactory.newInstance();
javax.xml.transform.Transformer transformer = tFactory.newTransformer(xsl);
transformer.transform(input, output);
results in
<?xml version="1.0" encoding="UTF-8"?><!--Nice comment -->
<test>[Saxonica]
(2.0
)[http://www.saxonica.com/]
</test>
Does anyone know why the JAXP pipeline omits the comments?
The SAX2 ContentHandler interface does not receive notification of comments. For that you need a LexicalHandler. But the SAX2 helper class XMLFilterImpl does not implement LexicalHandler, so it effectively drops the comments.
Switch to s9api in place of JAXP - it does these things much better.

How to Declare Complex Nested C# Type for Web Service

I would like to create a service that accepts a complex nested type. In a sample asmx file I created:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class ServiceNest : System.Web.Services.WebService
{
public class Block
{
[XmlElement(IsNullable = false)]
public int number;
}
public class Cell
{
[XmlElement(IsNullable = false)]
public Block block;
}
public class Head
{
[XmlElement(IsNullable = false)]
public Cell cell;
}
public class Nest
{
public Head head;
}
[WebMethod]
public void TakeNest(Nest nest)
{
}
}
When I view the asmx file in IE the test page shows the example SOAP post request as:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<TakeNest xmlns="http://schemas.intellicorp.com/livecompare/">
<nest>
<head>
<cell>
<block xsi:nil="true" />
</cell>
</head>
</nest>
</TakeNest>
</soap:Body>
</soap:Envelope>
It hasn't expanded the <block> into its number member.
Looking at the WSDL, the types all look good. So is this just a limitation of the post demo page creator?
Thanks.
But those elements ARE null. You need to construct them before they show up otherwise they are just null.
As Kevin pointed out the example POST XML indicates that those elements are nil. I should have simply tried to consume the web service. Once I did that I could see that the importer (either .NET, Java or Ruby) correctly created all the types. So really there is no question here after all.
The .NET code did not give up after a certain number of levels.
If you look at the code generated by "Add Web Reference", you'll find that there's a bool numberSpecified field. Only if the client sets that to true will the number be serialized.
If you look at the XML Schema, you'll see that the number element might be absent. If it were of a reference type, then that could be represented in the client by a null value. Since it's an int, this additional flag is necessary to indicate whether or not to serialize this optional value.