-->
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.  [ 3 posts ] 
Author Message
 Post subject: Query Issues With Many-To-One Relationships
PostPosted: Sun Oct 04, 2009 6:01 pm 
Newbie

Joined: Sun Oct 04, 2009 2:49 pm
Posts: 4
Hello all.

Code:
Specs:

Microsoft Windows XP Professional Edition
MySQL 5.1
Hibernate Core 3.0
JDK 1.6


So, I currently have 2 tables that have a many-to-one relationship, named Ability and AbilityType. Here are the sql commands used to create the tables:

Code:
AbilityType

create table AbilityType
(
  id int not null primary key,
  name varchar(30),
  description text
)type=InnoDB;

Ability

create table Ability
(
  id int not null primary key,
  name varchar(30),
  description text,
  abilityTypeID int not null references AbilityType(id)
)type=InnoDB;


Here is my hibernate config file:

Code:
<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>

    <!-- hibernate dialect -->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

   
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost/*</property>
    <property name="hibernate.connection.username">*</property>
    <property name="hibernate.connection.password">*</property>
    <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
   
    <!-- Automatic schema creation (begin) === -->     
    <!-- <property name="hibernate.hbm2ddl.auto">create</property> -->

   
    <!-- Simple memory-only cache -->
    <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>

     <!-- Enable Hibernate's automatic session context management -->
     <property name="current_session_context_class">thread</property>

    <!-- ############################################ -->
    <!-- # mapping files with external dependencies # -->
    <!-- ############################################ -->

    <mapping resource="com/brc/roe/db/ability/AbilityType.hbm.xml"/>
    <mapping resource="com/brc/roe/db/ability/Ability.hbm.xml"/>
   
</session-factory>
</hibernate-configuration>


Here are the XML mappings for the tables:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


<hibernate-mapping>

   <class name="com.brc.roe.db.ability.AbilityType" table="AbilityType">

      <id name="id" column="ID">
         <generator class="assigned" />
      </id>

      <property name="name" type="string">
         <column name="name"/>
      </property>

      <property name="description" type="text">
         <column name="description" />
      </property>
      
   </class>
</hibernate-mapping>

*********************************************************

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


<hibernate-mapping>

   <class name="com.brc.roe.db.ability.Ability" table="Ability">

      <id name="id" column="ID">
         <generator class="assigned" />
      </id>

      <property name="name" type="string">
         <column name="name"/>
      </property>

      <property name="description" type="text">
         <column name="description" />
      </property>
      
      <many-to-one name="abilityType" class="com.brc.roe.db.ability.AbilityType">
         <column name="abilityTypeID" />
      </many-to-one>
      
   </class>
</hibernate-mapping>


Here are the Java files for the tables:

Code:
package com.brc.roe.db.ability;

public class AbilityType
{
   Long id;
   String name;
   String description;
   
   public Long getId() {return id;}
   public void setId(Long id) {this.id = id;}
   
   public String getName() {return name;}
   public void setName(String name) {this.name = name;}
   
   public String getDescription() {return description;}
   public void setDescription(String description) {this.description = description;}
}

**********************************************************

package com.brc.roe.db.ability;

public class Ability
{
   private long id;
   private String name;
   private String description;
   private AbilityType abilityType;
   
   public long getId() {return id;}
   public void setId(long id) {this.id = id;}
   
   public String getName() {return name;}
   public void setName(String name) {this.name = name;}
   
   public String getDescription() {return description;}
   public void setDescription(String description) {this.description = description;}

   public AbilityType getAbilityType() {return abilityType;}
   public void setAbilityType(AbilityType abilityType) {this.abilityType = abilityType;}
}


Now, when I run a normal join on these tables to get all Abilities by a specific AbilityType, I am expecting to get 5 results of type com.brc.roe.db.ability.Ability in the result set. What happens however is, I get 5 results of type java.lang.Object. Encapsulated within these Objects are a reference to both a com.brc.roe.db.ability.Ability AND a com.brc.roe.db.ability.AbilityType.

Now, the results in of themselves are correct, but is there any way to just get the com.brc.roe.db.ability.Ability's? Here is my code:

Code:
   public List<Ability> fetchAbilityByType(String typeName)
   {
      Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
      session.beginTransaction();
      
      Query query = session.createQuery("from AbilityType t, Ability a where t.name = '" + typeName + "' and t.id = a.abilityType");
      List<Ability> list = query.list();
      
      session.getTransaction().commit();
      
      return list;
   }


If any more information is needed, let me know.

Thank you.


Top
 Profile  
 
 Post subject: Re: Query Issues With Many-To-One Relationships
PostPosted: Sun Oct 04, 2009 9:51 pm 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
Try to use:
Code:
Query query = session.createQuery("select t from AbilityType t, Ability a where t.name = '" + typeName + "' and t.id = a.abilityType");


Top
 Profile  
 
 Post subject: Re: Query Issues With Many-To-One Relationships
PostPosted: Sun Oct 04, 2009 10:42 pm 
Newbie

Joined: Sun Oct 04, 2009 2:49 pm
Posts: 4
SIau_Tie wrote:
Try to use:
Code:
Query query = session.createQuery("select t from AbilityType t, Ability a where t.name = '" + typeName + "' and t.id = a.abilityType");


Thanks, that did the trick! By the way, the code in my initial post should have read:

Code:
Query query = session.createQuery("from Ability a, AbilityType t where t.name = '" + typeName + "' and t.id = a.abilityType");


Regardless, now that I am using this:

Code:
Query query = session.createQuery("select a from Ability a, AbilityType t where t.name = '" + typeName + "' and t.id = a.abilityType");


Everything is working great. Thanks again!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.