Home > Software engineering >  Spring creating own columns with wrong reference in table
Spring creating own columns with wrong reference in table

Time:11-07

I have problem with "communication" between Spring and PostgreSQL.

My class User.java:

public static final String TABLE_NAME = "USERS";
public static final String SEQENCE = "USERS_SEQ";

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQENCE)
@SequenceGenerator(name = SEQENCE, sequenceName = SEQENCE, allocationSize = 1)
private Long userId;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "department_id")
private Department department;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "organizazion_id")
private Organization organization;

Create table script for USERS:

CREATE TABLE IF NOT EXISTS USERS (
    USER_ID BIGINT NOT NULL, 
    DEPARTMENT_ID BIGINT,
    ORGANIZATION_ID BIGINT,
    PRIMARY KEY (USER_ID),
    FOREIGN KEY (DEPARTMENT_ID) REFERENCES DEPARTMENT(DEPARTMENT_ID),
    FOREIGN KEY (ORGANIZATION_ID) REFERENCES ORGANIZATION(ORGANIZATION_ID)
);

Class Department.java

public static final String TABLE_NAME = "DEPARTMENT";
public static final String SEQENCE = "DEPARTMENT_SEQ";

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQENCE)
@SequenceGenerator(name = SEQENCE, sequenceName = SEQENCE, allocationSize = 1)
private Long departmentId;
private String departmentName;
.
.
. //Some other columns of primitive types about department..

Create table script for DEPARTMENT:

CREATE TABLE IF NOT EXISTS DEPARTMENT (
    DEPARTMENT_ID BIGINT NOT NULL, 
    DEPARTMENT_NAME VARCHAR(50),
    PRIMARY KEY (DEPARTMENT_ID)
);

Class Organization.java

public static final String TABLE_NAME = "ORGANIZATION";
public static final String SEQENCE = "ORGANIZATION_SEQ";

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQENCE)
@SequenceGenerator(name = SEQENCE, sequenceName = SEQENCE, allocationSize = 1)
private Long organizationId;
private String organizationName;
private String street;
private String postalCode;
private String state;
private String city;

Create table script for ORGANIZATION:

CREATE TABLE IF NOT EXISTS ORGANIZATION (
    ORGANIZATION_ID BIGINT NOT NULL, 
    ORGANIZATION_NAME VARCHAR(50),
    STREET VARCHAR(255),
    POSTAL_CODE VARCHAR(50),
    STATE VARCHAR(255),
    CITY VARCHAR(255),
    PRIMARY KEY (ORGANIZATION_ID)
);

When I run the program, the Spring create "own" columns in PostgreSQL, and it's look like:

USER_ID, DEPARTMENT_ID, ORGANIZATION_ID, DEPARTMENT_DEPARTMENT_ID, ORGANIZATION_ORGANIZATION_ID

How can I fix it ? It should be without columns

DEPARTMENT_DEPARTMENT_ID, ORGANIZATION_ORGANIZATION_ID

The annotation @Column(name = "column_name") can't be used here.

CodePudding user response:

As join column you should reference the attribute in the other class:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQENCE)
@SequenceGenerator(name = SEQENCE, sequenceName = SEQENCE, allocationSize = 1)
private Long userId;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "departmentId")
private Department department;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "organizazionId")
private Organization organization;
  • Related