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?
2nd question: Am I mapping this relationship correctly? (It's a 1:n relationship
Users:UserRoles )
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?
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