Save is not updating the model for the repository - spring

I am trying to update the model but facing some problems. isBookingActive variable is getting updated but transactionId is not getting updated. While debugging everything looks good. everything is getting updated in booking but not getting saved.
my code looks like this
bookingRepository.findById(bookingId).ifPresent { booking ->
booking.transactionId = bookingInfo.transactionId
booking.isBookingActive = true
bookingRepository.save(booking)
}
I have defined it like this
var transactionId: String? = DEFAULT_EMPTY_STRING,
Please help me.
I have tried various things and i feel the error is something related to declaring the variable, as the boolean value is getting updated but string is not.

Related

Breeze - Can't Create Entity when PK is an autogenerated ID

I have used breeze's CreateEntity a few times when table's PK is a user-entered value. And a few times with SQL SERVER when PK is an IDENTITY. This is my first time trying to do it when PK is autogenerated ID (actually a "sequence") in ORACLE. It isn't working.
I do check first to make sure I have fetched the Metadata then create the new, empty entity that will be filled in with values by user.
My code to createEntity (newEntity is a knockout Observable):
function createEntity(newEntity) {
newEntity(manager.createEntity(entityNames.escctransactions, {})); <<<<< this fails
return;
}
The Error:
Cannot attach an object of type (ESCC_TRANSACTIONS:... ) to an EntityManager without first setting its key or setting its entityType 'AutoGeneratedKeyType' property to something other than 'None'
I know I need to set the AutoGeneratedKeyType to "Identity" but not sure how to do it. Tried this when I'm inititalizing the metadata, but still getting same error so it's obviously not working:
var entyType = manager.metadataStore.getEntityType("ESCC_TRANSACTIONS");
entyType.setProperties({ AutoGeneratedKeyType: AutoGeneratedKeyType.Identity });
I've seen something about doing it in a constructor but I've never used a constructor in JavaScript. Also something about changing it in a config?
Using Breeze 1.6, Knockout.js 3.4, .NET 4.5.2 framework
THANKS
Figured it out myself and it's working now. The code to set AutoGeneratedKeyType is as follows:
var entityType = manager.metadataStore.getEntityType("ESCC_TRANSACTIONS");
entityType.autoGeneratedKeyType = "Identity";
Or this works:
var entityType = manager.metadataStore.getEntityType("ESCC_TRANSACTIONS");
entityType.autoGeneratedKeyType = breeze.AutoGeneratedKeyType.Identity;
And in spite of the Breeze documentation for AutoGeneratedKeyType here:
http://breeze.github.io/doc-js/api-docs/classes/AutoGeneratedKeyType.html, it's not a capital "A" in Auto, it's a small "a".

Parse-Server prevent fields from being added automatically

Right now, if I add a field to a Parse object and then save it, the new column shows up in the Parse dashboard.
For example, after running:
let media = new Parse.Object("Media");
media.set("foo", "bar");
await media.save();
I will have a new column called foo.
Is it possible to prevent this from happening?
Yes. This can be done using class-level permissions, which allow you to prevent fields being added to classes.
Parse lets you specify what operations are allowed per class. This lets you restrict the ways in which clients can access or modify your classes.
...
Add fields: Parse classes have schemas that are inferred when objects are created. While you’re developing your app, this is great, because you can add a new field to your object without having to make any changes on the backend. But once you ship your app, it’s very rare to need to add new fields to your classes automatically. You should pretty much always turn off this permission for all of your classes when you submit your app to the public.
You would have to add a beforeSave trigger for every one of your classes, keep a schema of all your keys, iterate over the request.object's keys, and see if there are any that do not belong in your schema. You can then either un-set them and call response.success(), or you can call response.error() to block the save entirely, preferably with a message indicating the offending field(s).
const approvedFields = ["field1", "field2", "field3"];
Parse.Cloud.beforeSave("MyClass", function(request, response) {
let object = request.object;
for( var key in object.dirtyKeys() ) {
if( approviedFields.indexOf(key) == -1 ) return response.error(`Error: Attempt to save invalid field: ${key});
}
response.success();
});
Edit:
Since this got a little attention, I thought I'd add that you can get the current schema of your class. From the docs: https://docs.parseplatform.org/js/guide/#schema
// create an instance to manage your class
const mySchema = new Parse.Schema('MyClass');
// gets the current schema data
mySchema.get();
It's not clear if that's async or not (you'll have to test yourself, feel free to comment update the answer once you know!)
However, once you have the schema, it has a fields property, which is an object. Check the link for what those look like.
You could validate an object by iterating over it's keys, and seeing if the schema.fields has that property:
Parse.Cloud.beforeSave('MyClass', (request, response) => {
let object = request.object;
for( var key in object.dirtyKeys() ) {
if( !schema.fields.hasOwnProperty(key) ) < Unset or return error >
}
response.success();
}
And an obligatory note for anyone just starting with Parse-Server on the latest version ,the request scheme has changed to no longer use a response object. You just return the result. So, keep that in mind.

Parse.com cloud code dirty relation

I am using Parse Cloud code hooks (beforeSave).
My object has a relation and I need to know which objects where added to this relation.
My problem is exactly the same than here.
I want to be able to do this :
var op = myObject.op('toto');
//Get all add op in relation toto
var added = op.added();
//Get all remove op in relation toto
var deleted = op.removed();
added.forEach(function(pointer) {
//Do something with pointer
//If you need value on pointer
pointer.fetch(function(objectFetched) {
//Do something with object
});
});
}
But that is not working anymore because
Result: TypeError: Object [object Object] has no method 'added'
How can I do now to know which objects was added to the relation ?
Unfortunately there is no information on this in Parse documentation. I had to poke through their source code to figure out how to do the exact thing for my Cloud Code. Anyway, you can get an Array of objectIds that are being added to a Relation like this:
var added = request.object.op("toto").relationsToAdd;
If you want to find the ones being removed, just replace relationsToAdd with relationsToRemove

MVC5 - Object reference not set to an instance of an object. while updating a record

I have done something similar in the past where it updates the table based on an id. Here is the code worked for me in the past but its returning null error.
Object reference not set to an instance of an object. System.NullReferenceException: Object reference not set to an instance of an object.
public ActionResult TestUpdate()
{
int id = 3; // this is the FK which exists on the Transactions table.
var results = db.StoreTransactions.Find(id);
if(results != null)
{
results.TransStatus = "Completed";
db.SaveChanges();
}
}
It appears that results is null but i verified that the record #3 exists. To simply the code below I hard-coded.
I just want to update the results.TransStatus to "Completed".
Can anyone help please. Thank you!
----------------- UPDATE ---------------------
I found out that Find() does not work with foreign keys(FK). I hard-coded PK and its no longer Null.
However, I'm still not able to update the table to completed. How can I update the table or the transStatus field? Is the code correct? Thanks again for your time.
I fixed it. The db.Entry(results).State = EntityState.Modified was missing. I still don't understand how or why the same code worked on my previous project.

Parse Android: update ParseObject containing an array of ParseUsers throws UserCannotBeAlteredWithoutSessionError

im working on an Android App.
I have a custom class which has relations with TWO ParseUsers and other fields. As suggested by the docs, I used an array (with key "usersArray") to store the pointers for the two ParseUsers, because I want to be able to use "include" to include the users when i query my custom class. I can create a new object and save it successfully.
//My custom parse class:
CustomObject customObject = new CustomObject();
ArrayList<ParseUser> users = new ArrayList<ParseUser>();
users.add(ParseUser.getCurrentUser());
users.add(anotherUser);
customObject.put("usersArray", users);
//I also store other variable which i would like to update later
customObject.put("otherVariable",false);
customObject.saveInBackground();
Also, i can query successfully with:
ParseQuery<CustomObject> query = CustomObject.getQuery();
query.whereEqualTo("usersArray", ParseUser.getCurrentUser());
query.whereEqualTo("usersArray", anotherUser);
query.include("usersArray");
query.findInBackground( .... );
My problem is when trying to UPDATE one of those CustomObject.
So after retrieving the CustomObject with the previous query, if I try to change the value of the "otherVariable" to true and save the object, I am getting a UserCannotBeAlteredWithoutSessionError or java.lang.IllegalArgumentException: Cannot save a ParseUser that is not authenticated exceptions.
CustomObject customObject = customObject.get(0); //From the query
customObject.put("otherVariable", true);
customObject.saveInBackground(); // EXCEPTION
I can see this is somehow related to the fact im trying to update an object which contains a pointer to a ParseUser. But im NOT modifying the user, i just want to update one of the fields of the CustomObject.
¿There is any way to solve this problem?
Maybe late but Parse users have ACL of public read and private write so you should do users.isAuthenticated() to check if its true or false.
If false then login with the user and retry. Note: you cannot edit info on two users at the same time without logging out and relogging in.
Another thing you can do is use Roles and define an admin role by using ACL which can write over all users.

Resources