PETAPOCO No Property of type ICollection`1 found on object of type: - petapoco

I am having my sql as
Connection.db.FetchOneToMany<Project, CustomerProject>(x => x.ID, string.Format(#"
SELECT *
FROM Project
left join customerProject on customerProject.ProjectID = project.id
where customerProject.CustomerID = #0 ORDER BY project.Name", customerID));
which is giving me the error
No Property of type ICollection`1 found on object of type: Project
CustomerProject definition
ID CustomerID ProjectID
Project definition
ID Name
What is wrong with the query?

You don't have a property of type List<CustomerProject> on your Project type. This is required if you want to do a OneToMany<> query. You will have to use the [Ignore] attribute on it too.

Related

Metabase - Field Filter as text

I have this SQL query:
select concept, count(*)
from annotation
where exists (select 1
from annotation a2
where a2.comment_commentid = annotation.comment_commentid and a2.concept = 'Fatigue'
)
group by concept;
And I want to replace 'Fatigue' with {{word}}, to do a filter widget, maping to the column from database.
I have the following error:
ERROR: syntax error at or near "=" Position: 307
What I need to change to aplly the filter? selecting the available words from that column?
With variable type as Text it works... But don't display all the available options, in filter, as variable type Field Filter do...
Thanks!
The outer annotation table needs an alias too. When in doubt, the inner scope always prevails whern resolving names, and the inner exists(...) query an an annotation name in scope, too)
[And the cause of your error is probably that the middleware gets confused]
select concept, count(*)
from annotation a1 -- <<-- HERE!
where exists (select 1
from annotation a2
where a2.comment_commentid = a1.comment_commentid and a2.concept = 'Fatigue'
)
group by concept;

Field "REF" is unknown in ADBC statement

I'm having trouble using ABDC.
This is the code I am trying to run:
DATA:
gr_sql_result_set TYPE REF TO cl_sql_result_set,
gr_sql_statement TYPE REF TO cl_sql_statement.
START-OF-SELECTION.
CREATE OBJECT gr_sql_statement.
gr_sql_result_set = gr_sql_statement->execute_query( 'SELECT VBELN ERDAT ERNAM AUDAT VKORG FROM VBAK' ).
gr_sql_result_set->set_param_table( itab_ref = REF # ( gt_orders_head ) ).
The problem lies in gr_sql_result_set->set_param_table. The REF # statement gives me the following error: Field "REF" is unknown. It is neither in one of the specified tables nor defined by a "DATA" statement.
Is it possibile that I don't have the right version of SAP-ABAP installed which supports this statement?
Note: gt_orders_head is defined as gt_orders_head TYPE TABLE OF zordhead_str.
zordhead_str is a structure that I defined/created in Transaction S11.
I'm currently using SAP_BASIS Release 731
The constructor expressions like REF require SAP_BASIS 7.40 or later. So you can not use them in your 7.31 system until you update.
In the meantime you will have to work with a temporary reference variable instead:
DATA gt_order_head_ref LIKE REF TO gt_orders_head.
GET REFERENCE OF gt_orders_head INTO gt_order_head_ref.
gr_sql_result_set->set_param_table( itab_ref = gt_order_head_ref ).

Cast exception in native sql query using hibernate

I have a sql query using tables in it. the result set is return to a bean class which is not mapped to a table in database. the code is here:
SQLQuery q2=ss.createSQLQuery("select tbl_policy.policyNum as POLICYNUM FROM tbl_policy join tbl_product on tbl_policy.FK_productId = tbl_product.pk_product_id join tbl_code on tbl_policy.FK_codeId = tbl_code.PK_codeId join tbl_agriyear on tbl_policy.FK_agriYearId = tbl_agriyear.pk_agriyear_id where tbl_policy.FK_naturalInsurantId = :p1 and tbl_agriyear.AGRIYEAR =:p2");
q2.addScalar("POLICYNUM", Hibernate.STRING);
List<SearchPolicyBean> lsql = (List<SearchPolicyBean>)q2.list();
bean class name is: SearchPolicyBean
when I run it, in this line
System.out.println("Finalllll "+lsql.get(0).getPOLICYNUM());
this error appears:
java.lang.String cannot be cast to BO.SearchPolicyBean
addScalar(String columnAlias)
Is to Declare a scalar query result.
You declared it as a String here q2.addScalar("POLICYNUM", Hibernate.STRING);
So obviously the query results a String list not your SearchPolicyBean pojos .
If you are expectiong an list of your pojo classe you have to use
q2.addEntity(SearchPolicyBean.Class);
And change your query according.
for getting first element:
setPolicyNum((String) ((Object[])polList.get(i))[0]);

Linq to sql - get value from db function and not directly from the db field (while mapping properties)

When you map a table to an object, every property created corresponds to one db column.
I want to execute a db function on a column before it gets mapped to the property, so the property gets the value returned by the db function, and not the column
I was trying to achieve that by Expression property of ColumnAttribute (as in the example below), so instead of BirthDate the usrFn_UTCToLocalTime(BirthDate) is returned
but it does not seem to be working and still gets pure value of the column.
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_BirthDate", DbType = "DateTime", UpdateCheck = UpdateCheck.Never, Expression = "dbo.usrFn_UTCToLocalTime(BirthDate)")]
public System.Nullable<System.DateTime> BirthDate
{
get
{
return this._BirthDate;
}
}
I have also modified the DBML XML as in:
other post on stackoverflow
but also without result.
Is that possible by using LINQ or do I have to overwrite a getter which costs roundtrip to the server?
According to the Remarks section on this MSDN page, the Expression property of the ColumnAttribute is used when calling CreateDatabase, so it won't work the way you intend unless you created your database with Linq to Sql.
You can create a Linq query that selects the various columns and calls the db function in one statement like this (based on MSDN example):
var qry = from person in db.Persons
select new {
FirstName = person.FirstName,
LastName = person.LastName,
BirthDate = person.BirthDate,
Dob = db.usrFn_UTCToLocalTime(person.BirthDate)
};
This projects into an anonymous type, but you could use a non-anonymous type as well. For the sake of the example, I've got a table named Person with FirstName, LastName, and BirthDate columns, and a user defined scalar function named usrFn_UTCToLocalTime. The sql statement sent to the server is:
SELECT [t0].[FirstName], [t0].[LastName], [t0].[BirthDate], CONVERT(DateTime,[dbo].[usrFn_UTCToLocalTime]([t0].[BirthDate])) AS [Dob]
FROM [dbo].[Person] AS [t0]
As I was suggesting in the question, for now I have overwritten the get method so I have:
get
{
using (var context = DB.Data.DataContextFactory.CreateContext())
{
return context.usrFn_UTCToLocalTime(_BirthDate);
}
//return this._BirthDate;
}
But with every access to the property, roundtrip is made - which is not my intention but gives a proper result.
I leave the question still open

Error using Join on uniqueidentifier - convert uniqueidentifier to numeric

I am using Fluent NHibernate on sqlserverce.
Using NHibernate QueryOver I try to retrieve a row - NHibernate generate automatically a a join query
and I get the following Exception:
[SQL: SELECT tag FROM CheckpointToProtectionGroup cp2pg
JOIN CheckpointStorageObject cp ON cp.id = cp2pg.checkpoint_id
JOIN ProtectionGroupCheckpointStorageObject pg ON pg.id = cp2pg.vpg_id
WHERE cp.CheckpointIdentifierIdentifier = 1111 AND
pg.ProtectionGroupIdentifierGroupGuid =
11111111-1111-1111-1111-111111111111]
---> System.Data.SqlServerCe.SqlCeException:
The conversion is not supported.
[ Type to convert from (if known) = uniqueidentifier,
Type to convert to (if known) = numeric ]
From what I see, it seems that it tries to convert the value - 11111111-1111-1111-1111-111111111111 to numeric but this value is a Guid field:
CheckpointToProtectionGroup checkpointToProtectionGroup = Session
.QueryOver<CheckpointToProtectionGroup>()
.JoinQueryOver( row => row.ProtectionGroup)
.Where(row => row.ProtectionGroupIdentifier.GroupGuid ==
protectionGroupIdentifier.GroupGuid)
.SingleOrDefault();
ProtectionGroupIdentifier.GroupGuid is of Guid type
Looks like your GroupGuid value is not correctly converted to SQL. It should have single quotes around the value.
pg.ProtectionGroupIndentifierGroupGuidId = '11111111-1111-1111-1111-111111111111'
SQL Server tried to convert left hand value from uniqueidentifier (Guid) to numeric, since the right hand value is numeric value - numeric subtract operation with few operands.
You have protectionGroupIdentifier.GroupGuid value in Where part of your QueryOver expression. Check if GroupGuid is indeed Guid property. If it's an object property, cast it to Guid.