Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 147775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:50:26+00:00 2026-05-11T08:50:26+00:00

I have a foo that has a relationship to many bar’s. When I delete

  • 0

I have a foo that has a relationship to many bar’s.

When I delete bar’s in my system I want to keep them in the database for some crazy business reason so I just set a deleted field to true.

Can I specify in my hibernate mapping that I only want my collection to hold elements where that field is false?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:50:27+00:00Added an answer on May 11, 2026 at 8:50 am

    Hibernate provides filters to accomplish this.

    Example beans:

    public class Foo {     private Long id;     private String text;     private Set<Bar> bars = new HashSet<Bar>();         // constructors, getters, setters }  public class Bar {     private Long id;     private boolean deleted;     private String text;     // constructors, getters, setters } 

    Example mappings NB: the filter element

    <hibernate-mapping package='org.nkl.hib'>   <class name='Foo'>     <id name='id' column='FOO_ID'>       <generator class='sequence' />     </id>     <property name='text' />     <set name='bars' cascade='all' fetch='join'>       <key column='FOO_ID' />       <one-to-many class='Bar' />       <filter name='deleted' condition=':deleted = deleted' />     </set>   </class>   <filter-def name='deleted'>     <filter-param name='deleted' type='boolean' />   </filter-def> </hibernate-mapping>  <hibernate-mapping package='org.nkl.hib'>   <class name='Bar'>     <id name='id' column='BAR_ID'>       <generator class='sequence' />     </id>     <property name='text' />     <property name='deleted' />   </class> </hibernate-mapping> 

    Example unit test:

    public class FooBarTest {      private static SessionFactory sessionFactory;      @AfterClass     public static void closeSessionFactory() {         sessionFactory.close();     }      @BeforeClass     public static void setupSessionFactory() {         Configuration configuration = new Configuration();         configuration.configure();         sessionFactory = configuration.buildSessionFactory();     }      @Test     public void testBarFilter() {         doInTransaction(new Command() {             public void execute(Session session) {                 Foo foo = new Foo('foo');                 foo.addBar(new Bar('bar1'));                 foo.addBar(new Bar('bar2'));                 foo.addBar(new Bar('bar3'));                 session.save(foo);             }         });          doInTransaction(new Command() {             public void execute(Session session) {                 Bar bar = (Bar) session.createQuery(                         'from Bar b where b.text = 'bar2'').                         uniqueResult();                 bar.setDeleted(true);                 session.update(bar);             }         });          doInTransaction(new Command() {             public void execute(Session session) {                 session.enableFilter('deleted').                         setParameter('deleted', Boolean.FALSE);                 Foo foo = (Foo) session.createQuery('from Foo').                         uniqueResult();                 assertEquals(2, foo.getBars().size());             }         });     }      private void doInTransaction(Command command) {         Session session = sessionFactory.openSession();         Transaction tx = session.beginTransaction();         command.execute(session);         tx.commit();         session.close();     } }  interface Command {     public void execute(Session session); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that implements IDisposable because it has a private member field
I have a database with a lots of relationships between Tables and a Silverlight
I want to make an ant target that can replace the content of a
I need to reference a model's self in a :has_many declaration. I have a
I think I have done this before, but it could also be a function
So, I'm working on an app where I want to users to be able
I have a rails 3 project where the index.html.erb currently includes <td><%= chapter.university_id %></td>
I see scheduler like feature in salesforce but it is somewhat tied with existing
Can anyone point me to a thread-safe implementation of the Copy-on-write (COW) idiom? The
Is there a way, short of actually checking out the parent commit, to determine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.