I need to have a composite ID in my entity, consisting of two integers that one must be generated. I have been exploring google for several days and I couldn't find anything constructive about composite keys and generating value. I cannot fix my model in order not to use composite key. My database sometimes imports "Client" records from another database so that is the only way to ensure uniqueness.
This is how my entity class looks like:
Code:
@Entity
@Table(name = "Klienci")
public class Client implements Serializable{
private static final long serialVersionUID = 1L;
private Integer id;
private Salon salon;
private Long pesel;
public Client() {
}
public Client(Long pesel, Salon salon) {
this.pesel = pesel;
this.salon = salon;
}
@Id
@Column(name = "Klient_ID")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Id
@ManyToOne
@JoinColumn(name = "Salon_fk")
public Salon getSalon() {
return salon;
}
public void setSalon(Salon salon) {
this.salon = salon;
}
@Column(name = "PESEL")
public Long getPesel() {
return pesel;
}
public void setPesel(Long pesel) {
this.pesel = pesel;
}
When I call save(client) I get following exception:
Code:
Exception in thread "AWT-EventQueue-0" org.springframework.orm.hibernate3.HibernateSystemException: IllegalArgumentException occurred while calling setter of mbrnwsk.ClientsRegister.model.client.Client.id; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of mbrnwsk.ClientsRegister.model.client.Client.id
When I use the @EmbeddedId approach instead of multi @Id annotations I don't get this exception and object is persisted correctly, but generated id is not being set so I cannot use this object to persist another object that is parent of client.
Maybe should I write custom generator that returns composite-id instead of single value? But I have no idea how to do it.
I would be very grateful for solution as I'm stuck with this problem for several days.