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

使用@Embedded列关联时遇映射错误:User类'id'字段无映射

Troubleshooting "No mapping found for field 'id' on class 'App\Entity\User'"

Hey there! Let's dig into why you're hitting this error when generating your database schema. This issue almost always ties back to how you've configured your embeddable ID and the mapping for your User entity. Let's walk through the most common fixes:

1. You forgot the @EmbeddedId annotation on your User's ID field

If you're using an @Embeddable class for your User's primary key, the field in your User entity that holds this embeddable class must be marked with @EmbeddedId (not the regular @Id annotation). Here's what the correct setup looks like:

// Your embeddable ID class
@Embeddable
public class UserId implements Serializable {
    // Add your ID fields here (e.g., private Long userId;)
    // Don't forget getters, setters, equals(), and hashCode() methods!
}

// Your User entity
@Entity
public class User {
    @EmbeddedId // This annotation is critical—don't skip it!
    private UserId id;

    // Your other fields and @ManyToOne associations go here
}

Without @EmbeddedId, JPA has no idea that your id field is supposed to map to the primary key, hence the "no mapping found" error.

2. Your embeddable ID class doesn't implement Serializable

All embeddable primary key classes are required to implement the Serializable interface. This is a JPA specification rule—without it, the persistence provider can't properly handle your primary key. Double-check that your embeddable ID class includes implements Serializable, and that you've correctly overridden equals() and hashCode() (these are needed for JPA to compare primary keys properly).

3. Your @ManyToOne associations are referencing the User's ID incorrectly

If your two associated classes use @ManyToOne to link to User, you need to make sure the join column configuration matches your embeddable ID structure. For example, if your UserId class has a field named userId, your association should explicitly reference that column:

@Entity
public class Post {
    @Id
    private Long postId;

    @ManyToOne
    @JoinColumn(name = "user_id", referencedColumnName = "user_id")
    private User user;

    // Other fields
}

If you skip referencedColumnName or use the wrong column name, it can cause indirect mapping issues that manifest as the error you're seeing.

4. Typos or mismatched field names

Double-check for simple mistakes:

  • Is the field name in your User entity exactly id (the error mentions 'id'—make sure you didn't name it something like userId by accident)?
  • Does the column name in your embeddable ID class match what you're using in your database schema or association mappings?

Start with the first two checks (missing @EmbeddedId and missing Serializable)—those are the most frequent culprits for this error. Once those are sorted, your schema generation should work as expected!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:05:41