坚持一套星期几的 [英] Persisting a set of Days of the Week

查看:113
本文介绍了坚持一套星期几的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种有效的方式坚持一套星期几的。换句话说,用户将从平日的列表中选择的任何天数。具体日期不要紧,一周中就在一天。我需要这套天的存储在一个Hibernate实体。

I'm looking for an efficient way to persist a set of Days of the Week. In other words, a user will select any number of days from a list of weekdays. The specific date doesn't matter, just the day of the week. I need to store this set of days in a hibernate entity.

我可以存储其中之一:

Set<DayOfWeek>
Set<Integer>

不过,我想这可能是比简单。如果我只是用什么单个int来存储数据,并用位操作。例如:

But I'm thinking it could be simpler than that. What if I just use a single int to store the data and used bitwise operations. For instance:

Sun = 1
Mon = 2
Tue = 4
Wed = 8
Thu = 16
Fri = 32
Sat = 64

所以要重新present集周日,周一,周三,周六我会坚持整数75.这是因为1 + 2 + 8 + 64 = 75。

So to represent the set of Sun, Mon, Wed, Sat I would persist the integer 75. This is because 1+2+8+64 = 75.

我关心的是在这样一个领域进行查询。我将能够找到那些选择结婚的所有实体?

My concern is performing queries on a field like this. Would I be able to find all entities that have Wed selected?

这是否看起来是个好办法?有没有人有一个更好的主意?

Does this seem like a good approach? Does anyone have a better idea?

谢谢!

推荐答案

您可以用枚举来设置星期几。

You could use Enum to set up days of the week

public enum DAYS_OF_THE_WEEK {
    SUN,
    MON,
    TUE,
    WED,
    THU,
    FRI,
    SAT;
}

现在你可以因为Hibernate支持它使用值类型的集合。

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.

象曰:

我是否能够找到已经结婚选择的所有实体?

Would I be able to find all entities that have Wed selected ?

select distinc OwningEntity _owningEntity inner join fetch _owningEntity.selectedDays selectedDay where selectedDay = :selectedDay

query.setParameter("selectedDay", DAYS_OF_THE_WEEK.WED);

query.list();

添加到原来的答案:如何实现FetchingStrategy

Added to original answer: how do i implement FetchingStrategy

假设下面的模型

@Entity
public class Customer {

    private List<Order> orderList = new ArrayList<Order>();

    // getter's and setter's

}

现在我们的界面CustomerRepository

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;         
    }

}

我们的执行

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
    }

} 

所以,无论一些使用案例的需要客户,我叫

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

问候,

这篇关于坚持一套星期几的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆