Hibernate/persistence without @Id

后端 未结 9 836
梦谈多话
梦谈多话 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:40

    If there's a combination of columns that makes a row unique, model a primary key class around the combination of columns. If there isn't, you're basically out of luck -- but you should reexamine the design of the view since it probably doesn't make sense.

    There are a couple different approaches:

    @Entity
    public class RegionalArticle implements Serializable {
    
        @Id
        public RegionalArticlePk getPk() { ... }
    }
    
    @Embeddable
    public class RegionalArticlePk implements Serializable { ... }
    

    Or:

    @Entity
    public class RegionalArticle implements Serializable {
    
        @EmbeddedId
        public RegionalArticlePk getPk() { ... }
    }
    
    public class RegionalArticlePk implements Serializable { ... }
    

    The details are here: http://docs.jboss.org/ejb3/app-server/HibernateAnnotations/reference/en/html_single/index.html#d0e1517

    Here's an posting that describes a similar issue: http://www.theserverside.com/discussions/thread.tss?thread_id=22638

提交回复
热议问题