How to delete an object store on Persisten Object on Blackberry? - blackberry

I have a class with name DataContext that use Hashtable to save data to Persistent Object.
Example I save two object:
DataContext data = new DataContext();
data.set("object1", EditField1.getText();
data.set("object2", EditField2.getText();
data.commit();
I only want to delete data of "object1" ( not delete data of "object2" ), so i can not use data.clear();
I make like this code:
if(data.get("object1")!=null){
data.set("object1",null);
data.commit();
}
But I received error " App Error 104 NullPointerExceptions ".
How I can to delete data of "object1" ?
Please help me.
Binh - VietNam

If you want to remove an element from a Hashtable then use the remove() method of your hashtable object.

Related

xamarin android Intent.getstringextra multiple variables

i created 4 variables namely, "id", "name", "address", "age"
why does the value duplicates whenever i try intent.putextra
Context context = view.Context;
Intent intent = new Intent(context, typeof(activity4));
intent.PutExtra(activity4.id, joblist[position].id);
intent.PutExtra(activity4.name, joblist[position].name);
intent.PutExtra(activity4.address, joblist[position].address);
intent.PutExtra(activity4.age, joblist[position].age);
now the problem is when I do this.
string userId= Intent.GetStringExtra(id);
string userName= Intent.GetStringExtra(name);
string userAddress= Intent.GetStringExtra(address);
string userAge= Intent.GetStringExtra(age);
when I put those strings in a textview, all four textviews would show the value for "age". as if all the data that was passed is only age. can anyone answer this? the output is like this
id= 12
name= 12
address= 12
age= 12
Intent.Extras don't work like that when you use the typeOf() keyword
typeOf is a C# keyword that is used when you have the name of the class. It is calculated at compile time and thus cannot be used on an instance, which is created at runtime. GetType is a method of the object class that can be used on an instance
Since it does not initialize like that your variables that you are assigning here are currently all null so the intent carries all the data in correspondence to null, Since the first value you enter with null is id you always get id
I would suggest you save the strings in either resource strings and reuse it or do something like this :
First Activity:
Intent intent = new Intent(_context, typeOf(SecondActivity));
intent.PutExtra("Variable Name",string_to_be_sent);
startActivity(intent);
Second Activity:
//Receiving data inside onCreate() method of Second Activity
String value =Intent.GetStringExtra("Variable Name");

Hunting Down Save Result Bug

I was looking for help trying to hunt down a bug with hasChanges still true after returning from a successful saveChanges. I am saving an entity and nested entity together and when it returns my saveResult is showing as an array with the first object showing the parent entity and the second object is a $ref. However one of my entityGroups is saying it still has changes so it acts like it needs to save again.
One interesting thing is that I saved a different set with a different nested entity and it returned with the nested entity as the first object in the saveResult array and my parent entity was the $ref. I did my best to make sure that both nested entity models looked the same. If anyone has any ideas or suggestions I will gladly give them a try and hopefully solve this issue.
{
"$id":"1",
"$type":"Breeze.ContextProvider.SaveResult, Breeze.ContextProvider",
"Entities":[
{
"$id":"2",
"$type":"CoreDBCodeFirst.Person, CoreDBCodeFirst",
"LastName":"Washington",
"FirstName":"George",
"MiddleName":null,
"SocialSecurity":null,
"DateOfBirth":"1974-12-10T06:00:00.000Z",
"Gender":"M",
"Language":"en-US",
"IdNumber":null,
"Eligibility":true,
"Active":true,
"PersonAddresses":[
{
"$id":"3",
"$type":"CoreDBCodeFirst.PersonAddress, CoreDBCodeFirst",
"Address1":"13000 S Dakota 244",
"Address2":null,
"Zip":"57751",
"City":"Keystone",
"State":"SD",
"IsPrimary":false,
"Active":true,
"PersonId":20118,
"Person":{
"$ref":"2"
},
"id":20108
}
],
"FullName":"Washington, George",
"Email":null,
"id":20118
},
{
"$ref":"3"
}
],
"KeyMappings":[
{
"$id":"4",
"$type":"Breeze.ContextProvider.KeyMapping, Breeze.ContextProvider",
"EntityTypeName":"CoreDBCodeFirst.Person",
"TempValue":-1,
"RealValue":20118
},
{
"$id":"5",
"$type":"Breeze.ContextProvider.KeyMapping, Breeze.ContextProvider",
"EntityTypeName":"CoreDBCodeFirst.PersonAddress",
"TempValue":-2,
"RealValue":20108
}
],
"Errors":null
}
Anytime you see a return value of a function in place of an entity, it's a good bet that there is something wrong with your metadata; usually having to do with the definition of one of your navigation properties. Can you post the metadata for Person and PersonAddresses?
Need more details.
What is the server tech?
What was your saveChanges call? Did you specify entities to save or ask to save the entire cache?
In a debugging session, capture the list of entities you're saving (manager.getChanges()) and compare them to the saveResult.entities in the response; are any entities missing?
Is anything remarkable about the unsaved entities after save?

How do I query complex data and return the data in the correct objects in Zend Framework 2?

I'm just starting out with ZF2 and I've run into a stumbling block and I cant find any useful advice on the internet.
Setting up retrieval of data from a single table and injecting it directly into a specific model is easy, for example, pulling data from a single row from 'school' table and injecting to a 'school' model.
However, I have some slightly more complex data and can't figure out how to return the data in the form of the correct model. For example, pulling multiple addresses from a school address table with a join on the school table.
I've got the following method in my AddressTable object...
public function fetchAllSchoolAddresses($school_id)
{
$stmt = $this->adapter->createStatement();
$stmt->prepare("CALL get_school_addresses(3)");
$stmt->getResource()->bindParam(3, $school_id, \PDO::PARAM_INT, 3);
$resultSet = $stmt->execute();
$addresses = new \ArrayObject();
if(!empty($resultSet)){
foreach ($resultSet as $result) {
$addresses->append($result);
}
}
return $addresses;
}
This quite nicely returns an array of addresses data but I want these results to be returned as Address objects. I'm not sure how to do this?
ZF2 comes with some standard Hydrators, which you can extend / modify if you wish.
http://framework.zend.com/manual/2.0/en/modules/zend.stdlib.hydrator.html
You could create a Hydrator for your School Object, and a Hydrator for your Address object.
The hydrators will build the object for you given the array data from the database for example
For example, you would Hydrate your School Object, and then find all addresses (like above) and use another hydrator to hydrate those. You would then add those to the School object to get your object graph as needed
$school->addAddress($address); // etc
Have a look here to see an example of using Hydrators and Hydrating ResultSets:
http://blog.evan.pro/zf2-tablegateway-hydration
http://framework.zend.com/manual/2.0/en/modules/zend.db.result-set.html
for exampple you could do something like this:
// How ever you want to get your database result do it here..
// this is where you get all addresses for your School
$stmt = $driver->createStatement($sql);
$stmt->prepare($parameters);
$result = $stmt->execute();
$resultSet = new HydratingResultSet(new ReflectionHydrator, new SchoolAddress);
$resultSet->initialize($result);
foreach ($resultSet as $address) {
$school->addAddress($address);
echo $address->getCity() . ' ' . $user->getPostcode() . PHP_EOL;
}
You would have a resultset (collection) of Addresses to add to your School.
That's code is just a very rough example hacked from the code in the docs to give an idea of what you do
How about this?
if(!empty($resultSet)){
foreach ($resultSet as $result) {
$address = new Address();
$address->exchangeArray($result);
$addresses->append($address);
}
}

Returning Updated Results from DBSet.SqlQuery

I want to use the following method to flag people in the Person table so that they can be processed. These people must be flagged as "In Process" so that other threads do not operate on the same rows.
In SQL Management Studio the query works as expected. When I call the method in my application I receive the row for the person but with the old status.
Status is one of many navigation properties off of Person and when this query returns it is the only property returned as a proxy object.
// This is how I'm calling it (obvious, I know)
var result = PersonLogic.GetPeopleWaitingInLine(100);
// And Here is my method.
public IList<Person> GetPeopleWaitingInLine(int count)
{
const string query =
#"UPDATE top(#count) PERSON
SET PERSON_STATUS_ID = #inProcessStatusId
OUTPUT INSERTED.PERSON_ID,
INSERTED.STATUS_ID
FROM PERSON
WHERE PERSON_STATUS_ID = #queuedStatusId";
var queuedStatusId = StatusLogic.GetStatus("Queued").Id;
var inProcessStatusId = StatusLogic.GetStatus("In Process").Id;
return Context.People.SqlQuery(query,
new SqlParameter("count", count),
new SqlParameter("queuedStateId", queuedStateId),
new SqlParameter("inProcessStateId", inProcessStateId)
}
// update | if I refresh the result set then I get the correct results
// but I'm not sure about this solution since it will require 2 DB calls
Context.ObjectContext().Refresh(RefreshMode.StoreWins, results);
I know it is an old question but this could help somebody.
It seems you are using a global Context for your query, EF is designed to retain cache info, if you allways need fresh data must use a fresh context to retrieve it. as this:
using (var tmpContext = new Contex())
{
// your query here
}
This create the context and recycle it. This means no cache was stored and next time it gets fresh data from database not from cache.

db4o issue with graph of objects

I am a new to db4o. I have a big problem with persistance of a graph of objects. I am trying to migrate from old persistance component to new, using db4o.
Before I peristed all objects its graph looked like below (Take a look at Zrodlo.Metadane.abstrakt string field with focused value) [its view from eclipse debuger] with a code:
ObjectContainer db=Db4o.openFile(DB_FILE);
try {
db.store(encja);
db.commit();
} finally{
db.close();
}
After that, I tried to read it with a code:
ObjectContainer db=Db4o.openFile((DB_FILE));
try{
Query q = db.query();
q.constrain(EncjaDanych.class);
ObjectSet<Object> objectSet = q.execute();
logger.debug("objectSet.size" + objectSet.size());
EncjaDanych encja = (EncjaDanych) objectSet.get(0);
logger.debug("ENCJA" + encja.toString());
return encja;
}finally{
db.close();
}
and I got it (picture below) - string field "abstrakt" is null now !!!
I take a look at it using ObjectManager (picture below) and abstrakt field has not-null value there!!! The same value, that on the 1st picture.
Please help me :) It is my second day with db4o. Thanks in advance!
I am attaching some code with structure of persisted class:
public class EncjaDanych{
Map mapaIdRepo = new HashMap();
public Map mapaNazwaRepo = new HashMap(); }
!!!!!!!!UPDATED:
When I tried to read only Metadane object (there was only one such a object), it is all right - it's string field abstrakt could be read correctly.
try{
Query q = db.query();
q.constrain(Metadane.class);
ObjectSet<Object> objectSet = q.execute();
logger.error("objectSet.size" + objectSet.size());
Metadane meta = (Metadane) objectSet.get(0);
logger.debu("Metadane" + meta.toString());
return meta;
}finally{
db.close();
}
This is a common db4o FAQ, an issue with what db4o calls "activation". db4o won't instantiate the entire graph you stored when you load an object from an ObjectContainer. By default, objects are instantiated to depth 5. You can change the default configuration to a higher value, but that is not recommended since it will slow down object loading in principle because the depth will be used everywhere you load an object with a query.
Two approaches are possible to solve your issue:
(1) You can activate an object to a desired depth by hand when you need a specific depth.
db.activate(encja, 10) // 10 is arbitrary
(2) You can work with Transparent Activation. There are multiple chapters on how to use Transparent Activation (TA) in the db4o tutorial and in the reference documentation.
You're not setting a filter in your query so you're reading the first object. Are you sure you didn't have a previous object in the database?

Resources