java 如何在 Hibernate 中设置只读列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14626341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:52:59  来源:igfitidea点击:

How to set read-only columns in Hibernate?

javadatabasehibernateormrelational-database

提问by cloudy_weather

I don't know how to force read-only columns in Hibernate.

我不知道如何在 Hibernate 中强制只读列。

I would like to set idgroup as read only column. Even if I set insertable=falseand updatable=false, in the hibernate SQL I can read:

我想将 idgroup 设置为只读列。即使我设置了insertable=falseand updatable=false,在休眠 SQL 中我也可以阅读:

Hibernate: insert into groups (description, name, account_idaccount, idgroup) values (?, ?, ?, ?) 

but I would like to obtain:

但我想获得:

insert into groups (description, name, account_idaccount) values (?, ?, ?) 

Here are my classes:

这是我的课程:

@Entity
@Table(name = "groups")
public class Group implements java.io.Serializable {

private static final long serialVersionUID = -2948610975819234753L;
private GroupId id;
private Account account;
private String name;
private String description;

@EmbeddedId
@AttributeOverrides({@AttributeOverride(name = "idgroup", column = @Column(name = "idgroup", insertable = false)),
        @AttributeOverride(name = "accountIdaccount", column = @Column(name = "account_idaccount", nullable = false))})
public GroupId getId() {
    return id;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "account_idaccount", nullable = false, insertable = false, updatable = false)
public Account getAccount() {
    return account;
}

@Column(name = "description", length = 512)
public String getDescription() {
    return description;
}


@Column(name = "name", nullable = false, length = 128)
public String getName() {
    return name;
}
..
}
@Embeddable
public class GroupId implements java.io.Serializable {

private int idgroup;
private int accountIdaccount;

@Column(name = "idgroup", insertable= false, updatable= false)
public int getIdgroup() {
    return this.idgroup;
}


@Column(name = "account_idaccount", nullable = false)
public int getAccountIdaccount() {
    return this.accountIdaccount;
}
..
}

I would like to have a read only column for idgroup because I can exploit the id auto-generation of the DBMS, I do not want to use the key auto-generation in Hibernate because it is not cluster-safe.

我想为 idgroup 设置一个只读列,因为我可以利用 DBMS 的 id 自动生成,我不想在 Hibernate 中使用密钥自动生成,因为它不是集群安全的。

采纳答案by cloudy_weather

The solution is using wrapper classes, such as Integer, not primitivetypes like int. When Hibernate will get a null pointer it will delegate the generation of the id to the DBMS.

解决方案是使用包装类,例如Integer而不是int. 当 Hibernate 得到一个空指针时,它将把 id 的生成委托给 DBMS。

回答by Bob Flannigon

I think you can mark an @Columnannotation as updatable=false

我认为您可以将@Column注释标记为updatable=false

回答by J. Jerez

Easy: @Column(insertable = false, updatable = false)

简单:@Column(可插入 = 假,可更新 = 假)

回答by jDub9

I was able to achieve this by using 2 DAO objects. I know it may not be preferred, but I didn't see another way around it. Setting the field to insertable=falseand updatable=falsedo not remove the columns from the insert SQL so Postgres will not invoke the defaultaction to populate those column values.

我能够通过使用 2 个 DAO 对象来实现这一点。我知道它可能不是首选,但我没有看到其他方法。将字段设置为insertable=false并且updatable=false不从插入 SQL 中删除列,因此 Postgres 不会调用default操作来填充这些列值。

   // GroupWrite

   @Entity
   @Table(name = "group")
   public class GroupWrite {

      @Id
      @Column(name = "account")
      private Account account;

      @Column(name = "name")
      private String name;

      @Column(name = "description")
      private String description;


      /* Getters and Setters */
  }

--

——

   // GroupRead

   @Entity
   @Table(name = "group")
   public class GroupRead {

      @EmbeddedId
      @Column(name = "id")
      private GroupId id;

      @Column(name = "account")
      private Account account;

      @Column(name = "name")
      private String name;

      @Column(name = "description")
      private String description;


      /* Getters and Setters */
  }

Then you need to use the GroupReadwhen using selectqueries and GroupWritewhen doing insertor updatequeries.

然后你需要使用GroupReadwhen using selectqueries 和GroupWritewhen doinsertupdate查询。

If you want to use attributes from both in a common way you can implement an interface that describes the overlapping fields.

如果您想以通用方式使用两者的属性,您可以实现一个描述重叠字段的接口。

   public interface Group {

        Account getAccount();

        void setAccount(Account account);
   }

   public class GroupWrite implements Group {...}

   public class GroupRead implements Group {...}

Hope this helps.

希望这可以帮助。

回答by Rakesh

@ReadOnlyProperty annotation will help you in this case.

在这种情况下,@ReadOnlyProperty 注释将为您提供帮助。