1

I have two classes ServiceFee and DeliveryFee.

@Entity
@Table(name = "service_fees")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorFormula(
        "CASE WHEN is_delivery_fee = 1 THEN 'DELIVERY_FEE' ELSE 'SERVICE_FEE' end"
)
public class ServiceFee  {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "is_delivery_fee")
    private Boolean isDeliveryFee;

   ...

}

and

@Entity
@DiscriminatorValue("DELIVERY_FEE")
public class DeliveryFee extends ServiceFee {

    public Enums.DeliveryOption getOrderType() {
        return Enums.DeliveryOption.DELIVERY;
    }

    public Boolean getIsDeliveryFee() {
        return Boolean.TRUE;
    }
}

My serviceFeeDAO has a method to return all ServiceFees (including the deliveryFees), which works as expected. I added method in the serviceFeeDAO which returns all the deliveryFee, please find code of the same below:

public List<DeliveryFee> getDeliveryFees() {
        CriteriaBuilder builder = currentSession().getCriteriaBuilder();
        CriteriaQuery<DeliveryFee> query = builder.createQuery(DeliveryFee.class);
        Root<DeliveryFee> root = query.from(DeliveryFee.class);
        return currentSession().createQuery(query)
                .list();
    }

invoking this method gave me an IllegalArgumentException that the DeliveryFee is not an entity.

Then I went ahead and created a DeliveryDAO class and added this method there. ad now the method returns an empty list.

I am looking to learn why I got the IllegalArgumentException in the first case, and why the method in the DeliveryFeeDAO won't identity the pick up the delivery fee entities.

Praveen Dass
  • 586
  • 1
  • 3
  • 13

1 Answers1

0

Maybe take a look on this question, seems it has something to do with downcasting to base class: JPA 2 Criteria Query on Single Table Inheritance (Hibernate)

Razvan
  • 11
  • 4