請注意:我是新來發揮框架玩! Framework數據庫繼承
信息:
我一直在使用播放框架尋找一個體面的教程或有關繼承的解釋一兩個小時,
但是每個例子似乎都忽略了引起混淆的整個示例,因此也是這個問題的原因。
爲了記錄在案,我使用MariaDB的(又名〜MySQL的)
研究&文檔:
文檔 - 玩:this是不是非常有幫助
文檔 - Java:useful,缺少完整示例
博客:This缺乏完整的例子,雖然有見地
搜索上Youtube,對於PlayFramework設立eBean有用。
不過,我可能只是完全misunderstandin,但所提供的例子加入不同的名字列到表未提及。
問題/疑問:
我需要建立一個數據庫,利用繼承(即外鍵等),究竟如何,與解釋,我能做到嗎?
數據庫模式:
我有什麼目前:
型號:用戶
import io.ebean.Finder;
import io.ebean.Model;
import play.data.validation.Constraints;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
@MappedSuperclass
//@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class User extends Model {
@Id
@GeneratedValue
private Long userId;
@Constraints.MaxLength(50)
private String name;
@Constraints.MaxLength(100)
private String surname;
@Constraints.Required
@Constraints.MinLength(8)
private String password;
@Constraints.Email
@Constraints.Required
private String email;
@Constraints.Required
@Constraints.MaxLength(10)
@Constraints.MinLength(10)
@Constraints.Pattern("[0]\\d{2}[- ]{0,1}\\d{3}[- ]{0,1}\\d{4}")
private String cellNumber;
private Boolean emailVerified = false;
private String token;
public static Finder<String, User> find = new Finder<String, User>(User.class);
public User(){}
public User(@Constraints.MinLength(10) @Constraints.MaxLength(10) Long userId, String name, String surname, @Constraints.Required String password, @Constraints.Email @Constraints.Required String email, @Constraints.Required @Constraints.Pattern("[0]\\d{2}[- ]{0,1}\\d{3}[- ]{0,1}\\d{4}") String cellNumber, Boolean emailVerified) {
this.userId = userId;
this.name = name;
this.surname = surname;
this.password = password;
this.email = email;
this.cellNumber = cellNumber;
this.emailVerified = emailVerified;
}
型號:員工
import io.ebean.Finder;
import play.data.validation.Constraints;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* Created by cybex on 2017/07/13.
*/
@Entity
public class Staff extends User {
@Id
private Long userId;
@Constraints.Required
private Boolean isKitchenStaff;
public static Finder<String, Staff> find = new Finder<String, Staff>(Staff.class);
public Staff(@Constraints.Required Long userId, String name, String surname, @Constraints.Required String password, @Constraints.Email @Constraints.Required String email, @Constraints.Required @Constraints.Pattern("[0]\\d{2}[- ]{0,1}\\d{3}[- ]{0,1}\\d{4}") String cellNumber, String userId1, @Constraints.Required Boolean isKitchenStaff) {
super(userId, name, surname, password, email, cellNumber, false);
this.isKitchenStaff = isKitchenStaff;
}
}
型號:客戶
import io.ebean.Finder;
import play.data.validation.Constraints;
import javax.persistence.Entity;
@Entity
public class Customer extends User {
@Constraints.Required
private Address address;
private Boolean isStudent = false;
public Customer(){}
public Customer(Long userId, String name, String surname, String email, String cellNumber, String password, String userId1, @Constraints.Required Address address, Boolean isStudent) {
/*super(userId, name, surname, email, cellNumber, password, false);*/
this.address = address;
this.isStudent = isStudent;
}
public static final Finder<String, Customer> find = new Finder<String, Customer>(Customer.class);
}
型號:地址
import io.ebean.Finder;
import io.ebean.Model;
import play.data.validation.Constraints;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* Created by cybex on 2017/07/13.
*/
// TODO: 2017/07/13 Add fields for suburb and city for larger projects
@Entity
public class Address extends Model {
@Id
@Constraints.Required
@GeneratedValue
@Constraints.Max(15)
private Long addressId;
@Constraints.Required
private String unitNumber;
@Constraints.Required
private String streetName;
private String communityName;
private Boolean isCommunity;
public Address(@Constraints.Required Long addressId, @Constraints.Required String unitNumber, @Constraints.Required String streetName, String communityName, Boolean isCommunity) {
this.addressId = addressId;
this.unitNumber = unitNumber;
this.streetName = streetName;
this.communityName = communityName;
this.isCommunity = isCommunity;
}
public static Finder<String, Address> find = new Finder<String, Address>(Address.class);
}
建議是增加:
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
但是它導致一個錯誤:
CreationException: Unable to create injector, see the following errors:
1) Error injecting constructor, java.lang.NullPointerException
看到here完全堆棧跟蹤
問題(和TL; DR):
TL; DR:基本功能齊全,具有解釋相關,播放!表示上述數據庫模式的框架模型。