There are lots of Q&A's about the size of a Java object, which is quite straightforward to understand. But I'm wondering about the size of a Java class in the PermGen space.
The reason I wonder about this is because I'm writing a code generator, generating a lot of classes. Essentially, I'm generating two classes for every table/view in a database. Now I also want to model foreign key relationships. Instead of maintaining a complex, serialisable object-structure (think about a table having a unique key being referenced by several foreign keys belonging to other tables having other foreign keys, etc), I'd prefer to generate one class per UNIQUE KEY
and one class per FOREIGN KEY
.
Here are my questions:
public
classes, static
classes and private
member classes?I found a different solution, not wasting as much memory as generating one class per KEY
. I generate a single class that roughly looks like this:
public class References {
// First, initialise all unique keys
public static final UniqueKey<TAuthorRecord> SysPk_14655 =
createUniqueKey(TAuthor.T_AUTHOR, TAuthor.ID);
// Then initialise all foreign keys
public static final Reference<TBookRecord, TAuthorRecord> SysFk_14666 =
createReference(SysPk_14655, TBook.T_BOOK, TBook.AUTHOR_ID);
public static final Reference<TBookRecord, TAuthorRecord> SysFk_14667 =
createReference(SysPk_14655, TBook.T_BOOK, TBook.CO_AUTHOR_ID);
// Factory method for unique keys
protected static <R extends Record> UniqueKey<R>
createUniqueKey(Table<R> table, TableField<R, ?>... fields) {
// Factory method for foreign keys referencing unique keys
protected static <R extends Record, U extends Record> Reference<R, U>
createReference(UniqueKey<U> key, Table<R> table, TableField<R, ?>... fields) {
}
The actual tables from the generated table classes can then reference and use the above keys. I looked into JPA annotations as suggested by BobG in one of his comments. But I didn't find them very useful to describe:
@IdClass
needs a type as parameter, and I want to avoid that type)Some of the comments mentioned why I should create such a generator, because there are lots of established frameworks. I'm doing this for http://www.jooq.org. And I feel jOOQ is filling a gap in today's database abstraction possibilities.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With