gpt4 book ai didi

java - Hibernate 为自己创建外键

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:18:35 26 4
gpt4 key购买 nike

我现在有一个非常奇怪的 hibernate 问题。它以某种方式在表上创建了一个引用自身的外键。该列也是一个主键。这实质上阻止了我从该表中删除任何行。

在日志中我可以看到一行:

DEBUG org.hibernate.SQL - alter table Device add index FK79D00A76C682495E (id), add constraint FK79D00A76C682495E foreign key (id) references Device (id)

具有类似表的其他表似乎没问题。这似乎对 MySQL 和 Derby 都是如此。我正在使用 hibernate 4.1.4

注解类如下。

@Entity(name = "Device")
public class Device extends DomainObject implements Searchable {

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

@Column(name = "Type")
@Enumerated(EnumType.STRING)
private DeviceTypeEnum type = DeviceTypeEnum.AccessControlDevice;

@Column(name = "Name", length = Constance.DATABASE_NAME_FIELD_LENGTH)
private String name;

@Column(name = "Description", length = Constance.DATABASE_DESCRIPTION_FIELD_LENGTH)
private String description;

@Column(name = "Identifier", length = Constance.DATABASE_IDENTIFIER_FIELD_LENGTH, unique = true)
private String identifier;

@ManyToMany
@JoinTable(name = "Device2Group", joinColumns = {@JoinColumn(name = "DeviceID")}, inverseJoinColumns = {@JoinColumn(name = "DeviceGroupID")})
private List<DeviceGroup> groups = new ArrayList<DeviceGroup>();

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "location")
private Location location;

@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "Address")
private Address address;


@ManyToOne
@JoinColumn(name = "Link", nullable = false)
private Link link;
}

最佳答案

事实证明,在 Device 实体引用的实体类“Location”之一中,它有一个 @ManyToMany 映射的 Device 集合,它实际上应该是 @OneToMany。将 @ManyToMany 更改为 @OneToMany 后,约束消失。

关于java - Hibernate 为自己创建外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15858539/

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