问题
I have a base entity that holds a enum field called State.
public enum State {
DELETED(0),
ACTIVE(1)
;
private int value;
private State(int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
}
Is there a JPA annotated way to set the where clauses of the generated queries to retrieve only entities that the state field is "ACTIVE"?
The reason is that if i have an entity "A" that holds a list of entities "B", when i retrieve from the DB an instance of "A" and initialize it, i want the list of "B" to hold only the entities where the State filed is "ACTIVE"
回答1:
There is no filter or where annotations in JPA and there is no way to filter collection entities using annotations.
I suggest you retrieving A entities and fetching only active B entities.
回答2:
Use @Filter
as
@Filter(name, condition)
or @Where
as
@where(state = STATE.ACTIVE.getValue())
回答3:
There is no standard way to do this in JPA.
One method might be to create a view that filters the table and map to the view instead of the table.
If you are using EclipseLink you can use @AdditionalCriteria
http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_additionalcriteria.htm#additionalcriteria
回答4:
I suggest you do this with database views. Access the underlying table containing all entities including the soft deleted entities and access a view for the non-deleted entities
来源:https://stackoverflow.com/questions/12921136/set-where-clause-on-field