【问题标题】:Hibernate/JPA works fine in Eclipse, throws a SchemaManagementException when deployedHibernate/JPA 在 Eclipse 中运行良好,部署时抛出 SchemaManagementException
【发布时间】:2018-02-19 18:16:20
【问题描述】:

我正在使用带有 JPA 的 Hibernate 来连接到 MySql 数据库。数据库是在应用程序启动之前创建的,因此 Hibernate 不会为我构建数据库。此应用程序是一个将部署到 tomcat 的 webapp。

对于一个表,我使用一些泛型来处理一些可能是StringIntegerBoolean 的值。该表是使用以下语句创建的:

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


    【解决方案1】:

    好吧,我显然已经解决了这个问题,但完全忘记了我已经发布了这个问题。所以这就是我做错了。问题基本上来自扩展类的configValue 的处理。这里的每一个都使用自己的类型,但数据库要求类型是varchar(255)。一旦我以以下形式为configValue 提供了列定义:

    @Column(columnDefinition = 'varchar(255)')
    private Integer configValue; // this could be for all the types listed in the question.
    

    问题自己解决了。至于在Eclipse中调试时没有显示自己的原因,我仍然不确定,但是这个修复在调试过程中没有引起任何问题,并且在部署过程中解决了问题。我自己并没有弄清楚这一切,但我不记得是谁帮助了我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 2015-10-26
      • 2017-04-16
      • 1970-01-01
      • 2011-04-12
      相关资源
      最近更新 更多