Hi,
You could use Enum to set up days of the week
public enum DAYS_OF_THE_WEEK {
SUN,
MON,
TUE,
WED,
THU,
FRI,
SAT;
}
Now you can use a collection of value type because of Hibernate supports it.
@CollectionOfElements
@Enumerated(EnumType.STRING)
@JoinTable(
name="SELECTED_DAYS_OF_THE_WEEK",
joinColumns=@JoinColumn(name="<OWNING_ENTITY_ID_GOES_HERE>")
)
public Set<DAYS_OF_THE_WEEK> getSelectedDays() {
return this.selectedDays;
}
Do not forget the lifespan of a composite element or a value-type instance is bounded by the lifespan of the owning entity instance.
As said:
Would I be able to find all entities that have Wed selected ?
Yes
select distinc OwningEntity _owningEntity inner join fetch _owningEntity.selectedDays selectedDay where selectedDay = :selectedDay
query.setParameter("selectedDay", DAYS_OF_THE_WEEK.WED);
query.list();
Added to original answer: how do i implement FetchingStrategy
Suppose the following model
@Entity
public class Customer {
private List<Order> orderList = new ArrayList<Order>();
// getter's and setter's
}
Now our interface CustomerRepository
public interface CustomerRepository {
Customer getById(Integer id, CustomerFetchingStrategy fetchingStrategy);
List<Customer> getAll(CustomerFetchingStrategy fetchingStrategy);
public static enum CustomerFetchingStrategy {
PROXY,
CUSTOMER,
CUSTOMER_WITH_ORDERS;
}
}
Our implementation
import static br.com.app.CustomerRepository.CustomerFetchingStrategy;
public class CustomerRepositoryImpl implements CustomerRepository {
// Usually Spring IoC or Seam @In-jection or something else
private SessionFactory sessionFactory;
public Customer getById(Integer id, CustomerFetchingStrategy fetchingStrategy) {
switch(fetchingStrategy) {
case PROXY:
return (Customer) sessionFactory.getCurrentSession().load(Customer.class, id);
case CUSTOMER:
return (Customer) sessionFactory.getCurrentSession().get(Customer.class, id);
case CUSTOMER_WITH_ORDERS:
return (Customer) sessionFactory.getCurrentSession().createQuery("from Customer c left join fetch c.orderList where c.id = :id")
.setParameter("id", id)
.list().get(0);
}
}
public List<Customer> getAll(CustomerFetchingStrategy fetchingStrategy) {
// Same strategy as shown above
}
}
So whether some Use Case only needs CUSTOMER, i call
import static br.com.app.CustomerRepository.CustomerFetchingStrategy;
public class SomeController {
// Again Spring Ioc or Seam @In-jection
private CustomerRepository customerRepository;
public void proccessForm(HttpServletRequest request, HttpServletResponse response) {
request.setParameter("customer", customerRepository.getById(Integer.valueOf(request.getParameter("customerId"))), CUSTOMER);
}
}
I hope it can be useful to you
regards,