I'm a relative newbie to OJB, but i'll give this one a whirl. 

See comments preceded w/ pgm:

-----Original Message-----
From: charles grohowski [mailto:[EMAIL PROTECTED]
Sent: Wednesday, April 02, 2003 3:16 PM
To: OJB Users List
Subject: Newbie OJB problem


Hi,
I recently set up OJB to work with a MySQL database and am having a problem
storing an UserRoles object I created.  Here are snippets from the
repository_user.xml and other important files.

repository_user.xml:

 <class-descriptor 
     class="ojb.Users"
     table="users"
   >
 <field-descriptor
    name="id"
    column="id"
    jdbc-type="INTEGER"
    nullable="false"
    primarykey="true"
    autoincrement="true"
    
 />
 <field-descriptor
    name="logon"
    column="logon"
    jdbc-type="VARCHAR"
    
 />

... more here, cut for brevity 

    <collection-descriptor
       name="userRoles"
       element-class-ref="edu.rutgers.rsa.ojb.UserRoles"
       auto-retrieve="true"
    >
       <inverse-foreignkey field-ref="userId"/>
       
 </collection-descriptor>
</class-descriptor>

   <class-descriptor 
     class="ojb.UserRoles"
     table="user_roles"
   >
 <field-descriptor
    name="userId"
    column="user_id"
    jdbc-type="INTEGER"
    primarykey="true"
    
 />

 <field-descriptor
    name="roleName"
    column="role_name"
    jdbc-type="VARCHAR"
    primarykey="true"
    
 />

 <field-descriptor
    name="logon"
    column="logon"
    jdbc-type="VARCHAR"
    
 />
    <reference-descriptor
       name="users"
       class-ref="edu.rutgers.rsa.ojb.Users"
       auto-retrieve="true"
       auto-delete="false"
       auto-update="false"
    >
       <foreignkey field-ref="userId"/>
 </reference-descriptor>

   </class-descriptor>

Classes: 
UserRoles.java
public class UserRoles { 
        
        private Integer userId;
        public void setUserId( Integer userId ) { this.userId = userId; }
        public Integer getUserId() { return userId; }
        
        private String logon;
        public void setLogon( String logon ) { this.logon = logon; }
        public String getLogon() {  return logon; }

        private String roleName;
        public void setRoleName( String roleName ) { this.roleName =
roleName; }
        public String getRoleName() {  return roleName; }

        private Users users;
        public void setUsers( Users users ) { this.users = users; }
        public Users getUsers() { return users; }
} 


Users.java

public class Users { 

        private Integer id;
        public void setId( Integer id ) { this.id = id; }
        public Integer getId() {  return id; }

        private String logon;
        public void setLogon( String logon ) { this.logon = logon; }
        public String getLogon() {  return logon; }

        private Vector userRoles;
        public void setUserRoles( Vector userRoles ) { this.userRoles =
userRoles; }
        public Vector getUserRoles() { return userRoles; }

// cut for brevity as well        
        
}

1st question: When I store a User object which has a Vector of User Roles
should these UserRole objects be persisted automagically? (they're not
currently and I have to store all the objects related to a user separately).
If this is possible, how is it accomplished?  

pgm: if you set auto-update=true in your mapping, (similar to auto-retrieve
flag) the relationship objects will be automagically persisted when you
persist the container (in your case, saving the User will save the UserRoles
stored in the User's collection)

2nd question: Am I mapping this relationship correctly? (It's a 1:n
relationship Users:UserRoles )

pgm: seems right to me. 

Now the real problem, when i go to store a UserRoles object, I get the
following debug code: 

[org.apache.ojb.broker.accesslayer.sql.SqlGeneratorDefaultImpl] DEBUG: SQL:
SELECT role_name,logon FROM user_roles WHERE logon = ?  AND role_name = ? 

[org.apache.ojb.broker.accesslayer.sql.SqlGeneratorDefaultImpl] DEBUG: SQL:
INSERT INTO user_roles (logon,role_name) VALUES ( ?, ? )

Why is this totally ignoring my user_id field even though it's specified as
a primary key?

pgm: Not sure. I've never looked @ the SQL that OJB generates. Are you
assigning the PK field yourself. If so, you can set auto-increment to false
in the descriptor, which I believe will ensure that the PK strategy
specified in your OJB.properties file will not be used. This might help.
Perhaps someone else can help w/ this one... 

Any help is appreciated as I am wearing a hole in my desk from banging my
head on it.  

Thanks for any and all help ( I thought I mapped things correctly according
to the tutorials ),

Chuck Grohowski

Reply via email to