【问题标题】:Set Primary key and Foreign key constraint on a single column having type String in hibernate mapping在休眠映射中对具有字符串类型的单个列设置主键和外键约束
【发布时间】:2023-03-19 08:49:02
【问题描述】:

我有 2 个表 - 请求和子请求。它们都需要进行一对一的映射。在 SubRequest 表中,我需要在同一列(即 RequestID)上有主键和外键。它很容易在数据库中完成。我的应用程序使用休眠。所以在休眠映射文件中我需要做同样的事情。还要求此键(RequestId)必须是 varchar 并由应用程序生成,而不是由 DB 自动生成。我试着从下面做 - http://www.codejava.net/frameworks/hibernate/hibernate-one-to-one-with-primary-key-xml-mapping-example
http://www.mkyong.com/hibernate/hibernate-one-to-one-relationship-example/
但所有这些都具有整数和自动生成的 id。 我无法使它适用于作为字符串的 RequestID。

是否可以在休眠中做到这一点?

【问题讨论】:

    标签: hibernate


    【解决方案1】:

    你可以使用 uuid 策略,它会在你的数据库中生成 uuid 作为字符串 id,你不需要设置 id,hibernate 会处理它。

    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid" )
    private String id;
    

    或使用@Id 而不使用@GeneratedValue

    @Id
    @Column(name = "ROLE_NAME", nullable = false)
    private String roleName;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多