当一个对象中的多个属性映射到数据库中的相同字段但只有一个映射可以写入该字段时,Toplink 可以使用只读映射。
JPA有没有这个功能,注解怎么写?我有一个 @ManyToOne
和一个 @Column
注释需要映射到数据库中的相同字段。
@ManyToOne(optional=false, fetch=FetchType.LAZY)
@JoinColumn(name="USR_ID", referencedColumnName="USER_ID", nullable=false)
private User user;
/** @generated **/
@Column(name="USER_ID", nullable=false, length=30)
private String userId;
最佳答案
来自 here
The Column annotation and XML element defines insertable and updatable options. These allow for this column, or foreign key field to be omitted from the SQL INSERT or UPDATE statement. These can be used if constraints on the table prevent insert or update operations. They can also be used if multiple attributes map to the same database column, such as with a foreign key field through a ManyToOne and Id or Basic mapping. Setting both insertable and updatable to false, effectively mark the attribute as read-only.
所以
@Column(name="USER_ID", nullable=false, length=30,
updatable=false, insertable=false)
private String userId;
应该这样做
关于java - JPA 只读映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30090941/