-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Programatic Access to Associated Class Type
PostPosted: Mon Jan 03, 2005 1:40 pm 
Newbie

Joined: Mon Aug 09, 2004 8:27 pm
Posts: 15
Location: Brazil
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


Top
 Profile  
 
 Post subject: BasicCollectionPersister
PostPosted: Tue Jan 04, 2005 10:43 am 
Newbie

Joined: Mon Aug 09, 2004 8:27 pm
Posts: 15
Location: Brazil
ok, from the BasicCollectionPersister I found that I can access ElementPersister, then the .getMappedClass() method brings me the class I was looking for.

But... how to get to the BasicCollectionPersister?? I found a way through the SessionFactoryImpl, but don't know an easy way to get a handle on that object either....

If anyone can tell me how to easily get a handle on either of these objects, that would solve my problem. Meanwhile, I'll continue my tests/learning on the Hibernate APIs.

Thanks,
~karokain


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 04, 2005 11:00 am 
Newbie

Joined: Mon Aug 09, 2004 8:27 pm
Posts: 15
Location: Brazil
ok, I tried to get a handle on the SessionFactoryImpl object like this:

Code:
SessionFactoryImpl sessionFactoryImpl = new SessionFactoryImpl(configuration, new Settings());


but received this error:
Code:
(impl.SessionFactoryImpl             119 ) building session factory
   java.lang.NullPointerException
   at net.sf.hibernate.persister.AbstractEntityPersister.<init>(AbstractEntityPersister.java:632)
   at net.sf.hibernate.persister.NormalizedEntityPersister.<init>(NormalizedEntityPersister.java:719)
   at net.sf.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:45)
   at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:137)
   at com.pcpower.amss.administration.test.Teste.main(Teste.java:169)


Is this a problem in my mappings files, my hibernate.cfg.xml, or in the lack of Settings() ? my guess is its the Settings()... but then my question returns to being "how do I gat a handle on the SessionFactoryImpl object?"


thanks,
~karokain


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 04, 2005 11:03 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You know, everyone expects that you are able to figure out a problem like this when you poke your finger into the internals of a software :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 04, 2005 11:10 am 
Newbie

Joined: Mon Aug 09, 2004 8:27 pm
Posts: 15
Location: Brazil
it's a shame the .buildSettings() method is protected :(

coming at this from a different angle, though, how do I utilize the role in getCollectionMapping(String role) ?

what do roles have to do with collections?

In the end, my goal here is pretty simple, i just need the type of elements contained within the collection... in fact this should be easy since we mention the type in the xml mapping! But, I am having a lot of difficulty finding this informatino programatically!!!

again, the type mentioned here in the mapping:
(example is User <--> Role)

Code:
    * @hibernate.set table="UserRoles" lazy="false" cascade="none" readonly="true"
    * @hibernate.collection-key column="user_id"
     * @hibernate.collection-many-to-many [i][b]class="com.pcpower.amss.security.objects.Role"[/b][/i] column="role_id"
     *
    * @return Returns the role.


please put me out of my misery!! hahaha

~karokain


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 04, 2005 12:34 pm 
Newbie

Joined: Mon Aug 09, 2004 8:27 pm
Posts: 15
Location: Brazil
finally! ok, here's the (way over-complicated) solution:

Code:
SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl)configuration.buildSessionFactory();
          BasicCollectionPersister cp = (BasicCollectionPersister)sessionFactoryImpl.getCollectionPersister("com.pcpower.amss.security.objects.User.roles");
          Class associatedClass = cp.getElementPersister().getMappedClass());


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.