1

I have a scenario, I am learning Hibernate. I have heard about the dynamic POJO classes(bytecode) in java. So I am assuming to create a POJO class Dynamically. For that,I need to know the name of the all columns in a Table(for a proper mapping), where only table name(say EMP_DATA) is given. Is there any way to do that?

So, what am I thinking is : If I am able to describe the table at runtime then may be I can get the column names. And then on the basis of that I may Create a Dynamic POJO(bytecode).

See the references for the Idea.

Community
  • 1
  • 1
yashpal bharadwaj
  • 323
  • 2
  • 6
  • 14
  • 1
    What would be the point? Why not just use JDBC to get all the column names and their value. The point of Hibernate is to have a rich, navigable static object model mapped to the database. Not to dynamically discover table and column names. – JB Nizet May 02 '16 at 16:22
  • I just want to know the procedure of **Hibernate-Reverse-Engineering**. We just provide the schema/dp name or reference and it creates mapping and pojos for us. How it is doing that. I just want to have the Idea because technically it looks brilliant. @v.ladynev sir please have a glance on the problem – yashpal bharadwaj May 03 '16 at 07:33
  • It does that without using Hibernate. It executes SQL queries to find the table, their columns, names and types, etc., and generates corresponding Java entities. Just like you would do by yourself: look at the tables and their structure, and write entities that map to these tables. – JB Nizet May 03 '16 at 08:19
  • @ JB Nizet correct me If I am wrong but I saw [Here](https://github.com/hibernate/hibernate-tools/tree/master/src/java/org/hibernate/cfg) that it is using all the hibernate claases tin the code. So I think saying **"It does that without using Hibernate."** will not be appropriate. – yashpal bharadwaj May 03 '16 at 10:30
  • Of course, the classes of the hibernate reverse engineering tool are part of the Hibernate project. What I mean is that this tool uses JDBC to access the database, find the tables and its columns. and generate Java source code, that can then be used in a Hibernate-based application to read and write in those tables. But that has nothing to do with what you suggest in your question, which is to write an hibernate-based application using hibernate to write in tables, but without defining the hibernate entities in advance, and instead generating them dynamically. – JB Nizet May 03 '16 at 10:54
  • @ JBNizet means you are saying hat there is no option to map the database other than defining pojos in advance? – yashpal bharadwaj May 04 '16 at 06:35
  • 1
    Don't think about it this way. How would you write an application using unknown classes, that would be dynamically generated at runtime. How would you do `Product p = new Product(); em.persist(p);` to create and save a product in the database, if you don't have a Product class, because it would be dynamically generated at runtime? – JB Nizet May 04 '16 at 06:41

0 Answers0