Hibernate无法创建表并抛出SQL语法异常问题求助
Let's walk through why you're hitting this SQLGrammarException and fix it step by step. The core issue is that Hibernate can't find the users table, and your attempts to auto-create it via hbm2ddl aren't working—here's why and how to fix it:
1. You're Using the Wrong HBM2DDL Configuration Key
The biggest mistake here is your hibernate.hbm2ddl property. The correct configuration key for auto-DDL behavior is hibernate.hbm2ddl.auto (you're missing the .auto suffix). Without this, Hibernate ignores your create setting entirely, so it never tries to create the users table.
Fix this in your XML config:
<property name="hibernate.hbm2ddl.auto">create</property>
- Use
createfor testing (it drops and recreates tables every time) - Switch to
updateonce you're ready to preserve data
2. Database Name Case Sensitivity Mismatch
Your config uses botSender as the database name in the JDBC URL, but the error says Table 'botsender.users' doesn't exist. MySQL is case-sensitive on table/database names in Linux environments (case-insensitive on Windows by default).
Fix this in one of two ways:
- Option 1: Match the database name exactly in your URL (e.g., if your actual database is
botsender, change the URL tojdbc:mysql://localhost:3306/botsender) - Option 2: Add a parameter to force case insensitivity (add this to your JDBC URL):
Full URL example:?lower_case_table_names=1&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/botSender?lower_case_table_names=1&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC</property>
3. Verify Your User Entity Annotations
Make sure your User entity is properly annotated so Hibernate knows to map it to the users table:
@Entity @Table(name = "users") // Explicitly specify the table name public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) // Use a valid generation strategy private Long id; @Column(name = "first_name") // Match column names if needed (optional if using camelCase to snake_case auto-mapping) private String firthName; // Note: You have a typo here—"firthName" should be "firstName"! // Rest of your fields, getters, and setters }
Also, fix the typo in firthName to firstName—this could cause column mapping issues later.
4. Use the Correct Hibernate Dialect
For MySQL 5.x or later, use a dialect that supports InnoDB (your current MySQLDialect is too generic). Update your dialect property:
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
If you're using MySQL 8+, use org.hibernate.dialect.MySQL8InnoDBDialect instead.
5. Ensure the Database Exists
Hibernate won't create the database for you—only tables. Make sure the botsender (or botSender) database exists in MySQL. If not, create it manually first, or add createDatabaseIfNotExist=true to your JDBC URL:
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/botSender?createDatabaseIfNotExist=true&lower_case_table_names=1&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC</property>
Final Working Configuration Example
Here's your updated config with all fixes applied:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.id.new_generator_mappings">false</property> <property name="hibernate.hbm2ddl.auto">create</property> <!-- Fixed key --> <property name="hibernate.bytecode.use_reflection_optimizer">false</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">admin12345</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/botSender?createDatabaseIfNotExist=true&lower_case_table_names=1&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC</property> <!-- Fixed URL --> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- Updated dialect --> <property name="show_sql">true</property> <mapping class="com.bot.entity.User"></mapping> <mapping class="com.bot.entity.Role"></mapping> <mapping class="com.bot.entity.Chanel"></mapping> <mapping class="com.bot.entity.MessageToChanel"></mapping> <mapping class="com.bot.entity.MessageToUser"></mapping> <mapping class="com.bot.entity.Status"></mapping> </session-factory> </hibernate-configuration>
After applying these fixes, run your test again—Hibernate should now create the users table automatically, and your save operation should work without the SQLGrammarException.
内容的提问来源于stack exchange,提问作者BruceVVayne

