Here is how i solve your problem
I work with a legacy system (Oracle) where other systems depends on it. In order to use it, DBA (not me) creates a database link with read-only access privilege. So if i need to create a new Entity for a new System without damaging (and worrying about) the database which is built on top of our legacy system, i do as follows
Suppose here goes our legacy class
@Entity
public class LegacySystemClass {
private Integer id;
/**
* I have read-only access privilege
* So insertable=false, updatable=false
*/
@Id
@Column(name="LEGACY_SYSTEM_ID", insertable=false, updatable=false)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
And i need to create a class for the new System which depends on LegacyClass
@Entity
public class NewSystemClass {
private Integer id;
private LegacySystemClass legacySystemClass;
@Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne
@JoinColumn(name="LEGACY_SYSTEM_ID")
public LegacySystemClass getLegacySystemClass() {
return legacySystemClass;
}
public void setLegacySystemClass(LegacySystemClass legacySystemClass) {
this.legacySystemClass = legacySystemClass;
}
}
Now, i generate a SQL file according to
AnnotationConfiguration configuration = new AnnotationConfiguration();
configuration
.addAnnotatedClass(NewSystemClass.class)
.addAnnotatedClass(LegacyClass.class)
.setProperty(Environment.DIALECT, <TYPE_YOUR_DIALECT>)
.setProperty(Environment.DRIVER, <TYPE_YOUR_DRIVER>);
SchemaExport schema = new SchemaExport(configuration);
schema.setOutputFile("schema.sql");
/*
schema.create(<DO_YOU_WANT_TO_PRINT_TO_THE_CONSOLE>, <DO_YOU_WANT_TO_EXPORT_THE_SCRIPT_TO_THE_DATABASE>);
*/
/**
* Make sure set up second parameter as false
* See above
*/
schema.create(true, false);
It will generate a file called schema.sql which contains
create table LegacySystemClass (LEGACY_SYSTEM_ID integer not null, primary key (LEGACY_SYSTEM_ID))
create table NewSystemClass (id integer not null, LEGACY_SYSTEM_ID integer, primary key (id))
alter table NewSystemClass add index FK8533D2E95B9B5D88 (LEGACY_SYSTEM_ID), add constraint FK8533D2E95B9B5D88 foreign key (LEGACY_SYSTEM_ID) references LegacySystemClass
I extract any SQL related to our legacy system such as LegacyClass. Our final schema.sql is shown as follows
create table NewSystemClass (id integer not null, LEGACY_SYSTEM_ID integer, primary key (id))
alter table NewSystemClass add index FK8533D2E95B9B5D88 (LEGACY_SYSTEM_ID), add constraint FK8533D2E95B9B5D88 foreign key (LEGACY_SYSTEM_ID) references LegacySystemClass (LEGACY_SYSTEM_ID)
I go to DBA room and ask to him
Can you run this schema.sql file to me, please ???
But as said by @Pascal Thivent
Communication is the key (Principle of Extreme Programming - XP)