vendredi 29 octobre 2021

Anonimous class generation at runtime with reflection from dao template

Giving this context:

I have an InstrumentDao class in hibernate, it could have some child class like DrumDao, or GuitarDao. The traditional way is to have this classes extending InstrumentDao, but I can't do that because of requeriment.

Instead, I have to read the name and properties of the child class from a json, properties, or yml file, and then recover the data from the database.

For example I want an instrument that is a guitar, I search for the properties of a guitar and then I create an object of class InstrumentDao, and I want to add the color atribute, and it's respective getter and setter methods.

Then the question is, can I create at runtime a new dynamic class from the InstrumentDao, that has the new properties of guitar, created using reflection or something? (I accept suggestions of course)

Then I would use an abstract dao like this to get the work done:

https://www.baeldung.com/simplifying-the-data-access-layer-with-spring-and-java-generics

The main problem is that I am not sure if Hibernate will accept any object created in this way. Old spring versions could do this with xml config files, but I also can not use it.

Thank you guys!





Aucun commentaire:

Enregistrer un commentaire