简体   繁体   中英

Hibernate/JPA works fine in Eclipse, throws a SchemaManagementException when deployed

I'm using Hibernate with JPA to connect to a MySql database. The database is created before the application launch so Hibernate is not building the database for me. This application is a webapp that is to be deployed to tomcat.

For one table I am using some Generics to handle some values that may either be String , Integer or Boolean . The table is created with this statement:

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;

The classes it is handling are an abstract class base with three classes that extends the abstract. Like this:

@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. 
}

All these classes are listed in my persistence.xml and when I run the application from Eclipse for debugging, everything works as expected and the values are written and edited as I would expect them to be. My problem is once I compile the war file and upload it to Tomcat. When the webapp is deployed to Tomcat there is an error during the start up of the application caused by the database.

SchemaManagementException: Schema-validation: wrong column type encountered in column [configValue] in table [registry]; found [varchar (Types#VARCHAR)], but expecting [bit (Types#BOOLEAN)]

Now I figure the answer to the problem is that I will need to go into each of the extending classes and map the configValue column to a specific data type. My question is why do I not receive the exception when running from my IDE?

Ok I apparently fixed this problem and totally forgot that I had posted this question. So here's what I was doing wrong. The problem basically comes from the handling of the configValue of the extending classes. Each one here used its own type but the database requires that the type is varchar(255) . Once I provided a Column Definition for the configValue in the form of:

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

the problem solved itself. As for the reason it didn't show itself while debugging in Eclipse, I'm still not sure, but this fix caused no problems during debugging and solved the problem during deployment. I didn't figure this all out myself, but I can't remember who helped me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM