I have an entity, say Customer, that comes in a library and I cannot change. I would like to add a NamedQuery to this entity using annotation. My first trial is to subclass the entity class and add a NamedQuery to the annotation of the subclass, but that does not work because hibernate requires a discriminator column in the table. I later find a solution by not using subclass, but just to map a separate entity to the table, like this:
@Entity @Table(name = "CUSTOMER") @NamedQueries({ @NamedQuery(name = "MY_CUSTOMER_QUERY" , query = "SELECT name from Customer " + "WHERE id in (:ids) order by name") }) public class MyCustomer {
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CUSTOMER_SEQ") @SequenceGenerator(name = "CUSTOMER_SEQ", sequenceName = "CUSTOMER_SEQ", allocationSize = 1) @Column(name = "CUSTOMER_ID") private Long id; }
While this appears to work, it does requires specifying a table and an id for the code to work, which is not really used. I wonder if there is a simpler way to do this?
|