Reputation: 9702
I have an Embeddable model (It is not an Entity), which has only getters(I do not want keep setters, because it's attributes are fixed.) I have this model embedded in another entity.
Now, my requirement is, When I do GET queries in the entity class, I would like to use some parameters defined in the "Embeddable model" . Now the problem is, since Embeddable model does not have seperate table, I could not query the database.
How can I approach this in Hiberanate?
Embeddable class
@Embeddable
public class Observation {
@Column(name = "CREATED")
private LocalDateTime created;
@NotNull
@Column(name="O_CODE")
private String o_code;
public String getCode() {
return o_code;
}
public String getCreated() {
return created;
}
}
Entity class
@Table(name = "field_observation", uniqueConstraints = @UniqueConstraint(columnNames = "code"))
public class FieldObservation {
private Long id;
@Embedded
private Observation observation = null;
getObservation{}
setObservation{}
......... }
GET call
public Response getItems(
@QueryParam("o_code") java.lang.String o_code,
@QueryParam("id") java.lang.Long id,
@QueryParam("format") @DefaultValue("list") String format
) {
---- //How to query database to get Observation entry?
Upvotes: 2
Views: 4269
Reputation: 11906
Hibernate : Supports collections of embeddables through the @CollectionOfElements
annotation and JPA 2.0 ElementCollection
mapping.
Embeddable objects
cannot be queried directly, but they can be queried in the context of their parent. Typically it is best to select the parent, and access the embeddable from the parent. This will ensure the embeddable is registered with the persistence context
. If the embeddable is selected in a query, the resulting objects will be detached
, and changes will not be tracked.
Ex. select fieldObservation.o_code from FieldObservation fieldObservation where id= :param
.
Upvotes: 1