java - 有人可以在 hibernate 中向我解释@MapsId吗?

标签 java hibernate jpa annotations hibernate-annotations

有人可以向我解释一下 hibernate 中的 @MapsId 吗?我很难理解它。

如果可以用一个例子来解释它会很棒,它最适用于什么样的用例?

最佳答案

这是来自 Object DB 的一个很好的解释.

Designates a ManyToOne or OneToOne relationship attribute that provides the mapping for an EmbeddedId primary key, an attribute within an EmbeddedId primary key, or a simple primary key of the parent entity. The value element specifies the attribute within a composite key to which the relationship attribute corresponds. If the entity's primary key is of the same Java type as the primary key of the entity referenced by the relationship, the value attribute is not specified.

// parent entity has simple primary key

@Entity
public class Employee {
   @Id long empId;
   String name;
   ...
} 

// dependent entity uses EmbeddedId for composite key

@Embeddable
public class DependentId {
   String name;
   long empid;   // corresponds to primary key type of Employee
}

@Entity
public class Dependent {
   @EmbeddedId DependentId id;
    ...
   @MapsId("empid")  //  maps the empid attribute of embedded id
   @ManyToOne Employee emp;
}

阅读 API Docs在这里。

关于java - 有人可以在 hibernate 中向我解释@MapsId吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9923643/

相关文章:

java - 具有附加参数的 JPA 关系 ManyToMany

java - 来自 Oracle DATE 列的 JPA 实体 : get the hours, 分钟和秒

java - 带有 resteasy 的 Spring 启动 - "could not find the type for bean named requestMappingHandlerMapping"错误

java - 使用 Jsoup,我如何获取每个链接中的每个信息?

java - Bean 创建异常。注入(inject) Autowired 依赖项失败

java - SEQUENCE_NEXT_HI_VALUE的含义

java - 使用 @OneToMany 或 @ManyToMany 定位未映射的类 : kg. aladin.jdbc.Student.books[kg.aladin.jdbc.Book]

java - 发布 Java 应用程序

java - 抽象类中的 Rest JSON POST 未编码 unmarshall 通用参数错误 400

没有 Id 的 Java/Hibernate View 实体