Search code examples
javahibernatesun-codemodel

how to define java codeModel generated class within another generatede class WITHOUT fully qualifuied name


I am using java codeModel to generate hibernate entity classes.

Where tables have compound keys, i am generating an @Embeddable class that i then need to define a field for in my entity class.

currently this compound key class is being defined as a fully qualified name with no java import statement in my entity class: e.g

private com.aaa.bbb.CompoundKey compoundKey;

How do i tell codeModel NOT to fully qualify my CompoundKey field? e.g

import com.aaa.bbb.CompoundKey;

private CompoundKey compoundKey;

I create my entity class like this

JCodeModel codeModel = new JCodeModel();
JDefinedClass entityClass = codeModel._class("com.aaa.bbb.EntityClassName");

i create my compound key class like this

    JDefinedClass compoundKeyClass = codeModel._class("com.aaa.bbb.CompoundKeyClassName");

i create the compoundKey field in the entity class like this

JFieldVar field = entityClass.field(JMod.PRIVATE, compoundKeyClass, "compoundKey");

Solution

  • i have discovered the cause of this issue

    when the compound key class name being generated starts with the entity class name codeModel fully qualifies the entity field declaration.

    e.g. when the entity and compound key class are named as follows i see the issue mentioned in my question

    ENTITY CLASS NAME               com.aaa.bbb.ENTITY
    COMPOUND KEY CLASS NAME         com.aaa.bbb.ENTITYKEY
    

    However when the compound key class name does not start with the entity class name codeModel works as desired

    ENTITY CLASS NAME               com.aaa.bbb.ENTITY
    COMPOUND KEY CLASS NAME         com.aaa.bbb.KEYENTITY