休眠中的关系 1-1 不起作用:未知列 account0_.date_of_birth
Posted
技术标签:
【中文标题】休眠中的关系 1-1 不起作用:未知列 account0_.date_of_birth【英文标题】:Relation 1-1 in hibernate not working : unknown column account0_.date_of_birth 【发布时间】:2020-07-01 10:29:19 【问题描述】:我想在 hiberante 中创建 1-1 关系,但收到错误:
java.sql.SQLSyntaxErrorException: Unknown column 'account0_.date_of_birth' in 'field list'
用户表:
@Entity
@Table(name = "user")
data class User(
@NonNull
@Column(nullable = false)
var login: String,
@NonNull
@Column(nullable = false)
var password: String,
@NonNull
@Column(nullable = false)
var email: String
)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Int? = 0
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = [CascadeType.ALL], orphanRemoval = true)
@Nullable
var bloodPressureInformationList: Set<BloodPressureInformation> = HashSet<BloodPressureInformation>()
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "account_id", referencedColumnName = "id")
var account: Account? = null
constructor() : this("", "", "")
账户表:
@Entity
@Table(name = "account")
data class Account(
@Column(nullable = false)
@NonNull
var firstName: String,
@Column(nullable = false)
@NonNull
var lastName: String,
@Column(nullable = false)
@NonNull
var dateOfBirth: String,
@Column(nullable = false)
@Enumerated(EnumType.STRING)
@NonNull
var sex: Sex)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Int? = 0
@OneToOne(mappedBy = "account", fetch = FetchType.LAZY, optional = false)
var user: User? = User()
Sql迁移代码:
Create Table user (
id BIGINT NOT NULL AUTO_INCREMENT,
login varchar(50),
password varchar (50),
email varchar(50),
CONSTRAINT user_pk PRIMARY KEY (id)
);
Create Table blood_pressure (
id BIGINT NOT NULL AUTO_INCREMENT,
systolic_pulse INTEGER NOT NULL,
diastolic_pulse INTEGER NOT NULL,
pulse INTEGER,
description varchar(250),
CONSTRAINT blood_pressure_pk PRIMARY KEY (id)
);
ALTER TABLE blood_pressure ADD COLUMN user_id BIGINT NOT NULL DEFAULT 1;
Create Table account (
id BIGINT NOT NULL AUTO_INCREMENT,
firstName varchar(50),
lastName varchar(50),
dateOfBirth varchar(50),
sex varchar(10),
CONSTRAINT account_pk PRIMARY KEY (id)
);
ALTER TABLE user ADD COLUMN account_id BIGINT NOT NULL DEFAULT 1;
您能告诉我如何解决这种关系吗?
【问题讨论】:
【参考方案1】:修正您的错字:dateOfBirth varchar(50)
应该是 date_of_birth varchar(50)
。其他骆驼案例列也是如此。
【讨论】:
以上是关于休眠中的关系 1-1 不起作用:未知列 account0_.date_of_birth的主要内容,如果未能解决你的问题,请参考以下文章
“java.sql.SQLSyntaxErrorException:'on 子句'中的未知列'*'”在休眠条件下加入表时