Вы можете использовать Enum, чтобы установить дни недели
public enum DAYS_OF_THE_WEEK {
SUN,
MON,
TUE,
WED,
THU,
FRI,
SAT;
}
Теперь вы можете использовать коллекцию типа значения из Hibernate поддерживает его.
@CollectionOfElements
@Enumerated(EnumType.STRING)
@JoinTable(
name="SELECTED_DAYS_OF_THE_WEEK",
[email protected](name="<OWNING_ENTITY_ID_GOES_HERE>")
)
public Set<DAYS_OF_THE_WEEK> getSelectedDays() {
return this.selectedDays;
}
Не забудьте срок службы составного элемента или экземпляра значения типа ограничена продолжительностью жизни владельца экземпляра объекта.
Как сказал:
Смогу ли я найти все объекты, которые Wed выбраны?
Да
select distinc OwningEntity _owningEntity inner join fetch _owningEntity.selectedDays selectedDay where selectedDay = :selectedDay
query.setParameter("selectedDay", DAYS_OF_THE_WEEK.WED);
query.list();
Добавлена оригинальный ответ: Как реализовать FetchingStrategy
Пусть следующая модель
@Entity
public class Customer {
private List<Order> orderList = new ArrayList<Order>();
// getter's and setter's
}
Теперь наш интерфейс CustomerRepository
public interface CustomerRepository {
Customer getById(Integer id, CustomerFetchingStrategy fetchingStrategy);
List<Customer> getAll(CustomerFetchingStrategy fetchingStrategy);
public static enum CustomerFetchingStrategy {
PROXY,
CUSTOMER,
CUSTOMER_WITH_ORDERS;
}
}
Наша реализация
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
}
}
Так ли какой-Use Case только потребности клиента, я называю
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);
}
}
Я надеюсь, что это может быть полезным для вас
С уважением,
Родственные: http://stackoverflow.com/ques/2199399/storing-enumset-in-a-database – finnw