我想在mysql数据库中创建一个表,该表有一个布尔列,其值为“活跃”。并且“不活跃”' 。我怎么能这样做?
我的实体类:
@Entity
@Table(name = "organization")
public class OrganizationEntity {
private Long id;
private String nameEntity;
private String provinceEntity;
private String supporterEntity;
private String supporterAddressEntity;
private boolean active;
@Id
@GeneratedValue
@Column(name = "id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "name")
public String getNameEntity() {
return nameEntity;
}
public void setNameEntity(String nameEntity) {
this.nameEntity = nameEntity;
}
@Column(name = "province")
public String getProvinceEntity() {
return provinceEntity;
}
public void setProvinceEntity(String provinceEntity) {
this.provinceEntity = provinceEntity;
}
@Column(name = "supporter_name")
public String getSupporterEntity() {
return supporterEntity;
}
public void setSupporterEntity(String supporterEntity) {
this.supporterEntity = supporterEntity;
}
@Column(name = "supporter_address")
public String getSupporterAddressEntity() {
return supporterAddressEntity;
}
public void setSupporterAddressEntity(String supporterAddressEntity) {
this.supporterAddressEntity = supporterAddressEntity;
}
@Column(name = "active")
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
}
我的组织实体类有一个布尔'活跃的'显示组织处于活动或非活动状态的字段。现在我怎么能在数据库表中有一个列呢?
答案 0 :(得分:1)
从技术上讲,MySQL没有布尔类型。 BOOL和BOOLEAN转换为TINYINT(1)。
值为零被视为false。非零值被认为是真的
您应该能够使用代码中的TINYINT(1)列,因为某些语言将1处理为true,将0处理为false(除非您被其覆盖)。
不确定您使用的语言(C#?),您可以尝试以下方法:
@Column(name = "active")
public boolean isActive() {
return Convert.ToBoolean(active);
}
这是未经测试的,所以试一试。
答案 1 :(得分:0)
您可以简单地使用布尔原始类型(但请确保您有 NOT NULL 列)或可空列的布尔包装器。 JPA 提供程序(Hibernate 或 EclipseLink)足够聪明,可以在幕后进行转换。
用于字段访问类型:
@Basic(optional = false)
@Column(name = "active")
private boolean active;
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
甚至对于属性访问类型:
private boolean active;
@Basic(optional = false)
@Column(name = "active")
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}