nikkatsa / nk-playground

Repository for playing around with libraries etc. Nothing too serious
2 stars 1 forks source link

Adding generated jpa entities to persistence unit. #1

Open mrgenco opened 6 years ago

mrgenco commented 6 years ago

Hi,

First of all thank you for sharing this project. I have a scenario where i have to create my hibernate entity classes at runtime and persisting this entities without restarting my application. I could generate sources but couldnt add the persistence unit at runtime. I wonder if hibernate tools provide this feature at runtime?

Thanks

nikkatsa commented 6 years ago

Hello, I am not very familiar with this, but i would think you need a mechanism to load the generated .class files at runtime. For example, you can generate the source files (.java), and i guess you can find a way to compile them into bytecode (.class). If you are at this stage, then i would assume (despite the security concerns) that you can easily use the system's classloader to load your newly generated .class files at runtime.

mrgenco commented 6 years ago

Thanks for quick reply.. Well, actually i could generate my source files(.java) with java-poet and could load the .class files with Open-HFT. I can set and get entity properties at runtime. But when im trying to persist it im getting No entity found error. I read your blog post https://nikoskatsanos.com/blog/2016/06/01/hibernate-tools-jpa-entity-generation/ and thought that you may have similar experience. But in the project you are generating entity source files but not using entities at runtime right?

nikkatsa commented 6 years ago

Hello, Yes indeed, that project was merely an example on how to generate entities using the hibernate tools. Those entities were not really used at runtime in the same project.

I am not an expert in hiberante, but it seems you need to somehow tell hibernate to reload your config. Or if you use Spring with Hibernate, to reload your spring context which bootstraps Hibernate.

I found this question on StackOverflow which might be of help: https://stackoverflow.com/questions/22321050/spring-hibernate-reload-entity-mappings

I hope you solve your problem. Looks like an interesting one. When you do, if you blog anywhere about it, please let me know. I would be interested to read.

mrgenco commented 6 years ago

Hello,

Thanks for the link. It seems similar to my problem. Sure if i solve it i will be sharing :)