petr.hribal 0 Newbie Poster

Hi Boys,
I would like to ask you, how should I correctly implement entity life-cycle using hibernate filters. At this time I have got the following classes and DAOS, which I considered to be right - but they obviously aren't.

Here are entities which are used in my system.

// AbstractCommontObject ----------------------------------------------
@Entity
@FilterDef(name = "softDelete")
@Filter(name = "softDelete", condition = "(status=0 OR status IS NULL)")
@Table(name = "abstract_common_object")
@Inheritance(strategy = InheritanceType.JOINED)
public class  AbstractCommontObject {
 ...
 private Integer status;
 ...
}
// Message -------------------------------------------------------------
@Entity
@Table(name = "message")
public class Message extends AbstractCommontObject {
}

The next class defines logical relation between two messages.

// MessageRelation ---------------------------------------------------------
@Entity
@Table(name = "message_relation")
public class MessageRelation {
  
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "source")
  @Filter(name = "softDelete", condition = "(status=0 OR status IS NULL)")
  private Message sourceObject;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "target")
  @Filter(name = "softDelete", condition = "(status=0 OR status IS NULL)")
  private Message targetObject;

  private RelationTypeEnum relationType;
}

The DAO object.

// MessageDaoHibernate ---------------------------------------------
    /** {@inheritDoc} */
    @Override
    public List<Message> getMessages(final int page, final int length) {
        String hql = "SELECT m FROM Message AS m ORDER BY m.lastUpdate DESC";
        Query q = getSession().createQuery(hql)
            .setFirstResult(page)
            .setMaxResults(length);
        @SuppressWarnings("unchecked")
        final List<Message> results = q.list();
        return results;
    }

    /** {@inheritDoc} */
    @Override
    public List<Message> getRelatedMessages(final Message messageid, final int page, final int length) {
        String hql = "SELECT source FROM MessageRelation AS mr INNER JOIN mr.sourceObject AS source "
            + "WHERE mr.relationType = :relationType "
            + "AND mr.targetObject.id = :messageid";
        Query q = getSession().createQuery(hql)
            .setString("relationType", RelationTypeEnum.SUBMESSAGE)
            .setFirstResult(page)
            .setMaxResults(length);
        @SuppressWarnings("unchecked")
        final List<Message> results = q.list();
        return results;
    }

    protected Session getSession() {
        Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
        session.enableFilter("softDelete");
        return session;
    }

The problem is that the first method defined in MessageDao works fine - the lifecycle filter excludes all messages being in other state then 0 or null, but the second method returns all messages - no matter, whether they are marked by "active" tag or not.

I suspect the problem is caused by the relation entity, but I'm not sure. I appreciate all your help in advance, Regards and Thanks a lot. P.H.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.