【发布时间】:2018-02-19 18:16:20
【问题描述】:
我正在使用带有 JPA 的 Hibernate 来连接到 MySql 数据库。数据库是在应用程序启动之前创建的,因此 Hibernate 不会为我构建数据库。此应用程序是一个将部署到 tomcat 的 webapp。
对于一个表,我使用一些泛型来处理一些可能是String、Integer 或Boolean 的值。该表是使用以下语句创建的:
CREATE TABLE IF NOT EXISTS `registry` (
`DTYPE` varchar(31) NOT NULL,
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`configName` varchar(255) NOT NULL,
`label` varchar(255) NOT NULL,
`configValue` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
它正在处理的类是一个抽象类基础,其中包含三个扩展抽象的类。像这样:
@Entity
@Table(name = "registry")
public abstract class Config<T> implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true, nullable = false)
private String configName;
private String label;
// standard getters and setters for the above fields.
public abstract void setConfigValue(T value);
public abstract T getConfigValue();
}
@Entity
public class BooleanConfig extends Config<Boolean>{
private Boolean configValue;
public void setConfigValue(Boolean configValue){
this.configValue = configValue;
}
public Boolean getConfigValue(){
return this.configValue;
}
}
@Entity
public StringConfig extends Config<String>{
private String configValue;
public void setConfigValue(String configValue){
this.configValue = configValue;
}
public String getConfigValue(){
return this.configValue;
}
}
@Entity
public IntegerConfig extends Config<Integer>{
// and similar as the other two but with Integer.
}
所有这些类都列在我的 persistence.xml 中,当我从 Eclipse 运行应用程序进行调试时,一切都按预期工作,并且按预期写入和编辑值。我的问题是一旦我编译了war文件并将其上传到Tomcat。将webapp部署到Tomcat时,由于数据库导致应用程序启动过程中出现错误。
SchemaManagementException: Schema-validation: wrong column type encountered in column [configValue] in table [registry]; found [varchar (Types#VARCHAR)], but expecting [bit (Types#BOOLEAN)]
现在我认为问题的答案是我需要进入每个扩展类并将 configValue 列映射到特定的数据类型。我的问题是为什么从我的 IDE 运行时没有收到异常?
【问题讨论】:
标签: java eclipse hibernate tomcat jpa