对一周中的多天进行建模

发布于 2024-10-15 18:02:25 字数 463 浏览 7 评论 0原文

这是我第二次遇到对象和工作日之间的一对多关系,我不确定我是否以最好的方式对其进行建模:

@Entity
MyObject {
    private int id;
    private String foo;
    private boolean monday;
    private boolean tuesday;
    private boolean wednesday;
    private boolean thursday;
    private boolean friday;
    private boolean saturday;
    private boolean sunday;
}

有没有人遇到更好的方法来做到这一点?

我的要求是从数据库中检索与当前星期几匹配的所有 MyObject 实例,即不需要检查多天,例如 mon=1 &&星期二=1

This is the second time i've come accross a one-many relationship between object and weekday and I'm not sure if i'm modelling it in the best way:

@Entity
MyObject {
    private int id;
    private String foo;
    private boolean monday;
    private boolean tuesday;
    private boolean wednesday;
    private boolean thursday;
    private boolean friday;
    private boolean saturday;
    private boolean sunday;
}

Has anyone come across a better way of doing this?

My requirement is to retrieve all instances of MyObject from the db that match the current day of the week, i.e. no need to check multiple days for example mon=1 && tues=1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〆凄凉。 2024-10-22 18:02:25

对集合中有限数量的项目进行建模时,通常最好使用 枚举

public enum DayOfWeek {
    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}

您可以在课堂上使用它,如下所示:

class YourClass {
    private Set<DayOfWeek> activeDays;

    public void addActiveDay(DayOfWeek day){
        activeDays.add(day);
    }
}
...

yourclass.addActiveDay(DayOfWeek.Friday);

When modeling a finite number of items in a set, it is usually best to use an enum.

public enum DayOfWeek {
    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}

You might use this in your class like so:

class YourClass {
    private Set<DayOfWeek> activeDays;

    public void addActiveDay(DayOfWeek day){
        activeDays.add(day);
    }
}
...

yourclass.addActiveDay(DayOfWeek.Friday);
愁杀 2024-10-22 18:02:25

创建一个星期几的枚举,然后使用一个私有数组来存储适用的日期怎么样?如果你想变得更奇特,你可以有一些结构(例如 Set,我认为它不允许重复),而不是防止重复的数组。

如果您也在数据库中使用它,您正在运行哪种查询来获取数据,以及您使用哪种表结构(枚举解决方案可能不适用于所有数据库)?

How about create an enum of days of the week, and then have a private array that stores the days that are applicable? If you want to get fancy, you could have some structure (such as a Set, which I think does not allow duplicates) other than an array that prevents duplicates.

If you're also using this with a database, what kind of query are you running to get data, and what kind of table structure are you using (enum solutions may not work with all databases)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文