Hibernate version: 2.1.4
Mapping documents:
Here is a sample (User) class:
Code:
package com.pcpower.amss.security.objects;
import java.util.Date;
import java.util.Set;
import com.pcpower.amss.PersistentObject;
/**
* @hibernate.joined-subclass table="Users"
* @hibernate.joined-subclass-key column="id"
*/
public class User extends PersistentObject{
private Long id;
private String username;
private String password;
private String name;
private transient String firstName;
private transient String lastName;
private Set roles;
private String govId;
private Date birthDate;
private Department department;
private String telephone;
private String cellular;
private String email;
private Client client;
public User(){}
/**
* @hibernate.id generator-class="foreign" type="java.lang.Long" column="id"
* @return Returns the id.
*/
public Long getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(Long id) {
this.id = id;
}
/**
* @hibernate.property column="name" type="java.lang.String" not-null="true" length="75"
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
/**
* @return Returns the username.
*/
public String getUsername() {
return username;
}
/**
* @hibernate.property column="username" type="java.lang.String" not-null="true" length="16"
* @param username The username to set.
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @hibernate.property column="password" type="java.lang.String" not-null="true" length="16"
* @return Returns the password.
*/
public String getPassword() {
return password;
}
/**
* @param password The password to set.
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @hibernate.property column="birthDate" type="java.util.Date" not-null="false" length="10"
* @return Returns the birthDate.
*/
public Date getBirthDate() {
return birthDate;
}
/**
* @param birthDate The birthDate to set.
*/
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
/**
* @hibernate.property column="cellular" type="java.lang.String" not-null="false" length="21"
* @return Returns the cellular.
*/
public String getCellular() {
return cellular;
}
/**
* @param cellular The cellular to set.
*/
public void setCellular(String cellular) {
this.cellular = cellular;
}
/**
* @hibernate.many-to-one class="com.pcpower.amss.security.objects.Client" not-null="true" column="client" cascade="none" lazy="true"
* @return Returns the client.
*/
public Client getClient() {
return client;
}
/**
* @param client The client to set.
*/
public void setClient(Client client) {
this.client = client;
}
/**
* @hibernate.many-to-one class="com.pcpower.amss.security.objects.Department" not-null="false" column="department" cascade="none" lazy="true"
* @return Returns the department.
*/
public Department getDepartment() {
return department;
}
/**
* @param department The department to set.
*/
public void setDepartment(Department department) {
this.department = department;
}
/**
* @hibernate.property column="email" type="java.lang.String" not-null="false" length="75"
* @return Returns the email.
*/
public String getEmail() {
return email;
}
/**
* @param email The email to set.
*/
public void setEmail(String email) {
this.email = email;
}
//@hibernate.property column="firstName" type="java.lang.String" not-null="true"
/**
* @return Returns the firstName.
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName The firstName to set.
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @hibernate.property column="govId" type="java.lang.String" not-null="true" length="50"
* @return Returns the govId
*/
public String getGovId() {
return govId;
}
/**
* @param govId The govId to set.
*/
public void setGovId(String govId) {
this.govId = govId;
}
//* @hibernate.property column="lastName" type="java.lang.String" not-null="true"
/*
* @return Returns the lastName.
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName The lastName to set.
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @hibernate.set table="UserRoles" lazy="false" cascade="none" readonly="true"
* @hibernate.collection-key column="user_id"
* @hibernate.collection-many-to-many class="com.pcpower.amss.security.objects.Role" column="role_id"
* @return Returns the role.
*/
public Set getRoles() {
return roles;
}
/**
* @param role The role to set.
*/
public void setRoles(Set roles) {
this.roles = roles;
}
/**
* @hibernate.property column="telephone" type="java.lang.String" not-null="false" length="21"
* @return Returns the telephone.
*/
public String getTelephone() {
return telephone;
}
/**
* @param telephone The telephone to set.
*/
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getFullName() { return this.firstName + " " + this.lastName; }
public String getInitials() { return getFirstInitial() + " " + getLastInitial(); }
public String getFirstInitial() {
if (firstName.length()>1){
return this.firstName.charAt(0) + ".";
}else
return String.valueOf(this.firstName.charAt(0));
}
public String getLastInitial() { if (firstName.length()>1){
return this.lastName.charAt(0) + ".";
}else
return String.valueOf(this.lastName.charAt(0));
}
public String toString() {
return "[User " + id + "] " + username + " - created on: " + createdDate;
}
public boolean equals(Object other){
if (this==other) return true;
if( !(other instanceof User)) return false;
final User that = (User) other;
if ( !this.getUsername().equals(that.getUsername())) return false;
if ( !this.getPassword().equals(that.getPassword())) return false;
return true;
}
public int hashCode(){
int result=14;
result = 29 * result + getUsername().hashCode();
result = 29 * result + getPassword().hashCode();
return result;
}
}
[b]Name and version of the database you are using:MS SQL Server 2000
The Xdoclet code embedded in the Java class above creates the HBM. All my Persistent Classes extend "PersistentObject" to get some generic data about persistence (date created, modified, etc) so i will not show the whole .hbm file (there is only one in the system).
The interesting part is here:
Code:
/**
* @hibernate.set table="UserRoles" lazy="false" cascade="none" readonly="true"
* @hibernate.collection-key column="user_id"
* @hibernate.collection-many-to-many class="com.pcpower.amss.security.objects.Role" column="role_id"
* @return Returns the role.
*/
public Set getRoles() {
return roles;
}
In this many-to-many association between the User and the Role, I need to programatically access the associated type (in this case, the "com.pcpower.amss.security.objects.Role")
I have tried finding a path through the Configuration Object (and the PersistentClass object), but have had no luck so far. Any tips!?
Also, any comments about the idea of having a single hbm, with many joined subclasses are accepted! For example, is this a performance nightmare?
Thanks,
~karokain