Hibernate/persistence without @Id

后端 未结 9 801
梦谈多话
梦谈多话 2020-11-28 09:07

I have a database view that yields a result set that has no true primary key. I want to use Hibernate/Persistence to map this result set onto Java objects. Of course, becaus

相关标签:
9条回答
  • 2020-11-28 09:59

    According to Hibernate user guide, using the pk is recommended so even if you are working with the existing project that doesn't contain the pk in the database side, you can add the pk column to the related database table and map it with the Java entities.

    The hibernate recommendation was like that:

    We recommend that you declare consistently-named identifier attributes on persistent classes and that you use a wrapper (i.e., non-primitive) type (e.g. Long or Integer).

    For more detail about pk and Hibernate mechanism, please visit here

    0 讨论(0)
  • 2020-11-28 10:01

    For each entity, you must designate at least one of the following:

    • one @Id
    • multiple @Id and an @IdClass (for a composite primary key)
    • @EmbeddedId

    so maybe you can create a composite primary key, containing multiple fields?

    0 讨论(0)
  • 2020-11-28 10:03

    Modify your select used for view creation:

    SELECT
       ROWNUM ID, -- or use nextval of some sequence
       -- other columns
    FROM
      TABLE
    

    and map "ID" as primary key.

    0 讨论(0)
提交回复
热议问题