Spring Boot中使用@Column重复定义同一列的问题
Alright, let's figure out why this build failure is happening and fix it up step by step.
核心原因
The root issue here is duplicate column mapping. You've already mapped the docId field in your Document class to the database column docid using @Column(name = "docid"). Then you added another field docIds in the Test class with the exact same @Column(name = "docid") annotation.
Hibernate (and JPA in general) doesn't allow multiple entity properties to map to the same database column unless you're using very specific, intentional configurations. When it detects this conflict, it throws a MappingException because it can't resolve how to sync two separate fields with a single column value during CRUD operations—like which value to use when inserting, or which field to populate when querying.
针对性解决方案
Let's break this down based on your actual use case:
1. If Test is a separate entity class mapping to the same table as Document
Option 1: Map
Testto its own table
Add a@Tableannotation to theTestclass to specify a unique table name, so itsdocIdsfield maps to a column in its own table instead of conflicting withDocument:@Entity @Table(name = "test_documents") // Replace with your actual table name public class Test { @Column(name = "docid") protected String docIds; // Other fields and methods }Option 2: Change the column mapping for
docIds
Modify the@Columnname fordocIdsto point to a different column in the same table (make sure the column exists in your database, or adjust your migration scripts accordingly):@Column(name = "doc_ids") // Unique column name matching your database schema protected String docIds;Option 3: Mark
docIdsas non-persistent
IfdocIdsdoesn't need to be stored in the database at all, remove the@Columnannotation and add@Transientto tell JPA to ignore this field for persistence:@Transient protected String docIds;
2. If you accidentally added duplicate fields in the same entity (e.g., typo where Test was meant to be Document)
- Remove one of the duplicate fields: Delete either
docIdordocIdsdepending on which one you don't need for your business logic. - Adjust for intentional aliasing (rare use case): If you need both fields for some reason (like exposing an alias to external systems), use property-level access to map them to the same column. Note this requires careful syncing of values to avoid inconsistencies:
protected String docId; @Transient protected String docIds; @Column(name = "docid") public String getDocId() { return docId; } public void setDocId(String docId) { this.docId = docId; this.docIds = docId; // Keep values in sync } public String getDocIds() { return docIds; } public void setDocIds(String docIds) { this.docIds = docIds; this.docId = docIds; // Keep values in sync }
Quick Troubleshooting Checks
- Double-check the
@Tableannotations on bothDocumentandTestclasses to confirm if they're targeting the same database table. - Scan all
@Column(name)values across your entities to ensure no duplicates pointing to the same column in the same table.
内容的提问来源于stack exchange,提问作者Willingtolearn

