6

I have this bean:

@Entity
@Table(name = "accesos")
public class Acceso implements Serializable {
    /** */
    @Column(name = "idUser")
    private String idUser;
    /** */
    @ManyToOne
    @JoinColumn(name = "idArea")
    private Area area;
    /** */
    @ManyToOne
    @JoinColumn(name = "idRol")
    private Rol rol;

But I get this error:

Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com...Acceso

How can I set this bean? What I need is based on the user ID get all the ROL-AREA that he has access.

I tried change the @Entity to @Embedded, but when I make the search no result is returned, and even in the log is no SQL sentence executed.

shruti1810
  • 3,920
  • 2
  • 16
  • 28
Alfredo M
  • 568
  • 3
  • 7
  • 26
  • 1
    An entity must always have a primary key; you cannot create an entity without a primary key (id). – Jesper Jun 08 '15 at 15:17
  • 1
    Possible duplicate of [Hibernate/persistence without @Id](https://stackoverflow.com/questions/925818/hibernate-persistence-without-id) – Alex K Feb 26 '18 at 09:20

3 Answers3

6

You have to have an identity for each bean, there is no way around. You can however use a combined key, if none of your fields is unique.

If the combination of all your fields is unique, then try to annotate all fields with @Id. Take as few fields as possible, but as many as required to make the combination unique.

slartidan
  • 20,403
  • 15
  • 83
  • 131
  • 1
    Or all **relevant** fields. As I speculated on another answer, I expect the primary key should be (`idUser`, `area`) in which case you would put `@Id` on both those fields but not on `rol`. See the [Hibernate manual](http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch05.html#d5e2471) for more details. – dcsohl Jun 08 '15 at 15:36
  • Yes, the answer was to set the 3 fields as @Id, because they are foreign keys, thank you! – Alfredo M Jun 08 '15 at 16:24
  • Caution: That they are foreign keys doesn't automatically mean that all three are part of your primary key. Think of the primary as being what actually identifies this particular `Acceso`. Can there be more than one row with the same (user,area) combination? My gut says `rol` should not be part of your primary key but maybe I'm wrong--if you can have multiple `rol`s for a user+area combination, then `rol` is part of your id. – dcsohl Jun 08 '15 at 16:48
  • @Slartidan "You can however use a combined key, if none of your fields is unique", I didn't understand this combined key concept. Can you explain it a bit? My table allows duplicate rows and it doesn't have a primary key or unique key in it. How can i map this table to a JPA entity? – Vipin CP Apr 26 '22 at 12:41
1

JPA Specifications state that all Entities must have an identifier (JSR 317, section 2.4). It can be a single column or a composite key.

You can either put an idAcceso identifier in the Acceso entity or not make Acceso an entity but rather a "component" (which is the purpose of the @Embeddable annotation). Components do not require an ID but cannot be queried separately (i.e. you cannot do select a from Acceso a but rather you need to query for User and then use the accessor method user.getAccesos().

You cannot substitute @Entity with @Embedded in this context.

@Embeddable
public class Acceso {
  // ...
}

@Entity
public class User {
  @Id protected String id;
  // ...

  @ElementCollection
  @CollectionTable(
    name="USER_ACCESSES",
    joinColumns=@JoinColumn(name="USER_ID")
  protected Set<Acceso> accesos = new HashSet<Acceso>();
}
EmirCalabuch
  • 4,756
  • 1
  • 25
  • 20
-2

You don't have an id specified and you MUST so add @Id annotation onto idUser

@Id
@Column(name = "idUser")
private String idUser;
slarge
  • 637
  • 1
  • 7
  • 19
  • 1
    Except that, based on his comments, I doubt he'd want idUser to be the primary key of this table. Sounds more like idUser is a foreign key into a user table. – dcsohl Jun 08 '15 at 15:27
  • Yeah I know but it is that or an EmbeddedId (composite primary key) – slarge Jun 08 '15 at 15:30
  • It *sounds* like it needs to be a composite key; I think a combination of (idUser, area) would do the trick. – dcsohl Jun 08 '15 at 15:36