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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 78k
  • Answers 79k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Ruby does some conflation - methods use (), but arrays,… May 11, 2026 at 3:58 pm
  • added an answer The problem you're having is that the Forms element is… May 11, 2026 at 3:58 pm
  • added an answer Try decimal x = 32.0040M; string value = x.ToString('N' +… May 11, 2026 at 3:58 pm

Related Questions

I have a foo that has a relationship to many bar's. When I delete
I'm not sure how to search for this answer, so I'll go ahead and
Suppose the following data schema: Usage ====== client_id resource type amount Billing ====== client_id
Let say I have a class like this: public sealed class Foo { public
Hey all, I have something of an interesting requirement for my project. I need

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.