Stored procedure mapping for many-to-many association - sql-server

I have a relatively simple table of Terms, and each Term can have multiple parents and children so there is a TermAssociation table.
Term TermAssociation
---- ---------------
TermID ParentTermID
TermName ChildTermID
...
When mapped in EF, this generates a Term entity with a many-to-many association with itself. Everything's cool.
The problem is I work in an environment where all table updates must go through stored procedures. I can use stored procedure mapping just fine for the Term entity, but how do I map an SP to the TermAssociation table since it's modeled as an association and not an entity?

I haven't found a way to do this through the designer, but it is possible if you edit the XML of the edmx file directly. Find the association set mapping:
<AssociationSetMapping Name="TermAssociation" TypeName="TCPDataDictionaryModel.TermAssociation" StoreEntitySet="TermAssociation">
<EndProperty Name="Term">
<ScalarProperty Name="TermId" ColumnName="ParentTermId" />
</EndProperty>
<EndProperty Name="Term1">
<ScalarProperty Name="TermId" ColumnName="ChildTermId" />
</EndProperty>
</AssociationSetMapping>
Then add a ModificationFunctionMapping inside the AssociationSetMapping. InsertAssociation is my insert SP and it takes #ParentTermId and #ChildTermId as parameters.
<ModificationFunctionMapping>
<InsertFunction FunctionName="TCPDataDictionaryModel.Store.InsertAssociation" >
<EndProperty Name="Term">
<ScalarProperty Name="TermId" ParameterName="ParentTermId" />
</EndProperty>
<EndProperty Name="Term1">
<ScalarProperty Name="TermId" ParameterName="ChildTermId" />
</EndProperty>
</InsertFunction>
</ModificationFunctionMapping>

Related

Having an issue with XML containing nested objects to POCO (de)serialisation (VB.Net)

I've created an SP in SQL Server that returns as XML. I decided to do this as the information has contacts and addresses in it and I wanted to reduce the amount of data I get.
<Accounts>
<Account>
<Company />
<ContactNumber />
<Addresses>
<Address>
<Line1 />
....
</Address>
<Addresses>
<Contacts>
<Contact>
<Name />
....
</Contact>
<Account>
</Accounts>
I have found SqlCommand.ExecuteXmlReader but I'm confused as to how to serialise this into my POCO. Can someone point me at what my next step is. (The POCO was created by the Insert XML as a class menu item in VS2019).
My Google fu is letting me down as I'm not sure what I should be looking for to help understand how to serialize the XML into something that will allow me to go with Foreach Account in AccountsClass type logic.
Any help is greatly appreciated.
PS The XML above is just a sample to show what I'm doing. The actual data is over 70 fields and with two FK joins the initial 40000 rows is well in excess of 1.8 million once selected as a normal dataset.
EDIT: Just in case someone stumbles on this and are in the same situation I was in.
When preparing a sample record for the Past XML to class make sure you have more than one record if you are expecting something similar to my example above. (The class changes to support more than one record.)
You get very different results when searching for deserialize when doing your research. This small change resulted in me figuring out the issue.

XML Attributes to SQL

I've seen posts that are similar to what I'm about to ask, but I can't find anything that actually solves my problem. I've seen (and duplicated) getting XML tags into SQL, but not getting the attributes of the tags into SQL.
Background: We use a program that runs "events" based on each event's schedule. The schedules are in XML. Ultimately, I'm trying to compare the last time a given event ran to the last time it should have run, based on its schedule. Once I get the schedules out of XML and into a table (or tables), I'm confident I can take it from there. But, I'm STRUGGLING with that first step.
Below is the XML I'm trying to get into a table (or tables). Any help would be greatly appreciated!!
<Schedule LastModified="2016-06-27T21:02:10.6041531Z" TimeZone="(UTC-06:00) Central Time (US & Canada)" ConvertedToUTC="True" Type="Weekly">
<Beginning StartDate="2016-05-26T22:26:00.0000000" />
<Block BlockType="AllDay" Interval="10" IntervalType="Minute" SetType="Inclusive" Start="15:00:00" End="17:00:00" Duration="02:00:00" />
<Interval Type="Weekly" RecurEveryX="1" Sunday="False" Monday="True" Tuesday="True" Wednesday="True" Thursday="True" Friday="True" Saturday="False" />
<Ending Type="NoEndDate" />
</Schedule>
Don't know, where you have your issues... Reading this XML is rather trivial (did not code all of the values, but you'll get the idea):
DECLARE #mockup TABLE(YourXML XML);
INSERT INTO #mockup VALUES
(N'<Schedule LastModified="2016-06-27T21:02:10.6041531Z" TimeZone="(UTC-06:00) Central Time (US & Canada)" ConvertedToUTC="True" Type="Weekly">
<Beginning StartDate="2016-05-26T22:26:00.0000000" />
<Block BlockType="AllDay" Interval="10" IntervalType="Minute" SetType="Inclusive" Start="15:00:00" End="17:00:00" Duration="02:00:00" />
<Interval Type="Weekly" RecurEveryX="1" Sunday="False" Monday="True" Tuesday="True" Wednesday="True" Thursday="True" Friday="True" Saturday="False" />
<Ending Type="NoEndDate" />
</Schedule>');
SELECT m.YourXML.value(N'(/Schedule/#LastModified)[1]',N'datetime') AS Schedule_LastModified
,m.YourXML.value(N'(/Schedule/#TimeZone)[1]',N'nvarchar(max)') AS Schedule_TimeZone
,m.YourXML.value(N'(/Schedule/Beginning/#StartDate)[1]',N'datetime') AS Beginning_StartDate
,m.YourXML.value(N'(/Schedule/Block/#BlockType)[1]',N'nvarchar(max)') AS Block_BlockType
,m.YourXML.value(N'(/Schedule/Block/#Interval)[1]',N'int') AS Block_Interval
,m.YourXML.value(N'(/Schedule/Interval/#Type)[1]',N'nvarchar(max)') AS Interval_Type
FROM #mockup AS m;
If this is not your solution, please edit your question: Add your own attempt, the wrong output and the expected output. Your explanation did not make is all clear to me...

How to create a table in adf with variable no of columns

Hello I am developing a web application using ADF (jdeve11.1.2.4). I know how to populate a table programatically from this post Programmatic ADF Table
But in the above post the no of columns are fixed (It is bean structure). But I cannot use the above post. Because I need to create a table with 'n' no-of columns. Means Columns are not fixed. Some times the columns may be 4 or some times the columns may be 7 and what ever it may be. Suppose I have two sqls and both contains differnt no of columns.
Ex: (suppose student is a table)
select id from student
select id,name from student
so in the above two sqls no of columns are diffent. I need to show the resul set of the above queries in a tablular format.
Please help me how I can achieve this.
Thanks in advance.
I wonder if declarative mode view objects would help? Blog here. So the query is not built until the UI is gen'd. You could dynamically generate the table and insert columns and then bind to the VO.
That's quite complex to do and it may require some extra study:
create a programatic view object by passing your sql statement using:
ApplicationModuleImpl.createViewObjectFromQueryStmt(java.lang.String voName,java.lang.String query)
try to construct af:columns in a loop:
<af:table value="#{bindings.EmployeesView1.collectionModel}" .. id="t1">
<af:iterator id="i1" value="#{bindings.EmployeesView1.attributesModel.attributes}" var="column" rows="0">
<af:column headerText="#{column.label}" id="c1">
<af:outputText id="d1" attributeModel="#{column}" value="#{row.bindings[column.name].inputValue}" />
</af:column>
</af:iterator>
</af:table>

hibernate with java.sql.BatchUpdateException

I am using hibernate to map our classes to tables in oracle.
My class has a primary key as id , gets automatically generated by hibernate
<id name="jobId" type="long">
<column name="JOBID" />
<generator class="increment" />
</id>
In my code I do:
Job job = new Job();
do some config for the job.
saveOrUpdate(job);
At this saveOrUpdate I encountered:
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at com.myCompany.BasicDaoImpl.saveOrUpdate(BasicDaoImpl.java:37)
at com.myCompany.JobRoutine.generateJob(JobRoutine.java:142)
Caused by: java.sql.BatchUpdateException: ORA-00001: unique constraint (DBGROUP.SYS_C0011345) violated
at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10700)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 13 more
I find the constraint is priamry_key.
This error not happy always, but sometimes.
Could anyone please give me some suggestion about it?
Thanks so much!
The documentation says:
increment
generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. Do
not use in a cluster.
You probably have another process inserting rows in the same table, and Hibernate is unaware of it, because the increment generator just storesthe next value in memory and assumes it's the only one to insert rows in this table.

reuse proxy call data for extjs store

I have a xml that i need to bind to XTemplate. The XML structure is as follows
<Name>
<Student_Name>
<First>John</First>
<Last>Smith</Last>
</Student_Name>
<Student_Name>
<First>John</First>
<Last>Doe</Last>
</Student_Name>
<Faculty_Name>
<First>Johny</First>
<Last>Byrd</Last>
</Student_Name>
</Name>
I am using Ext.data.Store with proxyurl tot he xml, Ext.data.XML reader to rad the xml and a listener that bind the data to the Xtemplate.
The xmlreader needs a root node to be specified and i have to give root name as "Student_Name"
It need to bind it to one more store for "Faculty_Name" as the root node. SO i end up calling my service to get xml twice. Is there a way to call service once to get the xml and bind it to two stores with different root nodes.
At the risk of sounding like Captain Obvious, you actually need 2 root nodes in your data. I would also suggest renaming your nodes (if possible) to eliminate redundancy and make your XML more semantically logical, e.g.,
<People>
<Students>
<Name>
<First>John</First>
<Last>Smith</Last>
</Name>
<Name>
<First>John</First>
<Last>Doe</Last>
</Name>
</Students>
<Faculty>
<Name>
<First>Johny</First>
<Last>Byrd</Last>
</Name>
</Faculty>
</People>
That way Students and Faculty can be your 2 separate root nodes and you'll only need a single server call.

Resources