我得到休眠异常:
列类型错误。找到:位,预期:BOOLEAN DEFAULT TRUE
我有班级用户:
package mypackage;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(name = "USER")
public class User {
@Id
@GeneratedValue
@Column(name = "ID", unique = true, nullable = false)
private Long id;
@Column(name = "STATUS", columnDefinition = "BOOLEAN DEFAULT TRUE",
nullable = false)
private Boolean status = Boolean.TRUE;
// getters and setters
}
如果 Hibernate 在数据库中创建表本身,这将正常工作。什么时候
<property name="hibernate.hbm2ddl.auto">create</property>
或者
<property name="hibernate.hbm2ddl.auto">create-drop</property>
但如果
- 我运行我的程序并允许 Hibernate 创建这个表
- 然后我将值更改
hibernate.hbm2ddl.auto
为验证 然后我用 Hibernate 生成的表再次运行我的程序
<property name="hibernate.hbm2ddl.auto">validate</property>
所以我得到了例外:
org.hibernate.HibernateException:dbtest.user 中列 STATUS 的列类型错误。找到:位,预期:BOOLEAN DEFAULT TRUE
知道 Hibernate 出现这种行为的原因是什么,我该如何解决?
我使用MySQL server 5.1
和Hibernate 4.0.1
。
我的Run
课只有两行:
public class Run {
public static void main(String[] main) {
SessionFactory sessionFactory =
new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
}
}
表结构:
CREATE TABLE USER (
ID BIGINT(20) NOT NULL AUTO_INCREMENT,
STATUS TINYINT(1) NOT NULL DEFAULT 1,
PRIMARY KEY (ID),
UNIQUE INDEX ID (ID)
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM
ROW_FORMAT=DEFAULT
AUTO_INCREMENT=2
我的hibernate.cfg
:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/dbtest</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="connection.pool_size">5</property>
<property name="hibernate.transaction.flush_before_completion">true</property>
<property name="hibernate.transaction.auto_close_session">true</property>
<property name="hibernate.hbm2ddl.auto">validate</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.use_sql_comments">false</property>
<property name="hibernate.connection.charSet">true</property>
<mapping class="mypackage.User"/>
</session-factory>
</hibernate-configuration>
Hibernate 使用类型创建表,TINYINT
我不会干预数据库,也不会手动对表进行任何更改!我只是将其更改hibernate.hbm2ddl.auto
为 the validate
,仅此而已。