gpt4 book ai didi

java - Hibernate 中 INT(11) 的 UNSIGNED 约束

转载 作者:行者123 更新时间:2023-11-29 04:37:44 29 4
gpt4 key购买 nike

我一直在尝试使用 MYSQL 向列添加约束 UNSIGNED。下面是我的表格。

public class UserActivity implements BaseEntity{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;

Long entityId;

@Column(columnDefinition = "enum('QUESTION','ANSWER', 'COMMENT', 'TAGS')")
@Enumerated(EnumType.STRING)
Type type;

@ManyToOne
@JoinColumn(nullable = false, columnDefinition="INT(11) UNSIGNED")
CustomerEntity user;

但是由于没有设置外键和索引,它没有为数据库中的用户列创建约束。 Thisthis不能帮忙。请帮帮我。谢谢

日志:

Hibernate:
alter table UserActivity
add constraint FK_jfw954ii6gw7mbqdth6y3n0rs
foreign key (user_Id)
references customerDb.customer (Id)
12:31:30.350 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table UserActivity add constraint FK_jfw954ii6gw7mbqdth6y3n0rs foreign key (user_Id) references customerDb.customer (Id)
12:31:30.350 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaExport - Can't create table 'community_db.#sql-43f0_5a07' (errno: 150)
Hibernate:

MySQL 查询:

ALTER TABLE `community_db`.`Question` 
ADD INDEX `fk_Question_Customer_idx` (`author_Id` ASC);
ALTER TABLE `community_db`.`Question`
ADD CONSTRAINT `fk_Answer_Customer`
FOREIGN KEY (`author_Id`)
REFERENCES `customerDb`.`customer` (`Id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

以下是上述 mysql 查询的日志:

Error Code: 1005. Can't create table 'community_db.#sql-43f0_5d19' (errno: 150) 0.048 sec

最佳答案

@JoinColumn 或外键的columnDefinition 属性值必须等于@ 的columnDefinition 属性值Entity Id 中存在列 注释,即它们必须具有相同的属性。

下面是客户实体

@PersistenceUnit(unitName="persistenceUnit")
@Entity
@Table(name = "customer", schema = "customerDb")
public class CustomerEntity {
@Id
@Column(name = "Id", nullable = false, columnDefinition = "INT(11) UNSIGNED")
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}
..................
}

并且在 UserActivity 中包含客户 Id 作为外部必须存在相同的 columndDefinition 键。

public class UserActivity implements BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;

Long entityId;

@ManyToOne
@JoinColumn(nullable = false, columnDefinition = "INT(11) UNSIGNED")
CustomerEntity user;
..................
}

我使用 IntelliJIdea 创建了那个表,它没有添加那些我收到错误的属性。

关于java - Hibernate 中 INT(11) 的 UNSIGNED 约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37179116/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com