Spring在表中使用错误的引用创建自己的列

h4cxqtbf  于 2022-11-21  发布在  Spring
关注(0)|答案(1)|浏览(158)

SpringPostgreSQL之间的“通信”出现问题。
我的班级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;

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)
);

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

DEPARTMENT创建表脚本:

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

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;

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)
);

当我运行程序时,SpringPostgreSQL中创建“自己的”列,它看起来像:

USER_ID, DEPARTMENT_ID, ORGANIZATION_ID, DEPARTMENT_DEPARTMENT_ID, ORGANIZATION_ORGANIZATION_ID

怎么修?应该是没有柱子的

DEPARTMENT_DEPARTMENT_ID, ORGANIZATION_ORGANIZATION_ID

此处不能使用注解@Column(name = "column_name")

3phpmpom

3phpmpom1#

作为连接列,您应该引用其他类中的属性:

@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;

相关问题