how do I link via @joincolumn an already existing key from table A with a field in table B.
In detail I have a class server with its key serverId. It has a one-to-one relation to a class license linked by the serverId from the embedded Class LicenseKey. Now the problem is, that my serverId is already defined as a ID and I got the error:
org.hibernate.MappingException: Column 'serverId' is duplicated in mapping for entity 'com.example.jpa.model.Server' (use '@Column(insertable=false, updatable=false)' when mapping multiple properties to the same column)
import jakarta.persistence.*;
@Entity
@Table(name = "server")
public class Server {
@Id
private String serverId;
@OneToOne
@JoinColumn(name="version", referencedColumnName="version")
@JoinColumn(name="serverId", referencedColumnName="serverId")
private License license;
// getter and setter
}
@Entity
@Table(name = "license")
public class License {
@EmbeddedId
private LicenseKey id;
// ...
}
@Embeddable
public class LicenseKey implements Serializable {
String version;
String serverId;
// ...
}
After that I tried to use @JoinColumn(name="serverId", insertable=false, updateable=false, referencedColumnName="serverId") but it does not work and I got more errors. Maybe it is easy and someone can help me here....
This might resolve your issue.
LicenseKey
License
Server
This is going to create the table structure like this