Unable to set the ID of an instance - codefluent

What is unique about my situation is that the ID's can not be randomly assigned so I set it's value within the instance. I created several instances of an entity using the modeler. Below is the XML created:
<cf:entity name="Test4" namespace="Amikids.TimeTracking" categoryPath="/Amikids.TimeTracking">
<cf:property name="Id" key="true" typeName="int" />
<cf:property name="Name" />
<cf:instance>
<cf:instanceValue name="Id">10</cf:instanceValue>
<cf:instanceValue name="Name">Test 1</cf:instanceValue>
</cf:instance>
<cf:instance>
<cf:instanceValue name="Id">20</cf:instanceValue>
<cf:instanceValue name="Name">Test 2</cf:instanceValue>
</cf:instance>
<cf:instance>
<cf:instanceValue name="Id">30</cf:instanceValue>
<cf:instanceValue name="Name">Test 3</cf:instanceValue>
</cf:instance>
</cf:entity>
There are 2 things that are not working as expected:
The records inserted do not use the ID specificed in the model/xml. Instead they were created incrementally starting at 1:
(The below is displayed in a code snippet only to prevent StackOverflow from reformatting my list so all records appear on one line)
ID Name
1 Test 1
2 Test 2
3 Test 3
When I build the model a second time duplicate records are inserted.
(The below is displayed in a code snippet only to prevent StackOverflow from reformatting my list so all records appear on one line)
ID Name
1 Test 1
2 Test 2
3 Test 3
4 Test 1
5 Test 2
6 Test 3

Although specifying the ID in the instance does not appear to be work, as a simple work around I created the records using code, which allowed me to specify the ID. This has been verified with the following code snippet.
Amikids.TimeTracking.Test4 test4 = new Amikids.TimeTracking.Test4();
test4.Id = 100;
test4.Name = "Test 100";
test4.Save();
test4 = new Amikids.TimeTracking.Test4();
test4.Id = 200;
test4.Name = "Test 200";
test4.Save();

Related

Postgres XML parsing - Calculate the sum of multiple nodes of the same name using XPath

I have an xml snippet as below:
<OpCodeLaborInfo JobStatus="F" UpSellFlag="N" JobNo="1" OpCode="02TTZ10K" OpCodeDesc="10K SERVICE">
<TechInfo ActualHrsWorked="2.50" CustTechRate="27.00" TechHrs="0.00" TechName="JEFF SELLERS" TechNo="4816" />
<TechInfo ActualHrsWorked="0.00" CustTechRate="27.00" TechHrs="0.70" TechName="JEFF SELLERS" TechNo="4816" />
<BillTimeRateHrs BillRate="129.97" />
<CCCStmts Correction="PERFORMED 10K SERVICE" Complaint="LUBE OIL FILTER CHANGE, TIRE ROTATION, PERFORM MULTI POINT" />
<CCCStmts Correction="X" Complaint="INSPECTION, INSPECT FILTERS AND RECOMMEND, INSPECT BRAKES," />
<CCCStmts Complaint="BELTS AND HOSES" />
<RoAmts DlrCost="18.90" PayType="Cust" AmtType="Job" TotalAmt="59.12" />
</OpCodeLaborInfo>
<OpCodeLaborInfo JobStatus="F" UpSellFlag="N" JobNo="2" OpCode="02TTZ10K" OpCodeDesc="10K SERVICE">
<TechInfo ActualHrsWorked="2.50" CustTechRate="27.00" TechHrs="1.00" TechName="JEFF SELLERS" TechNo="4816" />
<TechInfo ActualHrsWorked="0.00" CustTechRate="27.00" TechHrs="0.00" TechName="JEFF SELLERS" TechNo="4816" />
<BillTimeRateHrs BillRate="129.97" />
<CCCStmts Correction="PERFORMED 10K SERVICE" Complaint="LUBE OIL FILTER CHANGE, TIRE ROTATION, PERFORM MULTI POINT" />
<CCCStmts Correction="X" Complaint="INSPECTION, INSPECT FILTERS AND RECOMMEND, INSPECT BRAKES," />
<CCCStmts Complaint="BELTS AND HOSES" />
<RoAmts DlrCost="18.90" PayType="Cust" AmtType="Job" TotalAmt="59.12" />
</OpCodeLaborInfo>
I need to calculate the sum of the TechInfo/#TechHrs for each OpCodeLaborInfo. I tried the following:
unnest(xpath('sum(//dns:RepairOrder/dns:RoRecord/dns:Rolabor/dns:OpCodeLaborInfo/dns:TechInfo/#TechHrs[1])'::text,
data_detail.ro_data_xml,
ARRAY[ARRAY['dns'::text, 'http://www.starstandards.org/STAR'::text]]))::text::numeric AS lbrsoldhours
but this seems to return the sum of the Tech Hours inside both the OpCodeLaborInfo nodes. Could someone be able to tell me how I can tweak the xpath so as to get the desired result.
So basically I need :
Job
Tech Hrs
1
sum(0.00+0.70)
2
sum(1.00+0.00)
I would solve this using xmltable()
select d.job, sum(t.hours)
from data_detail d
cross join xmltable (
'/OpCodeLaborInfo/TechInfo'
passing d.ro_data_xml
columns hours numeric path '#TechHrs') as t
group by d.job;
Online example
The XPath is probably not correct as the XPath you have shown doesn't match your sample XML data. Your sample XML also doesn't contain a namespace, so I am not sure why you are passing one to xpath()
But if you need one, you can use something like this:
cross join xmltable (
xmlnamespaces ('http://www.starstandards.org/STAR' as dns),
'/dns:OpCodeLaborInfo/dns:TechInfo'
passing d.ro_data_xml
columns hours numeric path '#TechHrs') as t
xpath can also work fine:
SELECT job
, SUM((xpath('//#TechHrs', element))[1]::text::decimal)
FROM data_detail
, LATERAL (SELECT unnest(xpath('/OpCodeLaborInfo/TechInfo', ro_data_xml))) u(element)
GROUP BY job;
fiddle
(based on the fiddle of #a_horse_with_no_name)

NHibernate inserting wrong Id

I am using MVC razor 5
I'm making managment page to add SelectList values (like Car models)
I have values in my car models table:
--------------------------------------
Id | Name | Deleted
1 | BMW | False
2 | Audi | False
3 | Ford | False
4 | BMW5 | False
5 | SEAT | False
--------------------------------------
When i add new value id becomes 130++
I have noticed that my main class last records Id is 130.
And when I insert a record in car model class Id is the nextval(Id) of main class
But these two tables does not share same Controller or so...
All sessions are closed by transaction.Commit() so session closes.
What may be wrong?
Nhibernate class mapping:
<class name="web_nt.Models.Class.CarModel" table="car_model" schema="public">
<id name="Id" column="id" type="int">
<generator class="native" />
</id>
<property name="model_name" />
<property name="deleted" />
</class>
It was generator class fault.
To use postgres sequence I needed to specify it (NHibernate does not always catch it itself)
<generator class="sequence">
<param name="sequence">car_model_id_sequence</param>
</generator>

How can I convert this T-SQL to Fetch-XML?

I want to use this SQL in a Dynamics CRM report. I can't work out how to convert it to Fetch-XML.
select p.ProductNumber "Plan Number",p.Name,p.price "Monthly Rate",count(*) "Group", '0' "Direct Debit"
from contact c,product p
where c.integ_schemeid = p.ProductId
and c.ParentCustomerId is not null
group by p.ProductNumber,p.Name,p.price
union
select p.ProductNumber "Plan Number",p.Name,p.price "Monthly Rate", '0' "Group", count(*) "Direct Debit"
from contact c,product p
where c.integ_schemeid = p.ProductId
and c.ParentCustomerId is null
group by p.ProductNumber,p.Name,p.price
http://www.sql2fetchxml.com fails on this occasion.
You can't convert that query to FetchXML, because unions aren't supported. What you'll need to do is look into merging the queries into 1 if possible. Even if it means duplicating columns and using conditional statements within your report to display the relevant data instead. For example, if you simplified the query to this:
select p.ProductNumber "Plan Number", p.Name, p.price, c.ParentCustomerId
from product p
inner join contact c on c.integ_schemeid = p.ProductId
This can be converted to the following fetchxml:
<fetch mapping="logical">
<entity name="product">
<attribute name="ProductNumber" alias="Plan Number" />
<attribute name="Name" />
<attribute name="price" />
<link-entity name="contact" to="ProductId" from="integ_schemeid" alias="c" link-type="inner">
<attribute name="ParentCustomerId" />
</link-entity>
</entity>
</fetch>
Then, because you have all of the data in 1 dataset including contacts with null ParentCustomerIds, you can group within your report instead of within the query.

Group by in JPA

My Question is why the GROUP BY didn't show in the glassfish logs.
i added the following data below because someone wants to see it
public List<Message> listAllMessageBySender(String currentUserID) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PersistenceUnit");
EntityManager entityManager = emf.createEntityManager();
TypedQuery<Message> eq = entityManager.createQuery("SELECT m FROM Message m WHERE m.sendBy.userId = :userid GROUP BY m.sendTo.userId", Message.class);
eq.setParameter("userid", Integer.parseInt(currentUserID));
List<Message> message = eq.getResultList();
return message;
}
Glassfish 3.1.2.Logs
FINE: SELECT id, message, send_date, send_by, send_to FROM message WHERE (send_by = ?)
bind => [50]
Data:
User A : sendBy.userId = 50
User B : sendTo.userId = 44
User C : sendTo.userId = 43
id send_date send_by send_to message
1 2013-03-14 14:58:46 44 50 50 send message to 44
2 2013-03-14 14:58:46 50 44 reply from 44 to 50
3 2013-03-14 17:38:52 44 50 2nd reply to 50
6 2013-03-14 18:22:10 50 44 44 reply to 50
7 2013-03-14 18:22:10 50 43 new
in the query above is i want to list all the user sent from User A. it causes duplication because there is two send_to 44. the xhtml below shows :
<ui:repeat value="#{messageController.items}" var="messageItem">
<br />
<div onclick="loadMessage()">
<h:panelGroup layout="block" >
<p:graphicImage height="50" width="50" value="#{messageItem.sendTo.profilePicture}"/>
</h:panelGroup>
<h:panelGroup layout="block" style="width: 270px;">
<h:outputText value="#{messageItem.sendTo.registerName}"/>
<h:panelGroup layout="block" style="float:right;">
<h:outputText value="#{messageItem.sendDate}" >
<f:convertDateTime pattern="HH:mm"/>
</h:outputText>
</h:panelGroup>
</h:panelGroup>
</div>
<p:remoteCommand name="loadMessageBean" action="#{messageController.loadMessage(messageItem.id)}" /> </ui:repeat>
ERD
User Table Message Table
user_id message_id
register_name send_date
send_to ----> referenced by user_id User Table
send_by ----> referenced by user_id User Table
This request doesn't make sense. group by is used when the select clause contains at least one aggregation function (sum, avg, min, max, etc.). And all the other columns must appear in the group by clause.
So for example, the following query would make sense:
SELECT max(m.length), sender.userId from Message m
inner join m.sendBy sender
GROUP BY sender.userId
It would return, for each message sender, the length of its longest message.
Tell us, with example data, what you would like your query to return.
If you want to get all the distinct users whom user A sent a message to, the query should simply be
select distinct m.sendTo from Message m
where m.sendBy.userId = :userid

Adding an entity key when no key is inferred from a view

I have a database view which joins across a number of tables in SQL Server 2005. It looks something like this:
SELECT
m1.MenuName AS menu_name, m2.MenuName AS sub_menu_name, p.ProductName, od.amount
FROM
dbo.tblMenus AS m1
FULL OUTER JOIN
dbo.tblMenus AS m2
FULL OUTER JOIN
dbo.tblProductsRelMenus AS pm ON m2.Id = pm.SubMenuId ON m1.Id = pm.MenuId
FULL OUTER JOIN
(SELECT
dbo.tblOrderDetails.ProductId, SUM(dbo.tblOrderDetails.Ammount) AS amount
FROM
dbo.tblOrderDetails
FULL OUTER JOIN
dbo.tblOrders AS o ON dbo.tblOrderDetails.OrderId = o.OrderId
WHERE (o.OrderDestroyed = 0)
GROUP BY dbo.tblOrderDetails.ProductId) AS od
RIGHT OUTER JOIN
dbo.tblProducts AS p ON od.ProductId = p.ProductId ON pm.ProductId = p.ProductId
When I try to create an ADO .Net entity data model it complains about not having a primary key in the SSDL secion. I then found this:
http://msdn.microsoft.com/en-us/library/dd163156.aspx
but I don't understand the part about a defining query. Surely I just want a column with unique numbers to define the key, or?
<EntityType Name="SoldItemsView">
<Key>
<PropertyRef Name="SoldItemsViewId" />
</Key>
<Property Name="SoldItemsView" Type="int" Nullable="false" />
<Property Name="menu_name" Type="nvarchar" MaxLength="100" />
<Property Name="sub_menu_name" Type="nvarchar" MaxLength="100" />
<Property Name="ProductName" Type="nvarchar" MaxLength="50" />
<Property Name="amount" Type="int" />
</EntityType>
But how do I populate this column with unique numbers?
Thanks,
Barry
You can use only columns from the view. To define an entity key you must select column (or set of columns) from your view which uniquely identifies record from the view.