bra*_*ter 60 java annotations hibernate jpa hibernate-annotations
有人可以在hibernate中解释我@MapsId吗?我很难理解它.
如果能用一个例子解释它并且在什么样的用例中它最适用呢?
Man*_*uPK 38
这是Object DB的一个很好的解释.
指定ManyToOne或OneToOne关系属性,该属性提供EmbeddedId主键,EmbeddedId主键中的属性或父实体的简单主键的映射.value元素指定关系属性对应的复合键中的属性.如果实体的主键与关系引用的实体的主键具有相同的Java类型,则不指定value属性.
// 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;
}
Run Code Online (Sandbox Code Playgroud)
在此处阅读API文档.
Ton*_*sic 17
我发现这个注释也很有用:@MapsId在hibernate中,注释将列与另一个表的列相映射.
它也可以用于在两个表之间共享相同的主键.
例:
@Entity
@Table(name = "TRANSACTION_CANCEL")
public class CancelledTransaction {
@Id
private Long id; // the value in this pk will be the same as the
// transaction line from transaction table to which
// this cancelled transaction is related
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID_TRANSACTION", nullable = false)
@MapsId
private Transaction transaction;
....
}
@Entity
@Table(name = "TRANSACTION")
@SequenceGenerator(name = "SQ_TRAN_ID", sequenceName = "SQ_TRAN_ID")
public class Transaction {
@Id
@GeneratedValue(generator = "SQ_TRAN_ID", strategy = GenerationType.SEQUENCE)
@Column(name = "ID_TRANSACTION", nullable = false)
private Long id;
...
}
Run Code Online (Sandbox Code Playgroud)
Jau*_*era 13
恕我直言,最好的思考方式@MapsId是当您需要在 an:m 实体中映射复合键时。
例如,客户可以拥有一名或多名顾问,顾问也可以拥有一名或多名客户:
你的实体将是这样的(伪 Java 代码):
@Entity
public class Customer {
@Id
private Integer id;
private String name;
}
@Entity
public class Consultant {
@Id
private Integer id;
private String name;
@OneToMany
private List<CustomerByConsultant> customerByConsultants = new ArrayList<>();
public void add(CustomerByConsultant cbc) {
cbc.setConsultant(this);
this.customerByConsultant.add(cbc);
}
}
@Embeddable
public class CustomerByConsultantPk implements Serializable {
private Integer customerId;
private Integer consultantId;
}
@Entity
public class CustomerByConsultant{
@EmbeddedId
private CustomerByConsultantPk id = new CustomerByConsultantPk();
@MapsId("customerId")
@JoinColumn(insertable = false, updatable = false)
private Customer customer;
@MapsId("consultantId")
@JoinColumn(insertable = false, updatable = false)
private Consultant consultant;
}
Run Code Online (Sandbox Code Playgroud)
通过这种方式映射,每当您保存顾问时,JPA 都会自动插入Customerid 。所以你不需要手动创建.ConsultantEmbeddableIdCustomerByConsultantPk
| 归档时间: |
|
| 查看次数: |
42064 次 |
| 最近记录: |