剛剛遇見一個很奇怪的問題,是因 Hibernate SQL 方言配置不正確引起的,之前還真不知道后果會這么嚴(yán)重,花了點時間才找出問題的所在。
這個異常不好排,因為后臺沒有拋出任何的異常信息,而且后臺輸出打印的信息很正常 !
實體類 :
package net.yeah.fancydeepin.po;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name = "person")
public class Person {
private Integer id;
private String name;
@Id
@GenericGenerator(name = "idGenerator", strategy = "native")
@GeneratedValue(generator = "idGenerator")
public Integer getId() {
return id;
}
@Column(length = 18)
public String getName() {
return name;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
hibernate.cfg.xml 清單 :
1
2 <hibernate-configuration>
3
4 <session-factory>
5
6 <!-- Database connection settings -->
7 <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
8 <property name="connection.url">jdbc:mysql://localhost:3306/temp</property>
9 <property name="connection.username">username</property>
10 <property name="connection.password">password</property>
11
12 <!-- SQL dialect -->
13 <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
14
15 <!-- Enable Hibernate's automatic session context management -->
16 <property name="current_session_context_class">thread</property>
17
18 <!-- Echo all executed SQL to stdout -->
19 <property name="show_sql">true</property>
20 <property name="format_sql">true</property>
21
22 <mapping class="net.yeah.fancydeepin.po.Person"/>
23
24 </session-factory>
25
26 </hibernate-configuration>
27
2 <hibernate-configuration>
3
4 <session-factory>
5
6 <!-- Database connection settings -->
7 <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
8 <property name="connection.url">jdbc:mysql://localhost:3306/temp</property>
9 <property name="connection.username">username</property>
10 <property name="connection.password">password</property>
11
12 <!-- SQL dialect -->
13 <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
14
15 <!-- Enable Hibernate's automatic session context management -->
16 <property name="current_session_context_class">thread</property>
17
18 <!-- Echo all executed SQL to stdout -->
19 <property name="show_sql">true</property>
20 <property name="format_sql">true</property>
21
22 <mapping class="net.yeah.fancydeepin.po.Person"/>
23
24 </session-factory>
25
26 </hibernate-configuration>
27
Junit 測試 :
@Test
public void createTable(){
new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);
}
后臺輸出 :
drop table if exists person
create table person (
id integer not null auto_increment,
name varchar(18),
primary key (id)
)
從后臺輸出的信息看來,沒有任何的不妥,建表語句是正確的,但去數(shù)據(jù)庫那里無論怎么個刷新法,就是沒有 person 這張表,這個就很郁悶了 !
由于測試的時候,對于配置文件,一般都是采取 Copy 改的方式,這才引起的問題,回過頭看 hibernate.cfg.xml 清單中的第 13 行,當(dāng)注意到了
才知道自己犯了很低級的錯誤,第 13 配置的 Hibernate SQL 是 SQL Server 的方言,而我現(xiàn)在用的是 MySQL 數(shù)據(jù)庫,將這行配置改成 :
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
再次運行 Junit 測試,后臺打印輸出一模一樣的信息,不同的是,這回數(shù)據(jù)庫中的 person 表出來了,它是正確建的表。
最后附上 :
Hibernate SQL方言 (hibernate.dialect
) 表
RDBMS | Dialect |
---|---|
DB2 | org.hibernate.dialect.DB2Dialect |
DB2 AS/400 | org.hibernate.dialect.DB2400Dialect |
DB2 OS390 | org.hibernate.dialect.DB2390Dialect |
PostgreSQL | org.hibernate.dialect.PostgreSQLDialect |
MySQL | org.hibernate.dialect.MySQLDialect |
MySQL with InnoDB | org.hibernate.dialect.MySQLInnoDBDialect |
MySQL with MyISAM | org.hibernate.dialect.MySQLMyISAMDialect |
Oracle (any version) | org.hibernate.dialect.OracleDialect |
Oracle 9i | org.hibernate.dialect.Oracle9iDialect |
Oracle 10g | org.hibernate.dialect.Oracle10gDialect |
Sybase | org.hibernate.dialect.SybaseDialect |
Sybase Anywhere | org.hibernate.dialect.SybaseAnywhereDialect |
Microsoft SQL Server | org.hibernate.dialect.SQLServerDialect |
SAP DB | org.hibernate.dialect.SAPDBDialect |
Informix | org.hibernate.dialect.InformixDialect |
HypersonicSQL | org.hibernate.dialect.HSQLDialect |
Ingres | org.hibernate.dialect.IngresDialect |
Progress | org.hibernate.dialect.ProgressDialect |
Mckoi SQL | org.hibernate.dialect.MckoiDialect |
Interbase | org.hibernate.dialect.InterbaseDialect |
Pointbase | org.hibernate.dialect.PointbaseDialect |
FrontBase | org.hibernate.dialect.FrontbaseDialect |
Firebird | org.hibernate.dialect.FirebirdDialect |