-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Implementing Entity Lifecycle via Hibernate Filters
PostPosted: Wed May 25, 2011 10:10 am 
Newbie

Joined: Wed May 25, 2011 9:33 am
Posts: 3
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.

Code:
// 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;
...
}


Code:
// Message -------------------------------------------------------------
@Entity
@Table(name = "message")
public class Message extends AbstractCommontObject {
}


The next class defines logical relation between two messages.

Code:
// 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.

Code:

    // 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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.