3

I'm using servlets with JPA+Hibernate). I don't understand the error, unless I've tried other solutions suggested in this forum. In fact, I don't want to store the UserAccount class as an entity; but I just want to declare it in the Utilisateur class (the Ids of the Utilisateur class are declared in the useraccount class).

My code :

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

    @Id
    private UserAccount userAccount;

    private Civility civility;

    private Address address;

    private Contact contact;

    @Column(name = "sessions")
    private List<String> sessions;

    @Column(name = "particularRules")
    private boolean particularRules;

    public Utilisateur(UserAccount pAccount, Civility pCivility, 
        Address pAddress, Contact pContact, List<String> 
        pSessions, 
        boolean particularRules) {
        this.userAccount = pAccount;
        this.civility = pCivility;
        this.address = pAddress;
        this.contact = pContact;
        this.sessions = pSessions;
        this.particularRules = particularRules;
    }

    public Civility getCivility() {
        return civility;
    }

    public void setCivility(Civility civility) {
        this.civility = civility;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    public boolean isParticularRules() {
        return particularRules;
    }

    public void setParticularRules(boolean particularRules) {
        this.particularRules = particularRules;
    }

    public UserAccount getUserAccount() {
        return userAccount;
    }

    public void setUserAccount(UserAccount userAccount) {
        this.userAccount = userAccount;
    }

    public List<String> getSessions() {
        return sessions;
    }

    public void setSessions(List<String> sessions) {
        this.sessions = sessions;
    }
}

@Embeddable
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class UserAccount implements Serializable {
public UserAccount() {

}

public UserAccount(String pId, String pEmail, String pwsd, Date pCreaDate, Date pLastModDate) {
    this.identifier = pId;
    this.email = pEmail;
    this.password = pwsd;
    this.creationDate = pCreaDate;
    this.lastModificationDate = pLastModDate;
}

@OneToOne(mappedBy = "userAccount", cascade = CascadeType.ALL, 
fetch = FetchType.EAGER, orphanRemoval = true, targetEntity = 
Utilisateur.class)
private Utilisateur user;

@Column(name = "creationDate")
@Temporal(javax.persistence.TemporalType.DATE)
private Date creationDate;

@Column(name = "lastModificationDate")
@Temporal(javax.persistence.TemporalType.DATE)
private Date lastModificationDate;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "identifier", nullable = false)
private String identifier;

@Column(name = "email")
private String email;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "password", nullable = false)
private String password;

public String getIdentifier() {
    return identifier;
}

public void setIdentifier(String identifier) {
    this.identifier = identifier;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public Date getCreationDate() {
    return creationDate;
}

public void setCreationDate(Date creationDate) {
    this.creationDate = creationDate;
}

public Date getLastModificationDate() {
    return lastModificationDate;
}

public void setLastModificationDate(Date lastModificationDate) {
    this.lastModificationDate = lastModificationDate;
}

public Utilisateur getUser() {
    return user;
}

public void setUser(Utilisateur user) {
    this.user = user;
}

}

Patrick Dapa
  • 121
  • 4
  • 15

2 Answers2

3

You must use an Embedded Primary Key.

See the answer to this question here in Stackoverflow How to create and handle composite primary key in JPA.

Best regards!

Andres Bores
  • 212
  • 1
  • 14
  • 2
    You should add at least a small, commented example here. The link might be inavailable some time. – Markus Jun 15 '17 at 14:43
2

It may occur when Embedded Primary Key contains @EmbeddedId. Sub-composite key should be annotated with @Embedded instead.