NHibernate inserting wrong Id - postgresql

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>

Related

TSQL Parse XML with namespace

I'm trying to parse some xml that's stored in the database inside of stored procedure. The procedure should return 2 columns, id and value. I'm only part of the way through this, I'm stuck on the fact that I can't list even list the "Setting" nodes.
declare #PolicySettingsXml xml
set #PolicySettingsXml = '<?xml version="1.0" encoding="utf-8"?>
<Policy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.myurl.com/app/Policies">
<Setting id="VendorInfo0">
<string>fsdfdsfds</string>
</Setting>
<Setting id="VendorInfo1">
<string />
</Setting>
<Setting id="VendorInfo2">
<string />
</Setting>
<Setting id="SendSurchargeAsSeparateTransaction">
<boolean>false</boolean>
</Setting>
<Setting id="VendorSurchargeInfo0">
<string />
</Setting>
<Setting id="VendorSurchargeInfo1">
<string />
</Setting>
<Setting id="VendorSurchargeInfo2">
<string />
</Setting>
</Policy>'
select T.c.query('string') as value, T.c.query('#id') as id from #PolicySettingsXml.nodes('/Policy/Setting') T(c)
I'm getting an error, "XQuery [query()]: Attribute may not appear outside of an element," I'd expect to see:
id | value
VendorInfo0 | fsdfdsfds
VendorInfo1 | <null>
VendorInfo2 | <null>
SendSurchargeAsSeparateTransaction | <null>
VendorSurchargeInfo1 | <null>
VendorSurchargeInfo2 | <null>
The first issue is related to syntax.
This:
from PSXML.x.nodes('/*:Policy/*:Setting') T(c);
should be:
from (VALUES(#PolicySettingsXml)) AS PSXML(x)
CROSS APPLY PSXML.x.nodes('/*:Policy/*:Setting') T(c);
For what you are doing you can use the value method for the id column.
select
id = T.c.value('(#id)[1]', 'varchar(100)'),
[value] = T.c.query('(*:string/text())[1]')
from (VALUES(#PolicySettingsXml)) AS PSXML(x)
CROSS APPLY PSXML.x.nodes('/*:Policy/*:Setting') T(c);
Note that I am cheating on the namespaces using the "all namespaces" syntax: * :Object. A google search for "sql server t-sql xml namespaces" will show you the way you're supposed to do it. I cheated due to time constraints.

Unable to set the ID of an instance

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();

Mybatis resultmap ​only outputs one of three records

I have 3 records to be populated from a medication table​:​
select * from ccda_medication where client_id = 100009;
client_id | rxcui | drug | medicationtime | status | directions |pharmacynote
-----------+--------+---------------------------------------+----------------+--------+------------------------------------+
100009 | 573621 | Albuterol 0.09 MG/ACTUAT [Proventil] | 20120806 | ACTIVE | 2 puffs every 6 hours PRN wheezing |
100009 | 866924 | Metoprolol Tartrate 25 MG Oral Tablet | 20120806 | ACTIVE | by mouth once daily|
100009 | 197517 | Clarithromycin 500 MG Oral Tablet | 20120806 | ACTIVE | by mouth twice daily for 7 days|
(3 rows)
The debug also shows that 3 records were retrieved from database.
==> Preparing: select * from ccda_medication where client_id = ?
==> Parameters: 100009(Integer)
<==Columns: client_id, rxcui, drug, medicationtime, status, directions, pharmacynote
<==Row: 100009, 573621, Albuterol 0.09 MG/ACTUAT [Proventil], 20120806, ACTIVE, 2 puffs every 6 hours PRN wheezing,
<==Row: 100009, 866924, Metoprolol Tartrate 25 MG Oral Tablet, 20120806, ACTIVE, by mouth once daily,
<==Row: 100009, 197517, Clarithromycin 500 MG Oral Tablet, 20120806, ACTIVE, by mouth twice daily for 7 days,
<== Total: 3
but I only g​e​t one record ​when I call selectMedications- the last one: Row: 100009, 197517
​Interestingly, if I ​flatten out Substance to contain all fields directly (no inner class), I get all 3 records, so the column names are correct.
The Mapper file is as the following:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="ccda.mapper.interfaces.MedicationMapper">
<select id="**selectMedications**"
resultMap="MedicationsResultMap"
parameterType="Integer">
select * from ccda_medication where client_id = #{id}
</select>
<resultMap id="MedicationsResultMap" type="ccda.model.Substance">
<result property="status" column="status"/>
<association property="substanceTime" javaType="ccda.model.EffectiveTime">
<result property="low" column="medicationtime"/>
<result property="high" column="medicationtime"/>
</association>
<association property="medication" javaType="ccda.model.Medication">
<result property="rxcui" column="rxcui"/>
<result property="drug" column="drug"/>
<result property="directions" column="directions"/>
<result property="fillInstructions" column="pharmacynote"/>
</association>
</mapper>
The Substance, Medication and MedicationMapper are as the following:
package ccda.model;
public class Substance
{
public String status;
public EffectiveTime substanceTime;
public Medication medication;
}
package ccda.model;
public class Medication
{
public int rxcui;
public String drug;
public String directions;
public String fillInstructions;
}
package ccda.mapper.interfaces;
import java.util.List;
import ccda.model.Medication;
import ccda.model.Substance;
public interface MedicationMapper
{
List<Substance> selectMedications( int id );
}
​Can you please help me figure out why is that? Thanks A LOT!
The problem is that identifier field for substance is not specified. Because of this mybatis doesn't know what is the identity of returned entities and in this case it thinks that all records refer to the same entity.
So specify id in the resultMap and use unique field from ccda_medication for it.

XQuery modify with variable

I have the follow XML
<root>
<business name="LM" id="1" total_pes="0">
</business>
<business name="KO" id="354" total_pes="0">
</business>
<business name="TUI" id="889" total_pes="0">
</business>
</root>
I want to update the total_pes attribute with a record count of other table tbl_logs:
id | log
1 | A
1 | A
1 | A
354 | A
354 | A
889 | A
My output XML would be this:
<root>
<business name="LM" id="1" total_pes="3">
</business>
<business name="KO" id="354" total_pes="2">
</business>
<business name="TUI" id="889" total_pes="1">
</business>
</root>
This is what I already done:
DECLARE #total_pes_new int
DECLARE #ID INT
SET #ID = (SELECT TOP 1 ID FROM #IDS)
WHILE #ID IS NOT NULL
BEGIN
set #total_pes_new = ( SELECT COUNT(A.PES) FROM TBL A
WHERE A.ID = #ID)
SET #XML.modify('replace value of (/root/business[#id=sql:variable("#ID")]/#total_pes)[1] with sql:variable("#total_pes_new")')
SET #ID = (SELECT TOP 1 ID FROM #IDS WHERE ID > #ID)
END
I will have problems with this loop. Can anyone help me to do a better solution?
Tks
If your XML is in an XML database, then you also have the option of using XQuery Update, see http://www.w3.org/TR/xquery-update-10/.

powershell parsing of cdata-section

I'm trying to read an rss feed using powershell and I can't extract a cdata-section within the feed
Here's a snippet of the feed (with a few items cut to save space):
<item rdf:about="http://philadelphia.craigslist.org/ctd/blahblah.html">
<title>
<![CDATA[2006 BMW 650I,BLACK/BLACK/SPORT/AUTO ]]>
</title>
...
<dc:title>
<![CDATA[2006 BMW 650I,BLACK/BLACK/SPORT/AUTO ]]>
</dc:title>
<dc:type>text</dc:type>
<dcterms:issued>2011-11-28T22:15:55-05:00</dcterms:issued>
</item>
And the Powershell script:
$rssFeed = [xml](New-Object System.Net.WebClient).DownloadString('http://philadelphia.craigslist.org/sss/index.rss')
foreach ($item in $rssFeed.rdf.item) { $item.title }
Which produces this:
#cdata-section
--------------
2006 BMW 650I,BLACK/BLACK/SPORT/AUTO
2006 BMW 650I,BLACK/BLACK/SPORT/AUTO
How do I extract the cdata-section?
I tried a few variants such as $item.title."#cdata-section" and $item.title.InnerText which return nothing. I tried $item.title | gm and I see the #cdata-section listed as a property. What am I missing?
Thanks.
Since you have multiple of those, the title property itself would be an array, so the following should work:
$rss.item.title | select -expand "#cdata-section"
or
$rss.item.title[0]."#cdata-section"
based on what you need.