I have a very strange problem with hibernate at the moment. Somehow on a table, it create a foreign key which reference to itself. the column is also a primary key. This essentially prevent me from delete any row from that table.
In the log I could see a line:
DEBUG org.hibernate.SQL - alter table Device add index FK79D00A76C682495E (id), add constraint FK79D00A76C682495E foreign key (id) references Device (id)
Other table with similar table seems fine. and this seems to be true for both MySQL and DerbyDB. I'm using hibernate 4.1.4
I would like to do some debug to see exactly how this happened, can anyone suggest which class in hibernate I should look at?
the annotated class is as follow.
Code:
@Indexed
@Entity(name = "Device")
@NamedQueries({
@NamedQuery(name = "Device.findByIdentifier", query = "select d from Device d where d.identifier = ?1"),
@NamedQuery(name = "Device.findByLink", query = "select d from Device d where d.link = ?1"),
@NamedQuery(name = "Device.findByType", query = "select d from Device d where d.type = ?1"),
@NamedQuery(name = "Device.findAll", query = "select d from Device d")})
public class Device extends DomainObject implements Searchable {
@Column(name = "Type")
@Enumerated(EnumType.STRING)
private DeviceTypeEnum type = DeviceTypeEnum.AccessControlDevice;
@Fields({@Field, @Field(name = "ALL")})
@Column(name = "Name", length = Constance.DATABASE_NAME_FIELD_LENGTH)
private String name;
@Column(name = "Identifier", length = Constance.DATABASE_IDENTIFIER_FIELD_LENGTH, unique = true)
@Fields({@Field, @Field(name = "ALL")})
private String identifier;
@ManyToMany
@LazyCollection(LazyCollectionOption.FALSE)
@JoinTable(name = "Device2Group", joinColumns = {@JoinColumn(name = "DeviceID")}, inverseJoinColumns = {@JoinColumn(name = "DeviceGroupID")})
private List<DeviceGroup> groups = new ArrayList<DeviceGroup>();
@ManyToOne
@JoinColumn(name = "location", insertable = false, updatable = false)
private Location location;
@Column(name = "location")
private Long locationId;
@Embedded
private SerialDeviceAddress address;
@ManyToOne
@JoinColumn(name = "Link", nullable = false, insertable = false, updatable = false)
private Link link;
@Column(name = "Link", nullable = false)
private Long linkId;
}
@MappedSuperclass
public abstract class DomainObject extends BasicJavaBeanModel implements
Serializable, WithID {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected Long id;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
}