Search code examples
hibernategenericshibernate-mappingjavassist

Hibernate Entities Creation using Javassist


Our requirement needs Pojos to be generated based on user inputs. So classes are created on run time based on the inputs provided by users. We have been creating object using Javassist and use annotations with it to create all the required mapping. For example, the following gets created without a problem because there are no generics involved:

    @ManyToOne(cascade = CascadeType.ALL)
public Address getUserAddress() {
    return this.userAddress;
}

This gets created fine when we use Javassist. However, when we try to create annotations for one to many mappings, Javassist doesnt work as it doesnt support generics.

@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
public Set<UserRecord> getUserRecords() {
    return this.userRecords;
}

This doesnt work because we need to set the return type as "Set" which cant be created with Javassist. I also tried using the set generic method by using the following

ctfield.setGenericSignature(java.lang.String sig)

So far, I havent been successful in generating a class with generics return type using Javassist. Is there any other method or any other workaround available to address this?


Solution

  • CtMethod inherits CtBehavior#setGenericSignature().
    Follow to CtClass javadoc for sample code.