niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Databases » Berkeley DB
Subclasses with Data Persistence Layer
Post new topic   Reply to topic Page 1 of 1 [5 Posts] View previous topic :: View next topic
Author Message
shankar_ramaswamy@yahoo.c
*nix forums beginner


Joined: 10 Jul 2006
Posts: 3

PostPosted: Thu Jul 13, 2006 12:29 pm    Post subject: Re: Subclasses with Data Persistence Layer Reply with quote

Mark,

This proposal works perfect. Looking forward to seeing it implemented
in the next release of JE :-)

Regards,

shankar

mark.hayes@oracle.com wrote:
Quote:
shankar wrote:
Mark,

Thanks much for tracking that down so quickly - I verified it works.
BTW, I
love what you guys have done with the DPL - very cool !! I assume the
fix
will be in the next rev of JE ...

You're welcome. Hey, thanks for the positive feedback, it's great to
hear! Yes, the fix will be in the next rev of JE.

One nit - when I build a secondary index for my Manager class, I am
still
forced to declare them as Employees because the secondary index builds
off the primary index. This requires me to do casts to Manager on the
results.
Any chance you can declare the generic parameters for the secondary
index
as <SK, PK, "T extends Entity"> ? This will allow one to avoid the
casts and
I assume also allow your runtime to better check the validity of the
SecondaryKey
field name I pass in ...

It turns out that the Class of the entity subclass (Manager.class)
needs to be passed as a parameter when getting the secondary index.
This is because, prior to this point, that class may not be known to
the DPL. (There is no practical way in Java to get all the subclasses
of a given class.) In order to find the key field (department) the DPL
must know about the Manager class somehow. This is why calling
registerClass is a workaround.

By adding a new method to get the secondary index with an extra
parameter for the subclass, we are also able to return an index with
that subclass as the entity class -- the E type parameter. I believe
this is what you're asking for above.

I've called this method getSecondarySubclassIndex and it is used as
follows:

SecondaryIndex<String, EmployeeID, Manager> managersByDept =
store.getSecondarySubclass(employeesById, Manager.class,
String.class,
"department");

This seems to solve both issues. Do you agree?

Mark
Back to top
mark@sleepycat.com
*nix forums beginner


Joined: 22 Aug 2005
Posts: 18

PostPosted: Wed Jul 12, 2006 11:02 pm    Post subject: Re: Subclasses with Data Persistence Layer Reply with quote

shankar wrote:
Quote:
Mark,

Thanks much for tracking that down so quickly - I verified it works.
BTW, I
love what you guys have done with the DPL - very cool !! I assume the
fix
will be in the next rev of JE ...

You're welcome. Hey, thanks for the positive feedback, it's great to
hear! Yes, the fix will be in the next rev of JE.

Quote:
One nit - when I build a secondary index for my Manager class, I am
still
forced to declare them as Employees because the secondary index builds
off the primary index. This requires me to do casts to Manager on the
results.
Any chance you can declare the generic parameters for the secondary
index
as <SK, PK, "T extends Entity"> ? This will allow one to avoid the
casts and
I assume also allow your runtime to better check the validity of the
SecondaryKey
field name I pass in ...

It turns out that the Class of the entity subclass (Manager.class)
needs to be passed as a parameter when getting the secondary index.
This is because, prior to this point, that class may not be known to
the DPL. (There is no practical way in Java to get all the subclasses
of a given class.) In order to find the key field (department) the DPL
must know about the Manager class somehow. This is why calling
registerClass is a workaround.

By adding a new method to get the secondary index with an extra
parameter for the subclass, we are also able to return an index with
that subclass as the entity class -- the E type parameter. I believe
this is what you're asking for above.

I've called this method getSecondarySubclassIndex and it is used as
follows:

SecondaryIndex<String, EmployeeID, Manager> managersByDept =
store.getSecondarySubclass(employeesById, Manager.class,
String.class,
"department");

This seems to solve both issues. Do you agree?

Mark
Back to top
shankar_ramaswamy@yahoo.c
*nix forums beginner


Joined: 10 Jul 2006
Posts: 3

PostPosted: Tue Jul 11, 2006 5:17 pm    Post subject: Re: Subclasses with Data Persistence Layer Reply with quote

Mark,

Thanks much for tracking that down so quickly - I verified it works.
BTW, I
love what you guys have done with the DPL - very cool !! I assume the
fix
will be in the next rev of JE ...

One nit - when I build a secondary index for my Manager class, I am
still
forced to declare them as Employees because the secondary index builds
off the primary index. This requires me to do casts to Manager on the
results.
Any chance you can declare the generic parameters for the secondary
index
as <SK, PK, "T extends Entity"> ? This will allow one to avoid the
casts and
I assume also allow your runtime to better check the validity of the
SecondaryKey
field name I pass in ...

Regards,

shankar

mark.hayes@oracle.com wrote:
Quote:
Hello Shankar,

It looks like you have run into a bug in the beta release of the
Persistence API -- we apologize for that. We will work on a fix and
make it available to you, as well as including the fix in the next
release of Berkeley DB Java Edition. I will post back to this forum
when the fix is available.

In the mean time, there is a work around: You can explicitly register
the Manager class and any other subclass containing a secondary key.
To do this, you have to explicitly create the model object as shown
below, rather than allowing an AnnotationModel to be created by
default:

/* Create the model explicitly and register the subclasses */
EntityModel model = new AnnotationModel();
model.registerClass(Manager.class);

/* Use the model to configure the store */
StoreConfig storeConfig = new StoreConfig();
storeConfig.setAllowCreate(true);
storeConfig.setModel(model);
EntityStore store = new EntityStore(..., storeConfig);

Please let me know if this does not work for you.

Mark
Back to top
mark@sleepycat.com
*nix forums beginner


Joined: 22 Aug 2005
Posts: 18

PostPosted: Tue Jul 11, 2006 2:35 am    Post subject: Re: Subclasses with Data Persistence Layer Reply with quote

Hello Shankar,

It looks like you have run into a bug in the beta release of the
Persistence API -- we apologize for that. We will work on a fix and
make it available to you, as well as including the fix in the next
release of Berkeley DB Java Edition. I will post back to this forum
when the fix is available.

In the mean time, there is a work around: You can explicitly register
the Manager class and any other subclass containing a secondary key.
To do this, you have to explicitly create the model object as shown
below, rather than allowing an AnnotationModel to be created by
default:

/* Create the model explicitly and register the subclasses */
EntityModel model = new AnnotationModel();
model.registerClass(Manager.class);

/* Use the model to configure the store */
StoreConfig storeConfig = new StoreConfig();
storeConfig.setAllowCreate(true);
storeConfig.setModel(model);
EntityStore store = new EntityStore(..., storeConfig);

Please let me know if this does not work for you.

Mark
Back to top
shankar_ramaswamy@yahoo.c
*nix forums beginner


Joined: 10 Jul 2006
Posts: 3

PostPosted: Mon Jul 10, 2006 2:15 am    Post subject: Subclasses with Data Persistence Layer Reply with quote

I am new to Berkeley DB - looked at the new Data Persistence Layer and
seemed really simple so am trying to use it. In general, I like it a
lot and everything seems to work fine. The one area where instructions
and docs seem weak is in dealing with class hierarchies. In particular,
I am trying to implement Polymorphism within a single Entity and
running into trouble. Docs seem to indicate this can be done, but am
not quite able to do it:

(http://www.sleepycat.com/jedocs/java/com/sleepycat/persist/model/Entity.html)

I have two classes - Employee and Manager which extends Employee.
Employee is declared as an Entity and Manager is declared to be
persistent. Manager declares a SecondaryKey that I want to use to
access all managers. Relevant code snippets below:

@Entity
public class Employee {

@PrimaryKey
EmployeeID id;

....

}

@Persistent
public class Manager extends Employee {

@SecondaryKey(relate=ONE_TO_ONE)
String department;

....

}

Now, I want to create a secondary index to look for managers and am not
able to do this. Here is what I tried:

EntityStore store = ...
PrimaryKey<EmployeeID, Employee> employeesById =
store.getPrimaryKey(EmployeeID.class,
Employee.class);
SecondaryKey<String, EmployeeID, Employee> managersByDept =
store.getSecondaryKey(employeesById,
String.class, "department");

The second statement throws an exception saying department was not
recognized as a SecondaryKey in the entity Employee (I can see that
would happen because department is not a field in Employee). However, I
cannot seem to specify Manager as the Entity because I get an exception
saying Manager is not an entity. Again, I can see that would happen
because only Employee is defined as an Entity. So I'm stuck not knowing
how to get this secondary key working - maybe I'm missing something
obvious - any help would be much appreciated.

Regards,

shankar
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [5 Posts] View previous topic :: View next topic
The time now is Sat Nov 22, 2008 8:27 am | All times are GMT
navigation Forum index » Databases » Berkeley DB
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Bug#379103: ITP: complearn-gui -- 3D drag-and-drop interf... Rudi Cilibrasi devel 0 Fri Jul 21, 2006 11:00 am
No new posts Bug#379087: ITP: libcomplearn -- data-compression based i... Rudi Cilibrasi devel 0 Fri Jul 21, 2006 7:40 am
No new posts How do I render JPEG Data stored in char* buffer? On the Sparrow C++ 2 Thu Jul 20, 2006 8:44 pm
No new posts ANNOUNCE: Data-centric PHP framework (GPL) Kenneth Downs PHP 0 Thu Jul 20, 2006 6:11 pm
No new posts XML-RPC calls with real UTF8 data failed Erik Wasser Perl 0 Thu Jul 20, 2006 3:40 pm

Remortgages | Society 2007 | Cheap Loan | Loans | Loans
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2257s ][ Queries: 20 (0.1356s) ][ GZIP on - Debug on ]