掲示板

ORMException undefined alias

thumbnail
7年前 に Jorge Rivera によって更新されました。

ORMException undefined alias

Junior Member 投稿: 26 参加年月日: 11/11/16 最新の投稿
Hello

I have an external data source (a view in that database), I was able to retrieve the data using custom sql. I'd like very much to avoid creating custom SQL queries

I defined a finder in the entity and tried to retrieve the data using the finder and it is throwing errors

This is my entity with the finder

	<entity name="DataView" table="externaldata_view" session-factory="myAppSessionFactory" tx-manager="myAppTransactionManager" local-service="true" cache-enabled="false" human-name="DataView">
		<column name="column1" type="String" primary="true" />
		<column name="column2" type="String" />
		<column name="column3" type="String" />
		<column name="column4" type="String" />
		<column name="column5" type="String" />
		
		<finder return-type="Collection" name="ViewColumn1">
			<finder-column name="column1" />
		</finder>
	</entity>


I.ve tried to use the persistence functions that are created with the service builder but they don't work either, for instance
dataViewPersistence.findAll()

throws
 com.liferay.portal.kernel.dao.orm.ORMException: org.hibernate.QueryException: undefined alias: com [SELECT dataView FROM com.package.model.DataView dataView]


I have tried also using dynamic queries, selecting a single column from the entity works but when I try to get all columns using:

DynamicQuery query = DynamicQueryFactoryUtil.forClass(DataView.class, PortletClassLoaderUtil.getClassLoader());
		
		try {
			result = DataViewLocalServiceUtil.dynamicQuery(query);
		} catch (SystemException e) {
			e.printStackTrace();
		}


I get:
Caught unexpected exception org.hibernate.MappingException
com.liferay.portal.kernel.exception.SystemException: org.hibernate.MappingException: Unknown entity: com.package.model.DataView


Can someone explain to me why the persistence methods are not working properly? Why are the dynamic queries not working? Is it because I'm using a view? is it because persistence does not play well with external sources?
thumbnail
7年前 に David H Nebinger によって更新されました。

RE: ORMException undefined alias

Liferay Legend 投稿: 14916 参加年月日: 06/09/02 最新の投稿
Yep, there's a blog for that: https://web.liferay.com/web/user.26526/blog/-/blogs/liferay-7-service-builder-and-external-databases

Basically with LR7 SB modules can only point to one data source, the liferayDataSource. When you try to use multiple data sources, some piece effectively loads all aliases/mappings into the liferayDataSource but nothing gets bound to the subsequent data sources.

So you end up with errors re: undefined aliases or unknown entity errors.

Adhering to the restrictions outlined in my blog will get you around these problems and make it all work again.





Come meet me at the LSNA!
thumbnail
7年前 に Jorge Rivera によって更新されました。

RE: ORMException undefined alias

Junior Member 投稿: 26 参加年月日: 11/11/16 最新の投稿
Thanks for the info David.

How about Liferay 6.2 EE? what's the deal there?

Regards
thumbnail
7年前 に David H Nebinger によって更新されました。

RE: ORMException undefined alias

Liferay Legend 投稿: 14916 参加年月日: 06/09/02 最新の投稿
For 6.2 this should not be an issue.






Come meet me at the LSNA!
thumbnail
7年前 に Jorge Rivera によって更新されました。

RE: ORMException undefined alias

Junior Member 投稿: 26 参加年月日: 11/11/16 最新の投稿
Hi David

I've tested the service builder autogenerated methods, if I use them in the jsp files they work but if I try to call them from web service the unable to find model error shows again

This is the method in the DataViewLocalServiceUtil

	public List<string> getColumn1() {
		List<string> result = Collections.emptyList();
		
		DynamicQuery query = DynamicQueryFactoryUtil.forClass(DataView.class);
		query.setProjection(ProjectionFactoryUtil.distinct(ProjectionFactoryUtil.property("Column1")));
		
		try {
			result = DataViewLocalServiceUtil.dynamicQuery(query);
		} catch (SystemException e) {
			logger.error("Error getting Column1");
			logger.error(e.getMessage());
		}
		
		return result;
	}
</string></string>


This code above works in the jsp file but if I try to add it to a web service in DataViewServiceUtil

	public List<string> getColumn1 () {
		return DataViewLocalServiceUtil.getFunnelIDs();
	}
</string>


It throws this error:
java.lang.ClassNotFoundException: com.package.model.impl.DataViewImpl


This is not happening if I use custom sql, I really would like to avoid the custom sql, can this be done?
thumbnail
7年前 に David H Nebinger によって更新されました。

RE: ORMException undefined alias

Liferay Legend 投稿: 14916 参加年月日: 06/09/02 最新の投稿
There's things about what you're writing that I just don't understand...

For example, you don't implement anything in DataViewLocalServiceUtil. You might put this method in DataViewLocalServiceImpl and then rebuild services to expose the method in DataViewLocalServiceUtil, but this method wouldn't appear there directly.

And saying it works in JSP but not in a web service, I'm not sure how you're actually using it. Are you saying in a JSP page you have something like:

&lt;%
List<string> column1 = DataViewLocalServiceUtil.getColumn1();
...
%&gt;</string>


Sure that should work, but I don't know what you mean by how you are using it in a web service?

Web services are usually handled by adding methods to DataViewServiceImpl, rebuild services and then leverage the api/jsonws to get to the data remotely. Is this what you're doing?

I'm just trying to figure out where all of the pieces are and how they truly fit together...




Come meet me at the LSNA!
thumbnail
7年前 に Jorge Rivera によって更新されました。

RE: ORMException undefined alias

Junior Member 投稿: 26 参加年月日: 11/11/16 最新の投稿
David H Nebinger:

Web services are usually handled by adding methods to DataViewServiceImpl, rebuild services and then leverage the api/jsonws to get to the data remotely. Is this what you're doing?


Hi David,

Yes I made a mistake, the code for the web service is implemented in DataViewServiceImpl not in DataViewServiceUtil, it is making reference to the code in DataViewLocalServiceUtil which I implemented in DataViewLocalServiceImpl

I want to load some data using an ajax call from my javascript code, accessing the same methods I made available to my jsp, using DataViewLocalServiceUtil works in jsp but not the web service call, it throws the undefined error.

Does that make sense?
thumbnail
7年前 に David H Nebinger によって更新されました。

RE: ORMException undefined alias

Liferay Legend 投稿: 14916 参加年月日: 06/09/02 最新の投稿
The DataViewServiceImpl should not need to go through DataViewLocalServiceUtil, it should already have the DataViewLocalService injected in it as a member from the super class.






Come meet me at the LSNA!
thumbnail
7年前 に Jorge Rivera によって更新されました。

RE: ORMException undefined alias

Junior Member 投稿: 26 参加年月日: 11/11/16 最新の投稿
David H Nebinger:
The DataViewServiceImpl should not need to go through DataViewLocalServiceUtil, it should already have the DataViewLocalService injected in it as a member from the super class.



So, you're saying that instead of implementing the dynamic queries in DataViewLocalServiceImpl I have to do it in DataViewServiceImpl and then if I want to use them in the jsp or other place in my portlet I just use DataViewLocalServiceUtil and the methods are going to be there because DataViewServiceUtil is parent of DataViewLocalServiceUtil?
thumbnail
7年前 に David H Nebinger によって更新されました。

RE: ORMException undefined alias

Liferay Legend 投稿: 14916 参加年月日: 06/09/02 最新の投稿
No, I'm saying only go through the Xxx(Local)ServiceUtil classes when you do not already have a direct reference.

The super class for DataViewServiceImpl is DataViewServiceBaseImpl. If you open this class, you will see that there is already a member variable dataViewLocalService which will be populated with the value for the local service impl.

So you can define a method in DataViewServiceImpl such as:

public List<string> getColumn1() {
    return dataViewLocalService.getColumn1();
}</string>


The key is not to go through the Util classes unless you're forced to.

Now, whenever folks have issues with DQ, they are typically related to the class loader. I always recommend putting your DQ methods right into the XxxLocalServiceImpl class as it removes the class loader as an issue.

So I would suggest adding a method in DataViewLocalServiceImpl such as:

public List<string> getColumn1() {
    List<dataview> dataViews;

    DynamicQuery query = DynamicQueryFactoryUtil.forClass(DataView.class);
    query.setProjection(ProjectionFactoryUtil.distinct(ProjectionFactoryUtil.property("Column1")));
 
    dataViews = dynamicQuery(query);
    List<string> results = new ArrayList&lt;&gt;(dataViews.size());

    for (DataView dataView : dataViews) {
        results.add(dataView.getColumn1());
    }

    return results;
}</string></dataview></string>


No I should point out that since you've declared your entity has Column1 as the primary key for the table, Column1 must be unique always. So this DQ for unique Column1 values is a bad way to just list all of the records.







Come meet me at the LSNA!
7年前 に Traolly Xiong によって更新されました。

RE: ORMException undefined alias

Regular Member 投稿: 195 参加年月日: 11/12/30 最新の投稿
From reading this... are you talking about the member variable object in the service base impl that looks like this?

ex)
@BeanReference(type = com.custom.HDPortal.SB.service.AltirisLocalService.class)
protected com.custom.HDPortal.SB.service.AltirisLocalService altirisLocalService;

So use this object to get the record of a column vs. the UTIL class?

Confirm if that is correct or am I missing something?

Would going down that route help not throw errors when consuming a remote web service method? I can try it out as I'm experiencing a
similar issue , but wondered about your thoughts.

Thanks.
thumbnail
7年前 に David H Nebinger によって更新されました。

RE: ORMException undefined alias

Liferay Legend 投稿: 14916 参加年月日: 06/09/02 最新の投稿
The Util layer goes through an extra bunch of class loader boundary serialization.

You are in the service layer already, so you don't want to go through this extra layer.

The only time you go through the layer is when you have no other access; no spring context for injection, no existing beam mapped to service layer, ...







Come meet me at the LSNA!
7年前 に Traolly Xiong によって更新されました。

RE: ORMException undefined alias

Regular Member 投稿: 195 参加年月日: 11/12/30 最新の投稿
Make sense. Thank you sir as always.