I have a small Table called Table_LL_Category with two fields: Category_ID and Category (name of category).
With Hibernate 2 and Spring 1.0 all worked find, but when I upraded to Hibernate 3 and Spring 1.2, the category (String getCategory()) always contained the Id (field Category_ID) and not the field Category.
Hibernate did not even select the field category:
Code:
Hibernate: select category0_.Category_id as Category1_0_ from Table_LL_Category category0_ where category0_.Category_id=?
After renaming the field from Category to CategoryName all worked fine again. This problem did not occur with a similiar Table called Subcategory.
Is this a bug in Hibernate 3?
Hibernate version: 3.04 - 3.05
Mapping documents:<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://localhost/dtds/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1
http://boss.bekk.no/boss/middlegen/ http://www.hibernate.org/-->
<class
name="model.Category"
table="Table_LL_Category"
>
<id
name="categoryId"
type="java.lang.Integer"
column="Category_id"
>
<generator class="identity" />
</id>
<property
name="category"
type="string"
column="Category"
not-null="true"
length="50"
/>
<!-- Associations -->
<!-- bi-directional one-to-many association to Subcategory -->
<set
name="Subcategories"
lazy="true"
inverse="true"
cascade="none"
>
<key>
<column name="Category_id" />
</key>
<one-to-many
class="model.Subcategory"
/>
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import model.Category;
public void testCategory()
{
Category cat1 = new Category();
cat1.setCategory("cat1");
categoryDao.save(cat1);
Category cat2 = categoryDao.get(cat1.getCategoryId().intValue());
assertEquals("cat1", cat2.getCategory());
}
categoryDao:
Code:
public class CategoryDaoHibernate extends HibernateDaoSupport implements CategoryDao
{
public void save(Category category)
{
getHibernateTemplate().saveOrUpdate( category );
}
public void delete(Category category)
{
getHibernateTemplate().delete( category );
}
public List getAll()
{
return getHibernateTemplate().find( "from Category" );
}
public Category get(int id)
{
return (Category) getHibernateTemplate().get( Category.class, new Integer(id) );
}
}
Category.java
Code:
import java.io.Serializable;
import java.util.Set;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class Category implements Serializable {
/** identifier field */
private Integer categoryId;
/** persistent field */
private String category;
/** persistent field */
private Set Subcategories;
/** full constructor */
public Category(String category, Set Subcategories) {
this.category = category;
this.Subcategories = Subcategories;
}
/** default constructor */
public Category() {
}
public Integer getCategoryId() {
return this.categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public String getCategory() {
return this.category;
}
public void setCategory(String category) {
this.category = category;
}
public Set getSubcategories() {
return this.Subcategories;
}
public void setSubcategories(Set Subcategories) {
this.Subcategories = Subcategories;
}
public String toString() {
return new ToStringBuilder(this)
.append("categoryId", getCategoryId())
.toString();
}
}
Name and version of the database you are using:MSSQL 2000
The generated SQL (show_sql=true):Code:
Hibernate: select category0_.Category_id as Category1_0_ from Table_LL_Category category0_ where category0_.Category_id=?