java - JPA 只读映射

标签 java jpa

当一个对象中的多个属性映射到数据库中的相同字段但只有一个映射可以写入该字段时,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/

相关文章:

java - 间歇性错误 org.hibernate.PersistentObjectException : detached entity passed to persist

java - JPA 和 hibernate - 更新/合并一对多关系

java - 如何从 Java JNI 获取原始类型的类

java - 如何在java中删除字符串 "\u001b[1m"

java - 如何在JNA中制作结构?

java - 如何在 Android 中旋转 Rect 对象

java.sql.SQLException : Column 'JOB_ID' not found

java - @Embeddable 有自己的 id

Java 8 Stream api,用于选择按每个部门的工资排序的前 3 名员工姓名

java - JPA - @Column (unique=true) - 拥有 'unique' 属性的真正意义是什么?