473,722 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Identifier for an object when storing in a relational database

I'm currently in the middle of writing a persistence framework, and I
have to make a design decission. The framework will take care of all
the saving and restoring objects, and also the relationships between
them. Obviously, I need a way to identify objects uniquely. Now, one
way would be to use an autogenerated id from a autoincremental field in
the DB. But the problem is that I need to know the ID of the object as
soon as it is created, to be able to relate it to other objects. So, I
have come up with three possible solutions, none of which satisfies me.

The solutions are (in my current order of preference):
* A string ID, generated using uniqid. Main drawbacks: uniqid is a slow
function, and having a string as a primary key raises an alarm in my
head, as I tend to think (without any knowledge to back it up) that it
would be somewhat innefficient.
* An ID generated by an "ID manager", that serves IDs for each object
creation. Main drawbacks: it would have to read the "next id" from the
database for each request, and then update, as the manager can't be
shared between concurrent requests. Two database queries per object
creation. Add to that the object saving when the script ends.
* The autonumeric ID from the DB, only saving the object as soon as it
is created. Main drawbacks: extra save for the object (it will be
modified and saved later in almost every case), the object maybe ends
up being temporary (that shouldn't happen, you don't need a persistent
object for that, but who knows)

I would like to get comments on these methods, which of them you think
is best, and also if you can come up with any other (hopefully better)
methods.

Thanks in advance.

May 2 '06 #1
2 1848
na******@gmail. com wrote:
I'm currently in the middle of writing a persistence framework, and I
have to make a design decission. The framework will take care of all
the saving and restoring objects, and also the relationships between
them. Obviously, I need a way to identify objects uniquely. Now, one
way would be to use an autogenerated id from a autoincremental field in
the DB. But the problem is that I need to know the ID of the object as
soon as it is created, to be able to relate it to other objects. So, I
have come up with three possible solutions, none of which satisfies me.

The solutions are (in my current order of preference):
* A string ID, generated using uniqid. Main drawbacks: uniqid is a slow
function, and having a string as a primary key raises an alarm in my
head, as I tend to think (without any knowledge to back it up) that it
would be somewhat innefficient.
* An ID generated by an "ID manager", that serves IDs for each object
creation. Main drawbacks: it would have to read the "next id" from the
database for each request, and then update, as the manager can't be
shared between concurrent requests. Two database queries per object
creation. Add to that the object saving when the script ends.
* The autonumeric ID from the DB, only saving the object as soon as it
is created. Main drawbacks: extra save for the object (it will be
modified and saved later in almost every case), the object maybe ends
up being temporary (that shouldn't happen, you don't need a persistent
object for that, but who knows)

I would like to get comments on these methods, which of them you think
is best, and also if you can come up with any other (hopefully better)
methods.

Thanks in advance.


Hi,

IMHO your third solution is very good, mainly because you make sure that
every object has a DB-related unique identifier, which you relate to the
(serialized?) object you store in it.
Your second solution (the ID-manager) could actually rely on this if you
choose to implement it that way.

[Postgresql]
One sidenote: If you use Postgresql you could decide to use the
serial-function to retrieve a new ID WITHOUT actually storing it right
away.
(Maybe other DB's have this option too, I am not sure, but ever since I
learned PostgreSQL my interest for other DB's faided away. ;-)

Something like this:
supose you have a table like this:

CREATE TABLE tblobjects(
objectid SERIAL PRIMARY KEY,
serobject text
)

Now you can get the next serial/autonumber like this:
$SQL = "SELECT nextval('public .tblobject_obje ctid_seq'::text ) as
mynextobjectid; ";
// execute it

The serialname will differ of course, so look it up.

In that way you get the next ID with minimal CPU-cycles.
Use the required ID later to insert.
In Postgresql you CAN insert your id in a serialfield (as long as it doesn't
exists of course).
Also note that everytime you call nextval(someser ial) the number will be
increased by 1, so you are safe to use it later like this:

$SQL = "INSERT INTO tblobjects (objectid,serob ject) VALUES
($yournewidhere ,$yourserobject here)";

Just my 2 cent.

Good luck.
Your project sounds like fun. :-)

Regards,
Erwin Moller
May 2 '06 #2
na******@gmail. com wrote:
I'm currently in the middle of writing a persistence framework, and I
have to make a design decission. The framework will take care of all
the saving and restoring objects, and also the relationships between
them. Obviously, I need a way to identify objects uniquely. Now, one
way would be to use an autogenerated id from a autoincremental field in
the DB. But the problem is that I need to know the ID of the object as
soon as it is created, to be able to relate it to other objects. So, I
have come up with three possible solutions, none of which satisfies me.

The solutions are (in my current order of preference):
* A string ID, generated using uniqid. Main drawbacks: uniqid is a slow
function, and having a string as a primary key raises an alarm in my
head, as I tend to think (without any knowledge to back it up) that it
would be somewhat innefficient.
* An ID generated by an "ID manager", that serves IDs for each object
creation. Main drawbacks: it would have to read the "next id" from the
database for each request, and then update, as the manager can't be
shared between concurrent requests. Two database queries per object
creation. Add to that the object saving when the script ends.
* The autonumeric ID from the DB, only saving the object as soon as it
is created. Main drawbacks: extra save for the object (it will be
modified and saved later in almost every case), the object maybe ends
up being temporary (that shouldn't happen, you don't need a persistent
object for that, but who knows)

I would like to get comments on these methods, which of them you think
is best, and also if you can come up with any other (hopefully better)
methods.

Thanks in advance.


I would try a combination of 1 and 3. A unique string id has the
advantage of being unique universally, which makes your object less
tied to the database. I have ran into situations where data has to be
exported/imported between different instances of the application, where
a auto-increment key was inadequate.

Here's a possible scheme:

* Call uniqid() once in the script, then generate subsequent ids by
appending an incrementing number.

* An id of an object would have two parts: the unique string and the
database key. When created, the object acquires only the unique string.
* Obtain the database key only if the object is saved. Save the
database key to a hash table so that objects holding ids with just the
unique string can obtain it.

* When saving an object that links to another, check the database key
part of the id. If it's null, then save the linked-to object first and
obtain the datakey key from the aforementioned hash table.

May 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
11518
by: Rolf Hemmerling | last post by:
Hello ! Beginner's question: What ist the easiest way to store and save objects in a file generated by a C++ program, by using the "standard C++ library" and/or "Standard Template Library ( STL )" ? So I would like to generate some objects ( of different classes ) with a C++ program and would like to make it permanent / persistent, so that
34
7102
by: yensao | last post by:
Hi, I have a hard time to understand difference and similarities between Relational database model and the Object-Oriented model. Can somebody help me with this? Thank you in advance. Yensao
5
2245
by: Stephane | last post by:
Hi, I want to keep a list of my visitors in an ArrayList which I place in the application object like this: Application("Visitors") = new ArrayList(); // The list of visitors Then, each visitors update their own information in that list for every page they visit.
3
6200
by: Robert Abi Saab | last post by:
Hi everyone. I just finished a course on PostgreSQL and I found out that PostgreSQL doesn't provide any object relational features (as claimed in the official documentation), except table inheritance and very limited user defined types (I defined a UDT with 2 attributes and couldn't use it in a table, and the trainer said it must contain 1 attribute at most so that it can be used (as a column) in tables) So my question is whether...
6
2502
by: Mudcat | last post by:
Hi, I am trying to build a tool that analyzes stock data. Therefore I am going to download and store quite a vast amount of it. Just for a general number - assuming there are about 7000 listed stocks on the two major markets plus some extras, 255 tradying days a year for 20 years, that is about 36 million entries. Obviously a database is a logical choice for that. However I've never used one, nor do I know what benefits I would get...
5
1378
by: sh | last post by:
I am working on a database project, and I'm trying to think "objectively". Are there any tools that will map my "objects" to relational tables? I'd prefer a freebie, or something faily low-cost to start off. Thanks for any assistance.
10
3049
by: nayden | last post by:
I started playing with python a few weeks ago after a number of years of perl programming and I can say that my first impression is, unsurprisingly, quite positive. ;) The reason I am writing here is that I can't seem to figure out how to save/restore python objects into a relational database. The way I used to do it in perl was to 'freeze' the object before storing it into the database and 'thaw' it before restoring it. (For those not...
13
1611
by: sulyokpeti | last post by:
I have made a simple python module to handle SQL databases: https://fedorahosted.org/pySQLFace/wiki Its goal to separate relational database stuff (SQL) from algorythmic code (python). A SQLFace is a facade initialized with a configuration file (XML). It provides callable command objects for each sql query. The call substitutes template variables with its parameters, and returns the result of the query. I would like to get some opinions...
6
2911
by: Carl Banks | last post by:
I was wondering if anyone had any advice on this. This is not to study graph theory; I'm using the graph to represent a problem domain. The graphs could be arbitrarily large, and could easily have millions of nodes, and most nodes have a substantial amount of data associated with them. Obviously I don't want a whole such graph in memory at once, so libraries the just deal with in- memory graphs are out. I know I could implement this...
0
8863
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9157
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.