不带@Id的Hibernate /持久性 [英] Hibernate/persistence without @Id

查看:117
本文介绍了不带@Id的Hibernate /持久性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库视图,产生一个没有真正主键的结果集。我想用Hibernate / Persistence将这个结果集映射到Java对象上。当然,因为没有PK,我不能用 @Id 来装饰任何字段。

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, because there is no PK, I cannot decorate any field with @Id.

部署时,Hibernate抱怨缺少 @Id 。如何解决这个问题?

When deploying, Hibernate complains about the missing @Id. How can I work around this?

推荐答案

如果有一组列让行成为唯一的列,列的组合。如果没有,那么你基本上运气不好 - 但你应该重新审视视图的设计,因为它可能没有意义。

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 { ... }

详细信息如下: http://docs.jboss.org/ejb3/app-server/HibernateAnnotations / REF erence / en / html_single / index.html#d0e1517

以下是一篇描述类似问题的文章: http://www.theserverside.com/discussions/thread.tss?thread_id=22638

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

这篇关于不带@Id的Hibernate /持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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