Getting NHibernate to store unique object only once - nhibernate

I have got four tables in NHibernate
Job
{
DBID { int, primary key }
Name { string }
Media { fk_media (int) }
PublishInfo { fk_publishinfo (int) }
}
Media
{
DBID { int, primary key }
TransmissionID { string }
Series { string }
Title { string }
Description { string }
}
PublishInfo
{
DBID { int, primary key }
Destination { string }
AudioTrack_1 { fk_audiolanguage }
AudioTrack_2 { fk_audiolanguage }
AudioTrack_3 { fk_audiolanguage }
AudioTrack_4 { fk_audiolanguage }
}
AudioLanguage
{
Code { string, primary key }
Description { string }
}
What I want to achive with NHibernate is that it only stores unique records. So if a Media is used multiple times they all point to the same media entry. Same goes for PublishInfo.
A second issue I ran into is that when using the same audiolanguage for audiotrack_3 and 4 for instance it gives a error that is was already used in the session.
Any tips how I would do this properly?

This is a partial anwser to my own question. The reason it wasn't working correctly for the languages when using same lanuage on multiple tracks is because they got send back and forth between a client and server application. The way I solved this is by receiving it back to the server I lookup the proper objects stored in a list on the server and replace them.
I do not think this is the proper solution but it works.

Related

Shopify bulk query results parsing

I am working on a shopify integration, and I am trying to perform some bulk queries which return data in jsonl format.
I read carefully the documentation, and I understood which is the principle behind this format, but there is one thing I don't understand: the following is a portion of my jsonl file representing the first item in the result
{"id":"gid:\/\/shopify\/Product\/6755349070004","options":[{"id":"gid:\/\/shopify\/ProductOption\/8677003133108","name":"Città","position":1}],"title":"Product title","productType":"Concerto Live"}
{"id":"gid:\/\/shopify\/ProductVariant\/40163436363956","price":"100.00","title":"MIlano","__parentId":"gid:\/\/shopify\/Product\/6755349070004"}
{"id":"gid:\/\/shopify\/ProductVariant\/40163436396724","price":"100.00","title":"Roma","__parentId":"gid:\/\/shopify\/Product\/6755349070004"}
{"id":"gid:\/\/shopify\/ProductVariant\/40163436429492","price":"100.00","title":"Firenze","__parentId":"gid:\/\/shopify\/Product\/6755349070004"}
{"id":"gid:\/\/shopify\/Collection\/272323707060","description":"Product variant description","title":"CONCERTI","__parentId":"gid:\/\/shopify\/Product\/6755349070004"}
{"id":"gid:\/\/shopify\/Collection\/272323739828","description":"Product variant description","title":"LIVE","__parentId":"gid:\/\/shopify\/Product\/6755349070004"}
{"id":"gid:\/\/shopify\/Collection\/273036607668","description":"Product variant description","title":"Product variant description","__parentId":"gid:\/\/shopify\/Product\/6755349070004"}
and it's obtained by the following query
mutation {
bulkOperationRunQuery(
query: """
{
items: products{
pageInfo {
hasNextPage
}
edges {
cursor
node {
id
variants(first: 10) {
edges {
node {
id
price
title
}
}
}
options(first: 5) {
id
name
position
}
title
collections(first: 8) {
edges {
node {
id
metafields(first: 5) {
edges {
node {
id
key
namespace
value
}
}
}
description
title
}
}
}
productType
images(first: 2) {
edges {
node {
src
}
}
}
}
}
}
}
"""
) {
bulkOperation {
id
status
}
userErrors {
field
message
}
}
}
So The first line it the "main" product object, the lines 2,3 and 4 are the variants, then we have the collections and then the image: the problem is that, besides the parent's id, there is no way to know which parent's property a child line refers to. If I want to build back a json from this, how can I know for example that the second line is an item of the array in products.variants?
You can use either:
the id format (gid://shopify/Product/6755349070004 is a product)
the __typename property that exists on all GraphQL objects

Appending in FlatBuffer

We already are using a flatbuffer with a similar scheme
namespace potter;
union Magic { Weapon, Wand }
table Weapon {
name:string;
power:int;
}
table Wand {
make:string;
animal:string;
}
table MagicHolder {
name:string;
age:int;
magic:Magic;
}
root_type MagicHolder;
Now, if we want to add two things.
level:string in the MagicHolder
Under the Wand a key:value type, where key:string, and value can be either string or int or float or bool
What I’m trying is
table MagicHolder {
name:string;
age:int;
level:string; /*new addition*/
magic:Magic;
}
table KeyValue {
key:string;
vint:int;
vfloat:float;
vbool:bool;
vstring:string;
vtype:string; /* since I cannot differentiate between bool set or not, trying to store type here */
}
table Wand {
make:string;
animal:string;
kv:KeyValue;
}
Is this the best approach? What would be the pros/cons.. Any other recommended ways to do this new additions more efficiently

LINQ to SQL - Multiple Anonymous Columns

I am trying to update my DBML file for a stored procedure and it is returning an error that I have never seen before and tried to find a reputable resource for solving and have been unable to at this point. The error is:
Unsupported Stored Procedure Unable to extract stored procedure
'procedurename' because its result set contains multiple anonymous
columns.
I have loaded this stored procedure into the DBML successfully numerous times. What might be causing the issue all of the sudden? Thanks!
Here is what the auto-generated data-class code used to look like (restored from a backup). I hope it helps decipher what is going on.
[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.parseXMLForK12_EachStudent")]
public ISingleResult<parseXMLForK12_EachStudentResult> parseXMLForK12_EachStudent([global::System.Data.Linq.Mapping.ParameterAttribute(DbType="NVarChar(MAX)")] string strXML, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Date")] System.Nullable<System.DateTime> datesaved, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> filepassed, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> whichproc)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), strXML, datesaved, filepassed, whichproc);
return ((ISingleResult<parseXMLForK12_EachStudentResult>)(result.ReturnValue));
}
public partial class parseXMLForK12_EachStudentResult
{
private System.Nullable<int> _Column1;
public parseXMLForK12_EachStudentResult()
{
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="", Storage="_Column1", DbType="Int")]
public System.Nullable<int> Column1
{
get
{
return this._Column1;
}
set
{
if ((this._Column1 != value))
{
this._Column1 = value;
}
}
}
}

CoolStorage can't set One-To-One relationship

The issue is that I can't understand how to make a OneToOne relation between two objects the way for the first object to have a link to the second and for the second to have a link to the first. Here's the code:
[MapTo("Model")]
public class Model : CSObject<Model, int>
{
[OneToOne(LocalKey = "ModelID", ForeignKey = "ModelID")]
public Product Product { get { return (Product)GetField ("Product"); } set { SetField ("Product", value); } }
}
[MapTo("Product")]
public class Product : CSObject<Product, int>
{
[OneToOne(LocalKey = "ProductID", ForeignKey = "ProductID")]
public Model Model { get { return (Model)GetField ("Model"); } set { SetField ("Model", value); } }
}
The thing is that when I create a product and a model and set the model's property "Product" to the created one and save it, the product's "Model" property doesn't get set and remains NULL. I've already tried making all the local and foreign keys for both Product's and Model's properties the same (e.g. "ModelID") but it didn't solve the problem. What is the right way of doing this?
I guess making one of them [OneToMany] will do the trick but will return a collection while I need a single object to be returned by a property.
Update
Here comes a simple solution one would call a crutch:
[OneToMany]
public CSList<Product> _ProductList { get { return (CSList<Product>)GetField ("_ProductList"); } set { SetField ("_ProductList", value); } }
[NotMapped]
public Product Product {
get {
CSList<Product> list = this._ProductList;
if (list.Count > 0)
return list [0];
return null;
}
set {
if (value != null) {
CSList<Product> list = this._ProductList;
list.RemoveAll ();
list.Add (value);
}
}
}
You can make both relations [ManyToOne]. That will work in your scenario.

How to Retrieve the Primary Key When Saving a New Object in Anorm

I'm using Scala Play! framework with Anorm to persist the data model to the database. I followed the example code here :
case class Bar(id: Pk[Long], name: String)
object Bar {
val simple = {
get[Pk[Long]]("id") ~
get[String]("name") map {
case id~name => Bar(id, name)
}
}
def findAll(): Seq[Bar] = {
DB.withConnection { implicit connection =>
SQL("select * from bar").as(Bar.simple *)
}
}
def create(bar: Bar): Unit = {
DB.withConnection { implicit connection =>
SQL("insert into bar(name) values ({name})").on(
'name -> bar.name
).executeUpdate()
}
}
}
Trying to expand on it, I want to retrieve the primary key just created and store it in the case class.
How can I retrieve the primary key?
Use the executeInsert method instead of executeUpdate. Noted here, the foremer method returns Option[T] where T is the type of the primary key.
You can extract the value with a match statement:
DB.withConnection { implicit connection =>
SQL(...).executeInsert()
} match {
case Some(long) => long // The Primary Key
case None => ...
}