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.