0

So consider the following 2 tables:

table: User

id (pk)
...

and table UserProfile:

UserProfile
user_id(pk, and fk from User.id. fk is named profile_user_fk)
...

given this tables, I have the entity classes:

@Entity
@Table(name="User")
public class User implements Serializable {

    private int id;
    private UserProfile profile;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, unique = true)
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
    @OneToOne(mappedBy = "user")
    public UserProfile getProfie() {
        return profile;
    }

    public void setProfile(UserProfile  p) {
        profile = p;
    }
...
}

And the User class:
@Entity
@Table(name="UserProfile")
public class UserProfile implements Serializable {

    private User user;

    @OneToOne
    @PrimaryKeyJoinColumn(name="profile_user_fk")
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
...
}

I don't want to add an extra column in UserProfile because I think this column is meaningless. User.id should be sufficient to indicate the identity of UserProfile records.

So I supposed the @OneToOne annotations will tell hibernate user is a pk and also fk and should refer to User.id for its own id; however executing the code shows:

org.hibernate.AnnotationException: No identifier specified for entity: xxx.UserProfile

Apparently what I thought was wrong - but I've no idea what can I do to fix it without altering the schema.

Any helps please. Thanks!

jamesdeath123
  • 4,268
  • 11
  • 52
  • 93
  • Error itself says `org.hibernate.AnnotationException: No identifier specified for entity: xxx.UserProfile` you need to add primary key column in `UserProfile` class. – OO7 Nov 06 '14 at 07:19

1 Answers1

1

The error

No identifier specified for entity: xxx.UserProfile

says

In your Entity class (UserProfile), you have not defined a primary key. You must specify 
either @Id annotation or an @EmbeddedId annotation. Bcoz, every class defined as Entity
with @Entity annotation, needs an @Id or @EmbeddedId property.

Modify your UserProfile class as below :-

@Entity
@Table(name="UserProfile")
public class UserProfile implements Serializable {

    private long uProfileId;
    private User user;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "uProfileId", nullable = false, unique = true)
    public long getUProfileId() {
        return uProfileId;
    }

    public void setUProfileId(long uProfileId) {
        this.uProfileId = uProfileId;
    }

    @OneToOne
    @PrimaryKeyJoinColumn(name="profile_user_fk")
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    ...
}

Read: @Entity & @Id and @GeneratedValue Annotations


Entity Class without Primary Key :

If you don't want to add primary key in UserProfile table then you can use @Embedded & @Embeddable annotations or <component> tag in xml mapping. For more explanation on this look at below posts:-

  1. Using <component> or @Embedded & @Embeddable annotations
  2. Hibernate and no PK
  3. Hibernate/persistence without @Id
Community
  • 1
  • 1
OO7
  • 2,785
  • 1
  • 21
  • 33
  • so this means I have to also add a column in UserProfile, which I think is useless but only to comply with Hibnerate? As I said the fk to user.id should be sufficient as a pk of UserProfile too. Is it a way of working it out without changing db schema? Thanks! – jamesdeath123 Nov 06 '14 at 14:49
  • Since you have created `UserProfile` class as `Entity` class, you must have to add `Id` in it. If you don't want to alter ur database table then, you can use **@Embedded** & **@Embeddable** annotations or **``** tag in case of mapping with xml file. See my updated answer. – OO7 Nov 07 '14 at 09:16