[odb-users] Exception : "object not persistent" when trying to load object which does not exist in database

Florent GRATTA gratta at visuol.com
Thu Nov 12 10:27:08 EST 2015


Hi,

Thank's a lot for your answer. I apologize because I have not pay 
attention on the details in "3.9 Loading Persistent Objects".

I have also the "Object not persistent" exception when I try to load an 
object that is in database but with a null value in relation column 
(null value is allowed in db, but odn thourght the exception)

Please see the code below

std::shared_ptr<std::vector<std::shared_ptr<ControlledCar>>> 
ODBCarIdenticationServiceImpl::listControlledCarsByStatus(unsigned char 
status)
{
	typedef odb::query<ControlledCar> query;
	typedef odb::result<ControlledCar> result;

	std::shared_ptr<std::vector<std::shared_ptr<ControlledCar>>> 
listControlledCars (new std::vector<std::shared_ptr<ControlledCar>>());
	try
	{
		transaction t (m_db->begin ());
		result r (m_db->query<ControlledCar> (query::status <= status && 
query::lockedByUser == ""));

		for (result::iterator i (r.begin ()); i != r.end (); ++i)
		{
			std::shared_ptr<ControlledCar> p (i.load());
			listControlledCars->push_back(p);
		}
		t.commit ();
	}
	catch (const odb::exception& e)
	{
		m_logger->logError("listControlledCarsByStatus " + std::string(e.what 
()));
		//return nullptr;
	}
	return listControlledCars;
}

Le 12.11.2015 16:19, Boris Kolpackov a écrit :
> Hi Florent,
> 
> Florent GRATTA <gratta at visuol.com> writes:
> 
>> How can I can avoid avoing an exception?
>> 
>> std::shared_ptr<Mission> _mission (m_db->load<Mission> (scanexCarId));
> 
> Use find() instead of load(). BTW, this is all discussed in the manual.
> 
> Boris



More information about the odb-users mailing list