Hi , I have a legacy PHP forum (just like classical User/Board/Post forum) , I want to use Hibernate to map the forum to Java classes.
The schema is simple : The POST TABLE has two fields link to USER / BOARD TABLE's primary keys , indicating the poster and the board containg it.
But some posts' POSTER value cannot be found in USER.NUMBER , meaning these posts are orphan posts.
I don't want to lose these posts , and want to give them a DUMMY Poster.
To achieve this , I write an interceptor like this :
Code:
public class PostInterceptor implements Interceptor
{
public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException
{
if (entity instanceof Post)
{
if (("timestamp").equals( propertyNames[i]))
//..... (another intercept method) ........
if (("poster").equals( propertyNames[i]))
{
if (state[i] == null)
{
User u = new User();
u.setNumber(1);
u.setLoginName("Invalid_User");
u.setUsername ("Invalid_User");
u.setEmail("INVALID@NON_EXIST.COM");
u.setPassword("XXX");
u.setStatus("INVALID");
u.setTitle("INVALID");
state[i] = u;
}
}
}
}//if
return false;
}
}
I think it should be OK , but when running , hibernate throws this exception :
org.springframework.orm.hibernate.
HibernateObjectRetrievalFailureException: No row with the given identifier exists: 9169, of class: forum.User; nested exception is net.sf.hibernate.
UnresolvableObjectException: No row with the given identifier exists: 9169, of class: forum.User
I know this is because hibernate finds a Post whose PosterId = 9169 , and cannot find a User.Number = 9169 ....
But it seems Interceptor cannot intercept post.setUser() ... (other methods can be intercepted , such as post.setTimestamp()...)
How do I solve this ?
Hibernate version:2.1.6 with Spring 1.1.3 Mapping documents:Code:
<class name="Post" table="Posts">
<cache usage="read-only"/>
<id name="number" column="B_Number" type="long">
<generator class="native"/>
</id>
<property name="subject" column="B_Subject" type="java.lang.String"/>
<property name="body" column="B_Body" type="java.lang.String"/>
<property name="timestamp" column="B_Posted" type="long"/>
<many-to-one name="poster" column="B_PosterId" class="User" not-null="false"/>
<many-to-one name="board" column="B_Board" class="Board"/>
</class>
<bean id="postInterceptor" class="forum.hibernate.PostInterceptor" />
<bean id="postHibernateInterceptor" class="org.springframework.orm.hibernate.HibernateInterceptor">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>
</property>
<property name="entityInterceptorBeanName">
<value>postInterceptor</value>
</property>
</bean>
<bean id="postDaoImpl" class="forum.hibernate.PostDaoImpl">
<property name="sessionFactory">
<ref local="mySessionFactory"/>
</property>
</bean>
<bean id="postDao" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>forum.dao.PostDao</value>
</property>
<property name="interceptorNames">
<list>
<value>postHibernateInterceptor</value>
<value>postDaoImpl</value>
</list>
</property>
</bean>
Code between sessionFactory.openSession() and session.close():Spring :
Code:
public Post getPost(final long number)
{
return (Post) this.getHibernateTemplate().execute
(
new HibernateCallback()
{
public Object doInHibernate(Session session) throws HibernateException
{
try
{
return session.load(Post.class , new Long(number));
}
catch (ObjectNotFoundException e)
{
return null;
}
}
}
);
}
Full stack trace of any exception that occurs:
Name and version of the database you are using:
MySQL 3.23.58
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt: