Open JPA : An error occurred while parsing the query filter 'MY_QUERY' Error message: No field named "accessAuthorizationVs" in class "MyEntityClass" - jpa

I have configured it in my Rational Software Architect 8.0.4, by enabling the JPA 1.0 facet. It autogenerates almost all my entity classes except for the id's. So I manually add them. I am trying to query a simple table APP_USER that has one-to-many relation to ACCESS_AUTHORIZATION table. See below for the configurations and entity classes.
When I try to execute a simple named query which is
SELECT a.accessAuthorizationVs, a.empName, a.userCnum FROM AppUserEntity a WHERE a.userCnum = :userCnum
It throws an exception
**org.apache.openjpa.persistence.ArgumentException: An error occurred while parsing the query filter "SELECT a.accessAuthorizationVs, a.empName, a.userCnum FROM AppUserEntity a WHERE a.userCnum = :userCnum". Error message: No field named "accessAuthorizationVs" in class "class com.xxx.xxx.xxx.services.integration.entity.AppUserEntity".**
at org.apache.openjpa.kernel.exps.AbstractExpressionBuilder.parseException(AbstractExpressionBuilder.java:118)
at org.apache.openjpa.kernel.exps.AbstractExpressionBuilder.traversePath(AbstractExpressionBuilder.java:284)
at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.getPath(JPQLExpressionBuilder.java:1382)
at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.getPathOrConstant(JPQLExpressionBuilder.java:1337)
Here's a snapshot of my persistence.xml:
Can anyone guide me what I am doing wrong here? The field by that name is clearly defined in my entity class. Also I would like to mention that I had to enhance the classes[followed this] as there was an earlier error about classes not being enhanced.

When you create a named query in OpenJPA, remember that you are writing JPQL, not native SQL. The syntax is similar, but a little different.
In this case, I suggest changing your named query to the following:
SELECT a FROM AppUserEntity a WHERE a.userCnum = :userCnum
This will return an object of the AppUserEntity class which will include the set of AccessAuthorizationV objects (lazy loaded by default).
For more details, see the JPQL Language Reference.

Related

EF7 "Invalid Object Name 'xyz'" when manually scaffolding existing database

I've published a DB using an SqlDatabase Project.
I've manually created POCO classes, as well as a Db context to match the published database.
Whenever I try to execute an EF statement, valid SQL Query is generated, but I receive the error
An exception occurred in the database while iterating the results of a query.
System.Data.SqlClient.SqlException (0x80131904): Invalid object name '[xyz]'
The properties on the EF class match exactly type/nullable/name/etc in the database. The query generated, when run manually works fine.
Any help appreciated.

LIKE Column query

It is easy to do a Contains, StartsWith, or EndsWith query in Entity Framework when you know the pattern you'd like to find. But how to do the equivalent of "LIKE [Column]"?
For instance, this is easy:
Name LIKE '%Th'
But how do you do this? where Prefix is a column.
Name LIKE [Prefix]
I tried to use the SqlMethods and got the following error.
LINQ to Entities does not recognize the method 'Boolean Like(System.String, System.String)' method, and this method cannot be translated into a store expression.
I resolved this problem by using LINQ to Entities to bring back the set of data that I wanted, then using LINQ to Objects to filter the data.
I do not think there is currently a way to have a LINQ to Entities query be resolved in a database server as ...
... LIKE [column-name]
First, SqlMethods is for use in LINQ-to-SQL, not LINQ-to-Entities.
The best you can do to use the filter in the database engine (so you don't have to pull all data in memory first) is:
context.Entities.SqlQuery("SELECT * FROM EntityTable WHERE Name LIKE Prefix")

EclipseLink "SQL string is not Query"

I'm running EclipseLink on WLS 10.3.5.
I'm trying to use #NamedNativeQuery annotation, but all the SQL I try causes an "Internal Exception: java.sql.SQLException: SQL string is not Query" exception.
Even a simple SELECT * FROM TABLE causes that error where TABLE is the table that the containing Entity is mapped to.
The annotation is:
#NamedNativeQuery(name = AnnouncementDeliveryLog.FIND_NORMALIZED_RECIPIENTS_FOR_ANNOUNCEMENT, query = "SELECT * FROM ANNOUNCEMENT_DELIVERY_LOG", resultClass = AnnouncementDeliveryLog.class)
The query is being executed with:
em.createNativeQuery(AnnouncementDeliveryLog.FIND_NORMALIZED_RECIPIENTS_FOR_ANNOUNCEMENT).getResultList();
I can't find a good way to get more useful information from the system so any information is appreciated.
Wrong method is in use. Method createNativeQuery takes native SQL string as an argument. In given code argument seems to be name of the native query. Method createNamedQuery should be used, it takes name of the native query as an argument.

Ebean generates sql with undercase_notation not CamelCase as expected

I'm setting up a Play! 2 application with an already existing db. The entities have been ported to the new app. When running the application I get a PersistenceException since
the sql generated by ebean uses undercase_notation and not CamelCase as it did in my
previous application. So my restfulIdentifier property becomes restful_identifier_id
instead of restfulIdentifier_id.
I have read the documentation at http://www.avaje.org/ebean/getstarted_props.html but I can't find the settings.
Stack:
PersistenceException: Query threw SQLException:Unknown column 't0.restful_identifier_id'
in 'field list' Bind values:[200926947] Query was: select t0.id c0, t0.name c1, t0.state c2,
t0.restful_identifier_id c3 from company t0 where t0.id = ?
You will have to modify the meta data of the Entity to tell JPA that your columns have a different name now. Either you are using Annotations or XML files. However you need to compile the sources (or at least enahnce it) to get it done.
This is a really old question but Ebean has 2 naming convention implementations - UnderscoreNamingConvention and MatchingNamingConvention ... and MatchingNamingConvention would provide the camel case convention you are after.
So again, the answer would be to do
MatchingNamingConvention namingConvention = new MatchingNamingConvention();
ServerConfig serverConfig = ...
serverConfig.setNamingConvention(namingConvention);

Doubt regarding JPA namedquery

I am trying to execute a namedquery
#NamedQuery(name="getEmployeeDetails",query="select e.username,e.email,e.image,e.firstname,e.lastname from Employee e where e.empid=?1")
Now when I execute this query in a EJB 3.0 Session Bean what is the object I should return.I tried returning Listits returning a Vector which creates a classcast exception.The employee table contains fields like password and other confidential details which I don't want to fetch.So I am not using select e from Employee e.
I am learning JPA can anyone help.
Below is the sample query which fetches only the required fields, but have to make such constructor for it.
Query : SELECT NEW package_name.Employee(e.username,e.email,e.image,e.firstname,e.lastname) FROM Employee e where e.empid=?1;
It will return Employee entity with selected fields & remaining will have default values.
Inspect the returned type by calling .getClass() on a returned object. I'd guess it's an array.
But this is not really a good way to use JPA. Select the whole entity and then just don't use what you don't need. It's not such a performance hit.