實體類註解生成資料庫表
阿新 • • 發佈:2019-01-26
實體基類:是否啟用
@MappedSuperclass public abstract class AbstractBasis extends AbstractEntity { private static final long serialVersionUID = 7952571906802309278L; /** * 禁用狀態,false:禁用 true:可用 */ private Boolean isEnable = true; @Column(nullable = false) public Boolean getIsEnable() { return isEnable; } public void setIsEnable(Boolean isEnable) { this.isEnable = isEnable; } }
@MappedSuperclass 作用:封閉類中的通用屬性(如:id),此類不能再新增@Entity或@Table註解,即不會對映到資料庫表,產生資料庫表,但該類的屬性將對映到其子類中,資料庫中表也將生成對應的欄位。
實體類(標籤)
@Entity @Table(name = "edu_tag") public class EduTag extends AbstractBasis { private static final long serialVersionUID = -5409801373006627913L; private String code; private Long appId; private Long corpCode;/**教育號編碼 */ private String name; private School school; @Id @Column(name="edu_tag_code",nullable = false, length = 20) public String getCode() { return code; } public void setCode(String code) { this.code = code; } @Column(name="edu_tag_name",nullable = false,length = 20) public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name="edu_tag_appId" ,nullable = false,length = 20) public Long getAppId() { return appId; } public void setAppId(Long appId) { this.appId = appId; } @Column(name="edu_tag_corpCode", nullable = false,length = 20) public Long getCorpCode() { return corpCode; } public void setCorpCode(Long corpCode) { this.corpCode = corpCode; }
@OneToOne(cascade = CascadeType.REFRESH , fetch = FetchType.EAGER) @JoinColumn(name = "edu_corp_shcool") public School getShcool() { return shcool; } public void setShcool(School shcool) { this.shcool = shcool; } } @Entity : 實體類 @Table(name = "xxx"): 定義資料庫表名xxx @Id : 定義主鍵 @Column: 定時資料庫表字段 (name:欄位名,nullable:是否允許為null,length:欄位長度) @OneToOne: 定時外來鍵(CascadeType.REFRESH:保持資料完整,查檢關聯表中的資料,FetchType.EAGER:急載入 ) @JoinColumn: 功能與@Column類似。 org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: could not initialize proxy - no Session (through reference chain: com.nenglong.zsedu.ucenter.api.po.educorp.EduCorp["organization"]-> com.nenglong.zsedu.ucenter.api.po.basic.Organization_$$_jvst2c4_12["name"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy - no Session (through reference chain: com.nenglong.zsedu.ucenter.api.po.educorp.EduCorp["organization"]->com.nenglong.zsedu.ucenter.api.po.basic.Organization_$$_jvst2c4_12["name"]) 該報錯的解決方法: 將FetchType.EAGER.LZAY改為 FetchType.EAGER。