I have 3 objects which I will list here with only the relevant code with annotations:
OBJECT #1 : column ID index
uniqueness constraint on the column pair (NAME, ORGANISATION)
one to many relationship with object #2
@Entity
@Table(name = "module", uniqueConstraints = @UniqueConstraint(columnNames = { "NAME", "ORGANISATION" }))
public class Module implements Serializable
{
private static final long serialVersionUID = 8618562883278938873L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID")
private Long moduleId;
@Column(name = "NAME")
private String name;
@Column(name = "ORGANISATION")
private String organisation;
@OneToMany(mappedBy = "module", cascade = CascadeType.ALL)
private List<ModuleVersion> versions = new ArrayList<ModuleVersion>();
etc, etc ....
}
OBJECT #2 column VERSION_ID index
foreign key MODULE_ID mapped to ID in OBJECT #1
uniqueness constraint on the column pair (MODULE_ID, VERSION)
many to one relationship with object #1
one to many relationship with object #3
@Entity
@Table(name = "moduleversion", uniqueConstraints = @UniqueConstraint(columnNames = { "MODULE_ID", "VERSION" }))
public class ModuleVersion implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "VERSION_ID")
private Long versionId;
@Column(name = "VERSION")
private String version;
@ManyToOne
@PrimaryKeyJoinColumn(name = "MODULE_ID", referencedColumnName = "ID")
private Module module;
@OneToMany(mappedBy = "moduleVersion", cascade = CascadeType.ALL)
private List<DownloadRecord> downloads = new ArrayList<DownloadRecord>();
etc, etc
}
OBJECT #3 index column DOWNLOAD_ID
foreign key DOWNLOAD_VERSION_ID mapped to VERSION_ID in OBJECT #2
uniqueness constraint on the column pair (MODULE_ID, VERSION)
many to one relationship with object #2
@Entity
@Table(name = "downloadrecord")
public class DownloadRecord implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "DOWNLOAD_ID")
private Long downloadId;
@ManyToOne
@PrimaryKeyJoinColumn(name = "DOWNLOADED_VERSION_ID", referencedColumnName = "VERSION_ID")
private ModuleVersion moduleVersion;
etc, etc
}
Finally the error output:
14:15:40,890 INFO [SchemaUpdate] updating schema
14:15:40,937 INFO [TableMetadata] table found: public.downloadrecord
14:15:40,937 INFO [TableMetadata] columns: [downloaddate, downloadedby, moduleversion_version_id, download_id]
14:15:40,937 INFO [TableMetadata] foreign keys: []
14:15:40,937 INFO [TableMetadata] indexes: [downloadrecord_pkey]
14:15:40,984 INFO [TableMetadata] table found: public.module
14:15:40,984 INFO [TableMetadata] columns: [id, moduletype, description, isdeleted, organisation, name, deletiondate, deletedby]
14:15:40,984 INFO [TableMetadata] foreign keys: []
14:15:40,984 INFO [TableMetadata] indexes: [module_pkey]
14:15:41,015 INFO [TableMetadata] table found: public.moduleversion
14:15:41,015 INFO [TableMetadata] columns: [author, modulevisibility, submissiondate, submittedby, module_id, md5, version_id, uri, creationdate, rsversion, version]
14:15:41,015 INFO [TableMetadata] foreign keys: [fk5061a4ec79e9af3, fk_moduleversion_module_id]
14:15:41,015 INFO [TableMetadata] indexes: [moduleversion_pkey]
14:15:41,015 ERROR [SchemaUpdate] Unsuccessful: alter table downloadrecord add constraint FK5A7AAED963EF8508 foreign key (moduleVersion_VERSION_ID) references moduleversion
14:15:41,015 ERROR [SchemaUpdate] ERROR: number of referencing and referenced columns for foreign key disagree
14:15:41,015 INFO [SchemaUpdate] schema update complete
I know the why of this error (see
http://archives.postgresql.org/pgsql-sq ... g00349.php), but I can't figure out how to resolve it in code. Any ideas?
Thanx
Gaby