Here is what i suggest you do, if this fits your requirments.
In this example Student extends Person and Person has a 1:M relationship with UserLogin. You can change it to 1:1 relation ship by makeint UserLogin.personId the PK instead of UserLogin.userLoginId. This way all people can have logins.
--------------- repository -----------------------
<!-- Person -->
<class-descriptor
class="net.office.common.model.party.OPerson"
table="PERSON"
>
<field-descriptor id="1"
name="personId"
column="PRESON_ID"
jdbc-type="INTEGER"
primarykey="true"
/>
<field-descriptor id="2"
name="firstName"
column="FIRST_NAME"
jdbc-type="VARCHAR"
/>
<field-descriptor id="3"
name="middleName"
column="MIDDLE_NAME"
jdbc-type="VARCHAR"
/>
<field-descriptor id="4"
name="lastName"
column="LAST_NAME"
jdbc-type="VARCHAR"
/>
</class-descriptor>
<!-- Student --> <class-descriptor class="net.office.common.model.party.OStudent" table="PERSON" > <field-descriptor id="1" name="personId" column="PRESON_ID" jdbc-type="INTEGER" primarykey="true" /> <field-descriptor id="2" name="iq" column="IQ" jdbc-type="INTEGER" />
<reference-descriptor
name="super"
class-ref="net.office.common.model.party.OPerson"
auto-update="true"
>
<foreignkey field-ref="personId"/>
</reference-descriptor></class-descriptor>
<!-- USER_LOGIN -->
<class-descriptor
class="net.office.common.model.security.OUserLogin"
table="USER_LOGIN"
>
<field-descriptor id="1"
name="userLoginId"
column="USER_LOGIN_ID"
jdbc-type="VARCHAR"
primarykey="true"
/>
<field-descriptor id="2"
name="password"
column="PASSWORD"
jdbc-type="VARCHAR"
/>
<field-descriptor id="3"
name="personId"
column="PERSON_ID"
jdbc-type="INTEGER"
/> <reference-descriptor
name="person"
class-ref="net.office.common.model.party.OPerson"
auto-retrieve="true"
>
<foreignkey field-ref="personId"/>
</reference-descriptor></class-descriptor>
------------ end repository -------------------
After creating your Person or Student this is how you can store your UserLogin. You will need to query for the person first, you must set both userLogin.person and userLogin.personId or person id will be null.
--------------- how to store user -----------------
// get your person if youhave not already
OPerson person = new OPerson();
person.setPersonId("001");
person = (OPerson) getPersistenceBroker().getObjectByQuery(new QueryByCriteria(person));
// now create the login OUserLogin userLogin = new OUserLogin(); userLogin.setUserLoginId(userLoginId);
// make sure user does not exist, it looks better than an SQL exception
OUserLogin checkUserLogin = (OUserLogin)getPersistenceBroker().getObjectByQuery(new QueryByCriteria(userLogin));
if(checkUserLogin != null)
{
throw new OCommandException(
"User login '"
+ userLoginId
+ "' already exist.");
}
userLogin.setPassword(password); userLogin.setPerson(person); userLogin.setPersonId(person.getPersonId()); getAPersistenceBroker().store(userLogin);
------------ end how to store user ----------------
-chris worley
Hi Gerhard,
I'm not saying the B1 and B2 instances should extend the same instance of A, but that they should be materialized from the same A record in the database. B1 and B2 would have to be different objects obviously. I think this could be quite possible in OJB.
But let me ask the question a different way - suppose I have an instance of A that already exists. Now I want to create a B1 instance that is associated with the already existing A instance. So I don't want a new A record to be created, only new B1 record that "extends" the pre-existing A record. Is that possible?
Thank you for your input.
Matt
Gerhard Grosse wrote:
Hi Matt,
Excuse me to jump in here, but I believe what you have in mind is not possible in Java and even less with OJB. If your B1 and B2 instance should 'contain' the same A instance as base class, they are identical objects, which would require a language that supports multiple inheritance (and virtual base classes). You could do that in in C++ but not in Java. What you can do in Java (and OJB) is declare an interface I_A implemented by A, B1 and B2. I_A just declares all public methods of A and in B1 and B2 you implement this interface by delegation. That is in B1 and B2 you have a reference to a A object and all I_A methods on B1 and B2 are just routed to this reference. That way distinct instances of B1 and B2 can easily share a common A reference.
HTH, Gerhard
On Tue, 21 Oct 2003 00:27:02 -0400, Matt Mastrangelo <[EMAIL PROTECTED]> wrote:
Chris,
Just to clarify -- are you saying it's not possible to create the User instance without also creating a new Person instance? Maybe my example was not too clear. Let me re-explain:
I have a base class A with two subclasses, B1 and B2. I have already created an instance of B1 and stored it to the database, which means that an A record and a B1 record were inserted. I now want to create an instance of B2 that extends from the same A record as B1. Is this possible?
Thanks again.
Matt
[EMAIL PROTECTED] wrote:
Matt,
I believe since the Person and Student object exist it will set there
values to null except the personId. Because in the User instance you are
storing allthe other fields are null (if you set them to null when you
declare them) except the loginName. So, ojb will know that person and
student 001 exist and will do and update instead of an insert.
You should create them on the first store. Or do not have user extend
Student, instead have a 1:1 relationship. Depends on your requirments i
guess.
-chris worley
I'm trying to figure out how to store a new persisted object that extends a pre-existing object.
For example, I have 3 classes: Person, Student, and User. Student and
User extend Person. A Student object already exists, and I now want to
create a User object that inherits from the same Person as the Student
object. Is this possible? Here's a code example:
User user = new User(); user.setLoginName("matt"); user.setPersonOid("001"); // A pre-existing person
broker.beginTransaction(); broker.store(user); broker.commitTransaction();
The result of the above code is a NEW empty person object being created
with a newly generated OID (via a sequence). How can I associate the
new User with a pre-existing Person?
Thanks again.
Matt
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
