I have a very complicated entity which I wish to load in two phases:
- Load all business data
- Load all metadata
I want to separate the loading.
Can I declare fields as @Transient on runtime only using reflection API?
I tried to use preLoad event but couldn’t find how to set a field transient.
Thanks
Idob
1) Annotation information is part of java class information. You cannot change in runtime.
Of course, you can generate some byte code generation tool: javaassist or cglib to generate class and load appropriate entity implementation in runtime. But it is looks like ugly hack. Also this solution is pretty complex and not stable.
2) Try do not hack and add complexity in your application. Better to refactor existing domain model. Consider wy you need several fixed representation for you domain.
3) Also if you really need weak structure of the entity you may serialize Entity into XML or Binary format and store in DB as Text(or maybe your db support XMLType) or as Blob.
If your have a problem with LazyInitializationException and you develop Web application try to consider OpenSessionInView pattern. Hibernate will create one Hibernate session per each request. So you can manipulate with Hibernate objects in one request without thinkin about LazyInitializationException.