Home > Software design >  Repeated column in mapping for entity in @OneToOne
Repeated column in mapping for entity in @OneToOne

Time:11-09

I have two tables:

CREATE TABLE users
(
    id              INTEGER PRIMARY KEY AUTO_INCREMENT,
    username        VARCHAR(50) NOT NULL UNIQUE,
    password        VARCHAR(68) NOT NULL,
    oldPassword     VARCHAR(68),
    enabled BOOLEAN NOT NULL
);

and

CREATE TABLE authorities (
                             id INTEGER PRIMARY KEY AUTO_INCREMENT,
                             username VARCHAR(50) NOT NULL,
                             authority VARCHAR(50) NOT NULL,
                             FOREIGN KEY (username) REFERENCES users(username)
);

CREATE UNIQUE INDEX ix_auth_username on authorities (username,authority);

Unfortunatelly instead of joining with authority_id which should be in users table I just have username which is the same in both tables.

In models I tried (ommited getters and setters):

@Entity(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String username;
    private String password;
    private String oldPassword;
    private boolean enabled;
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "username", referencedColumnName = "username")
    private Authorities authority;
}

and

@Entity
public class Authorities {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String username;
    private String authority;
    @OneToOne(mappedBy = "authority")
    private User user;
}

but then I have an error: Repeated column in mapping for entity column: username (should be mapped with insert="false" update="false")

CodePudding user response:

The message is clear: you have a repeated column in the mapping, and a column "username" in both tables has repeated. Instead of this, you should use this :

@OneToOne(cascade = CascadeType.ALL) @JoinColumn(name = "authority_username", referencedColumnName = "username") private Authorities authority;

CodePudding user response:

The reason you are getting usernames in both tables is that you have declared username column in both the tables.

Also, you have to map the relation based on the authority_id so the code of the models would look like this:

@Entity(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String username;
    private String password;
    private String oldPassword;
    private boolean enabled;
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "authority_id", referencedColumnName = "id")
    private Authorities authority;
}


@Entity
public class Authorities {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String authority;
    @OneToOne(mappedBy = "authority")
    private User user;
}

And the tables will be created like this:

Hope this solves your problem.

  • Related