You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hibernate @Generated注解失效:MySQL拼接学生全名运行报错求助

Hey there! Let's troubleshoot the error you're hitting when trying to use MySQL's CONCAT to generate a fullName for your Student entity with Hibernate. I've dealt with similar issues before, so here are the most common fixes based on how you're setting this up:

Common Solutions & Fixes

1. Use @Formula for a computed (non-persisted) fullName

If you don't need fullName stored in the database and just want it calculated on-the-fly when querying, @Formula is the way to go. Hibernate will inject the raw SQL into your queries automatically.

Correct Entity Example:

@Entity
@Table(name = "students")
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String firstName; // Maps to DB column `first_name` (default naming strategy)
    private String lastName;  // Maps to DB column `last_name`
    
    // Use MySQL's CONCAT with DB column names, not entity property names
    @Formula("CONCAT(first_name, ' ', last_name)")
    private String fullName;
    
    // Constructors, getters, setters omitted
}

Key Note: Make sure you use the actual database column names (e.g., first_name) in the @Formula string, not the camel-case entity property names. This is a super common mistake that causes "unknown column" errors.

2. Persist fullName to the database (auto-populate on save/update)

If you need fullName stored in your MySQL table, you have two reliable options:

Option A: Java-level pre-processing with @PrePersist/@PreUpdate

This keeps logic in your entity and avoids database-specific function dependencies:

@Entity
@Table(name = "students")
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String firstName;
    private String lastName;
    
    @Column(name = "full_name")
    private String fullName;
    
    // Auto-update fullName before saving or updating
    @PrePersist
    @PreUpdate
    public void syncFullName() {
        this.fullName = String.join(" ", firstName, lastName);
    }
    
    // Constructors, getters, setters omitted
}

Option B: Database-level transformation with @ColumnTransformer

If you specifically want to use MySQL's CONCAT during insert/update operations:

@Entity
@Table(name = "students")
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String firstName;
    private String lastName;
    
    @Column(name = "full_name")
    @ColumnTransformer(
        write = "CONCAT(?, ' ', ?)", // Uses first and last name values for the DB insert
        read = "full_name"
    )
    private String fullName;
    
    // Constructors, getters, setters omitted
}

Common Error Causes to Check

  • Wrong column names in @Formula: If you see an error like Unknown column 'firstName' in 'field list', you used the entity's camel-case property name instead of the DB's snake-case column name.
  • Trying to use MySQL CONCAT in HQL/JPQL: If you're writing a query manually, use HQL's built-in CONCAT function (it works across databases) or use a native MySQL query with createNativeQuery().
  • Uninitialized fullName with non-null DB constraint: If fullName is a required DB column but you didn't set up auto-population logic, you'll get a "not null constraint violated" error.

内容的提问来源于stack exchange,提问作者Pankaj Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:13:29