目前我对休眠有一个非常奇怪的问题。不知何故,它在表上创建了一个引用自身的外键。该列也是主键。这基本上阻止了我从该表中删除任何行。
在日志中我可以看到一行:
DEBUG org.hibernate.SQL - 更改表设备添加索引 FK79D00A76C682495E (id),添加约束 FK79D00A76C682495E 外键 (id) 引用设备 (id)
具有类似表的其他表似乎很好。这对于 MySQL 和 Derby 来说似乎都是正确的。我正在使用休眠 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;
}