How to @joincolumn key from table a with foreign key in table b in JPA/Spring

184 Views Asked by At

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....

2

There are 2 best solutions below

12
rahulP On BEST ANSWER

This might resolve your issue.

LicenseKey

@Embeddable
@Getter
@Setter
public class LicenseKey implements Serializable {

   private Long l_server_id;

   //
}

License

    @Entity
    @Getter
    @Setter
    public class License {
    
        @EmbeddedId
        private LicenseKey licenseKey;
    
        @OneToOne
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
        @MapsId("l_server_id")
        private Server server;
    
    }

Server

 @Entity
    @Getter
    @Setter
    public class Server {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long serverId;
    
        private String serverName;
    
            @OneToOne(cascade = CascadeType.ALL,mappedBy = "server",fetch = FetchType.EAGER)
        private License license;
    
    }

This is going to create the table structure like this

Hibernate: create table license (l_server_id bigint not null, primary key (l_server_id)) engine=InnoDB
Hibernate: create table server (server_id bigint not null auto_increment, server_name varchar(255), primary key (server_id)) engine=InnoDB
Hibernate: alter table license add constraint FKkope30d18c3etsmqjipi6yrw9 foreign key (l_server_id) references server (server_id)
3
Ramanujan R On

The reason for the issue is in the error message itself.

Column 'serverId' is duplicated

As the Server class has field serverId, the JPA has to map that field to a database column serverId. Now the second field license is a separate field in Server class with two @JoinColumns. This means it has to match to two other columns version and serverId(duplicated). This is not possible, right?

From your code, it is difficult to understand what is your database structure. Could you also provide that to suggest a solution.

From what I am assuming, you may either change to @Id private String id or change the serverId name in the @JoinColumn.