【发布时间】:2018-06-14 20:05:08
【问题描述】:
我正在创建一个需要与数据库交互的程序。这是一个简单的库存管理系统,所以实体是“Item”和“Patron”。
编辑:这是一个使用 Spring Boot 和 Spring Data JPA 的 Vaadin 应用程序
首先,我将从我的 2 个类开始,为简洁起见省略 getter/setter。
@Table(name="item")
@Entity
public class Item implements Serializable, Cloneable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long barcode;
@NotNull
private String name, type;
@NotNull
private boolean isAvailable;
@Nullable
private boolean isLate;
@Nullable
private String notes;
@Nullable
private Patron currentPatron;
@Nullable
private Patron[] history;
@Nullable
private Date checkOutDate, dueDate;
public Item() {}
public Item(long barcode, String name, String type, boolean isAvailable) {
this.barcode = barcode;
this.name = name;
this.type = type;
this.isAvailable = isAvailable;
}
public Item(long barcode, String name, String type, String notes, boolean isAvailable) {
this.barcode = barcode;
this.name = name;
this.type = type;
this.notes = notes;
this.isAvailable = isAvailable;
}
public Item(long barcode, String name, String type, String notes, boolean isAvailable, Date checkOutDate, Date dueDate, boolean isLate, Patron currentPatron, Patron[] history) {
this.barcode = barcode;
this.name = name;
this.type = type;
this.notes = notes;
this.isAvailable = isAvailable;
this.checkOutDate = checkOutDate;
this.dueDate = dueDate;
this.isLate = isLate;
this.currentPatron = currentPatron;
this.history = history;
}
}
@Entity
@Table(name="patron")
public class Patron {
@Id
private long id;
@NotNull
private String name, email;
@Nullable
private Item[] checkedOutItems;
@Nullable
private List<Item> itemHistory;
@Nullable
private boolean owesFines;
@Nullable
private int finesOwed;
public Patron() {}
public Patron(long id, String name, String email, boolean owesFines) {
this.id = id;
this.name = name;
this.email = email;
this.owesFines = owesFines;
}
public Patron(long id, String name, String email, Item[] checkedOutItems, List<Item> itemHistory, boolean owesFines, int finesOwed) {
this.id = id;
this.name = name;
this.email = email;
this.checkedOutItems = checkedOutItems;
this.itemHistory = itemHistory;
this.owesFines = owesFines;
this.finesOwed = finesOwed;
}
在实践中,通过使用 MSR 扫描其校园 ID 来实例化赞助人对象。然后,该数据填充顾客类别的姓名、电子邮件和 ID 字段。
在结账时,顾客会先用 MSR 刷卡(系统会确认他们在数据库中,如果没有则添加)。 在他们的磁条被扫描后,他们想要的物品的二维码就会被扫描,这样我们就可以将该物品与他们联系起来。
当一个项目被借给顾客时,我们需要从顾客表中获取他们的 id、name 和 email,然后填充其余变量:check_out_date、due_date 等。
一个读者可以借出许多单册,但一个读者只能借出一个单册。这是否建立了OneToMany 关系?赞助人 -> 物品(
我的思考过程如下:
用于赞助对象
有一个 Items 数组来存储他们当前拥有的项目的条形码。
有一个项目数组列表来存储有关顾客拥有它以及何时拥有它的信息 List<Item> history,这样代码就像 history.addToFront(something) 一样简单
对于项目对象 有一个赞助人对象,看看谁拥有它 有一个顾客数组列表来查看它被签出的所有时间
Q1:将数组和列表作为两个类的实例数据是否多余?
Q1.2:对象数组和对象列表是否适合此类场景的数据结构?
Q1.3:将javax.persistence.*; 和org.springframework.data.annotation.*; 用于ID 之类的东西有区别吗?import javax.validation.constraints.NotNull; 和import org.springframework.lang.NonNull; 之间有区别吗
Q2:这会在 Patron 和 Items 之间产生OneToMany 关系吗?
Q3:为了实现这一点,我相信我的数据库中需要一些额外的表。我在想这样的事情:(我意识到在实现新架构时我需要包含适当的弹簧注释)
项目表
create table item(barcode int(10) primary key, name varchar(64) not null, type varchar(64) not null, availability boolean, is_late boolean, note varchar(255), check_out_date Datetime, due_date Datetime); #foreign keys for currentPatron and Patron History
顾客表
create table patron(id int(10) primary key, name varchar(64) not null, email varchar(64) not null, owes_fines boolean, fines_owed int); #foreign key to item table?
Patron_Item_History 表 :这会从 Patient 表中提取 id、name、email,然后从 item 表中提取 id、check_out_date、due_date?
Item_Patron_History 表:与上表类似的结构?
提前谢谢你。
【问题讨论】:
-
你尝试过每个班级一个文件吗? Spring 使用了很多反射性。这可能是问题所在。这看起来像是一场考试,不是吗?
-
不,是为了工作。此外,patron 和 item 是它们自己独立的类。
标签: java mysql spring hibernate jpa