JPA只读映射 [英] JPA readonly mapping

查看:146
本文介绍了JPA只读映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当对象中的多个属性映射到数据库中的相同字段但只有一个映射可以写入字段时,Toplink可以使用只读映射。

Toplink can use read-only mappings when multiple attributes in an object map to the same fields in the database but only one of the mappings can write to the field.

JPA有这样的功能,如何编写注释?我有一个@ManyToOne和一个@Column注释需要映射到数据库中的相同字段。

Does JPA has such feature, how to write annotation? I have one @ManyToOne and one @Column annotation which need to map to same field in database.



    @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


列注释和XML元素定义可插入和可更新的选项。这些允许从SQL INSERT或UPDATE语句中省略此列或外键字段。如果表上的约束阻止插入或更新操作,则可以使用这些。如果多个属性映射到同一数据库列,也可以使用它们,例如通过ManyToOne和Id或Basic映射使用外键字段。将insertable和updatable都设置为false,有效地将该属性标记为只读。

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;

应该这样做

这篇关于JPA只读映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆