对一周中的多天进行建模
这是我第二次遇到对象和工作日之间的一对多关系,我不确定我是否以最好的方式对其进行建模:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对集合中有限数量的项目进行建模时,通常最好使用
枚举
。您可以在课堂上使用它,如下所示:
When modeling a finite number of items in a set, it is usually best to use an
enum
.You might use this in your class like so:
创建一个星期几的枚举,然后使用一个私有数组来存储适用的日期怎么样?如果你想变得更奇特,你可以有一些结构(例如
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)?