Hibernate/persistence without @Id

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

Leave a Comment