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

Spring Boot + MongoDB:嵌入文档字段被重命名问题求助

Fixing Unexpected Field Renaming in Nested Class List Entries

Hey there! That field renaming issue sounds super frustrating—let’s break down what’s likely happening and walk through solutions tailored to your use case.

First, let’s clarify a key point: the @Field annotation you’re using is probably not the right tool for the job here. It’s most commonly used in JPA/Hibernate for defining basic field properties, not for controlling how fields are named during serialization (like to JSON) or ORM mapping. Let’s cover the two most common scenarios you might be dealing with:

Scenario 1: JSON Serialization/Deserialization (Jackson, Gson, etc.)

If you’re converting your List<Nested> to/from JSON, the framework’s default naming strategy is probably overriding your attempts. Here’s how to fix it:

For Jackson (Spring’s default JSON library)

Use @JsonProperty to explicitly define the field name, and ensure the framework can access your field (via getters/setters or allowing direct field access):

public class Nested {
    @JsonProperty("nestedTitle") // Forces the JSON key to be "nestedTitle"
    private String nestedTitle;

    // Add getters and setters (or use Lombok's @Getter/@Setter)
    public String getNestedTitle() {
        return nestedTitle;
    }

    public void setNestedTitle(String nestedTitle) {
        this.nestedTitle = nestedTitle;
    }
}

Also, check your global Jackson configuration—if you’ve set a naming strategy like SNAKE_CASE, it’ll automatically convert camelCase to snake_case. You can either remove this global setting or let @JsonProperty override it. For Spring Boot, look for this in your application.yml/application.properties:

spring:
  jackson:
    property-naming-strategy: SNAKE_CASE # Remove this if you don't want auto-renaming

For Gson

Use @SerializedName instead of @JsonProperty to lock in the field name:

public class Nested {
    @SerializedName("nestedTitle")
    private String nestedTitle;

    // Getters/setters are optional here if Gson has permission to access private fields (default behavior)
}

Scenario 2: ORM Database Mapping (JPA/Hibernate)

If you’re storing the List<Nested> in a database, you’re likely using the wrong annotation. Here’s how to map fields correctly:

  1. Use @Column for database column names: @Field doesn’t control the column name—@Column does.
  2. Mark nested classes as @Embeddable if they’re value objects embedded in your main entity.
  3. Map the list properly with @ElementCollection (for simple nested objects) or @OneToMany (for full entities).

Example code:

// Nested class as an embeddable value object
@Embeddable
public class Nested {
    @Column(name = "nestedTitle") // Explicitly sets the database column name
    private String nestedTitle;

    // Getters and setters
    public String getNestedTitle() {
        return nestedTitle;
    }

    public void setNestedTitle(String nestedTitle) {
        this.nestedTitle = nestedTitle;
    }
}

// Main document entity
@Entity
public class MainDocument {
    @Id
    private Long id;

    @ElementCollection
    @CollectionTable(name = "main_nested_entries", joinColumns = @JoinColumn(name = "main_document_id"))
    private List<Nested> nestedList;

    // Getters and setters for id and nestedList
}

Also, check your JPA naming strategy configuration—if it’s set to convert camelCase to snake_case globally, it might override @Column. Adjust this in your Spring Boot config if needed:

spring:
  jpa:
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl # Disables auto-renaming

Additional Checks

  • Double-check annotation imports: Make sure you’re importing the correct annotation for your framework (e.g., com.fasterxml.jackson.annotation.JsonProperty for Jackson, jakarta.persistence.Column for JPA). Importing the wrong package will make the annotation do nothing.
  • Check custom serializers/deserializers: If you’ve written custom logic to handle Nested class serialization, it might be overriding your annotations. Verify that this logic preserves your field names.
  • Field access permissions: Ensure private fields are accessible to your framework—either via getters/setters, or by configuring the framework to use reflection to access private fields (most do this by default, but custom configs might disable it).

That should cover the most common causes of this issue. Try these steps based on your specific use case, and you should see those fields stop getting renamed and start accepting values correctly!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:58:06