实体基类:是否启用
@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。