我正在使用 Hibernate 和 JPA 连接到 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 运行时我没有收到异常?
最佳答案
好吧,我显然解决了这个问题,完全忘记了我已经发布了这个问题。所以这就是我做错的地方。问题基本上来自对扩展类的 configValue
的处理。这里的每个都使用自己的类型,但数据库要求类型为 varchar(255)
。一旦我以以下形式为 configValue
提供了列定义:
@Column(columnDefinition = 'varchar(255)')
private Integer configValue; // this could be for all the types listed in the question.
问题自行解决。至于在Eclipse中调试时不显示的原因,我还不清楚,但这个修复在调试时没有出现问题,在部署时解决了问题。我自己并没有弄清楚这一切,但我不记得是谁帮助了我。
关于java - Hibernate/JPA 在 Eclipse 中运行良好,部署时抛出 SchemaManagementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46156818/