nirvair nirvair - 1 year ago 64
MySQL Question

@Where annotation in hibernate on child table

This is my schema:

- id (PK)
- name
- description

- id (PK)
- event_id (References id (event_details)
- ticket_name


//bi-directional many-to-one association to EventTicketType
@OneToMany(mappedBy="eventDetail", cascade = CascadeType.ALL)
private Set<EventTicketType> eventTicketTypes;

in my EventDetail entity, I have added
@Where(clause = "deleted_at is NULL")

I've also added the same in EventTicketType

This is how I am getting the data in service.

EventDetail eventDetail = eventDetailRepository.getById(eventId);
Set<EventTicketType> eventTicketTypes = eventDetail.getEventTicketTypes();

And then I am mapping entity and dto.

In the query log - I see that parent table (event_details) executes query with where condition i.e
where deleted_at is NULL
, but the child table (event_ticket_types) does not have a where condition

Where is it that I am doing wrong?

Answer Source

You're not showing where the @Where annotations are but I'm guessing they're both at class level only. Try also adding the @Where annotation to the @OneToMany method above.

The annotation works at entity level when you select the entity directly, but you need it on the relationship method also if you want it to work for joins.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download