JPA one-to-many filtering

前端 未结 4 1902
遇见更好的自我
遇见更好的自我 2021-02-05 05:20

We are nesting several entities. However upon retrieving we only want to get those entities which are active.

@Entity
public class System {
  @Id
  @Column(name          


        
4条回答
  •  清酒与你
    2021-02-05 05:53

    I needed to solve similar problem in EclipseLink. I used special SessionCustomizer and changed mapping condition for OneToMany annotation. Maybee Hibernate has something similar.

    Add customizer to persistence unit:

    props.put(PersistenceUnitProperties.SESSION_CUSTOMIZER,MySessionCustomizer.class.getName());
    EntityManagerFactory factory = new PersistenceProvider().createEntityManagerFactory(pu.getPersistenceUnitName(), props);
    

    Fragment of customizer:

    public class MySessionCustomizer implements SessionCustomizer {
        public void customize(Session session) throws Exception {
            final Map descs = session.getDescriptors();
        if (descs != null)
        {
          // This code assumes single table per descriptor!
          for (final Object descObj : descs.values())
          {
            final ClassDescriptor desc = (ClassDescriptor) descObj;
            final Class sourceClass = desc.getJavaClass();
    
            for (DatabaseMapping mapping : desc.getMappings())
            {
              if (mapping instanceof OneToManyMapping)
              {
                final OneToManyMapping collectionMapping = ((OneToManyMapping) mapping);
    
                // create default foreign key condition (nescessary):
                final DatabaseField sourceField = mapping.getSourceKeyFields().get(0);
                final DatabaseField targetField = mapping.getTargetForeignKeyFields().get(0);
                final Expression defaultFkExpression =  new ExpressionBuilder(mapping.getReferenceClass()).getParameter(sourceField).equal(eb.getField(targetField));
    
                // add filter condition "additionalExpression"
                final Expression finalExpression = defaultFkExpression.and(additionalExpression);
    
                 // SET default foreign key condition and filter condition to mapping
                 mapping.setSelectionCriteria(finalExpression);
              }
            }
          }
        }
        }
    }
    

提交回复
热议问题