Reputation: 568
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.
Upvotes: 6
Views: 31315
Reputation: 4844
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>();
}
Upvotes: 1
Reputation: 21566
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.
Upvotes: 6
Reputation: 644
You don't have an id specified and you MUST so add @Id annotation onto idUser
@Id
@Column(name = "idUser")
private String idUser;
Upvotes: -2