Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
3.2.5.ga
Mapping documents:
Code:
<hibernate-mapping>
<class name="com.company.data.User" table="user" catalog="mydb" discriminator-value="false">
<cache usage="nonstrict-read-write"/>
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="identity" />
</id>
<discriminator column="member" type="boolean" />
<property name="anonymous" type="boolean">
<column name="anonymous" not-null="true" />
</property>
<property name="emailAddress" type="string">
<column name="email_address" not-null="true" />
</property>
<property name="firstName" type="string">
<column name="first_name" />
</property>
<property name="lastName" type="string">
<column name="last_name" />
</property>
<property name="bozo" type="boolean">
<column name="bozo" not-null="true" />
</property>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping>
<subclass name="com.company.data.Member" extends="com.company.data.User" discriminator-value="true">
<join table="member" catalog="mydb">
<key column="id"/>
<property name="username" type="string">
<column name="username" not-null="true">
<comment>the login username of the user</comment>
</column>
</property>
<property name="password" type="string">
<column name="password" length="64" not-null="true">
<comment>the login password of the user</comment>
</column>
</property>
<property name="createdAt" type="timestamp">
<column name="created_at" length="19" not-null="true">
<comment>the date the user was created</comment>
</column>
</property>
<property name="updatedAt" type="timestamp">
<column name="updated_at" length="19" />
</property>
<many-to-one class="com.company.data.Role" fetch="select" name="role">
<column name="role_id">
<comment>the role of the member</comment>
</column>
</many-to-one>
</join>
</subclass>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Session session = HibernateUtil.getCurrentSession();
session.beginTransaction();
User user = (User)session.load(User.class, id);
session.getTransaction().commit();
return user;
Name and version of the database you are using:MySQL 5.0
With the above mapping, when I set some Users to have their member column be "true" and some be "false" (it is actually a BIT(1) column with 1 or 0), user.isMember() ALWAYS returns false.
Also, the below code always throws the ClassCastException:
Code:
Member member = (Member)post.getAuthor();
out.println(member.getFirstName() + ": member");
}catch(ClassCastException e){
out.println(post.getAuthor().getFirstName() + ": user");
}
However, when I change the discriminator values on the superclass/subclass to be 1 or 0, I get the following HibernateException:
Code:
org.hibernate.WrongClassException: Object with id: 2 was not of the specified subclass: com.company.data.User (Discriminator: true)
org.hibernate.loader.Loader.getInstanceClass(Loader.java:1453)
org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1284)
org.hibernate.loader.Loader.getRow(Loader.java:1206)
org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)
org.hibernate.loader.Loader.doQuery(Loader.java:701)
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
org.hibernate.loader.Loader.loadEntity(Loader.java:1860)
org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3044)
org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:395)
org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)
org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:98)
org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:836)
org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:66)
org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
com.company.data.User$$EnhancerByCGLIB$$48c74ea3.isMember(<generated>)
org.apache.jsp.ForumListTest_jsp._jspService(ForumListTest_jsp.java:111)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
As you can see here, it detects the column value as "true", where it says "(Discriminator: true)". So, why wasn't it working the way it was setup before? Why was it outputting "false" for user.isMember() and why will it not return a Member instance, rather than a User instance?
Any help is appreciated!