最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

spring - How to create Entity Class Dynamically without statically typed class in java - Stack Overflow

programmeradmin2浏览0评论

In order to query a table using JPA, we create a POJO class and annotate it with @Entity(name="table_name") and then give all the field mappings with @Column(name="column_name")

Is there a way where if I give table name, It needs to query that table retrieve data, columnNames and create Entity class Dynamically.

In this scenario, I don't want to write "SELECT" Query as my anization won't allow me to write Queries. I don't have a restriction of using JPA. I can use any framework like Hibernate, JPA or any flexible framework which helps me to create Dynamic Entity with retrieved Column Names.

In order to query a table using JPA, we create a POJO class and annotate it with @Entity(name="table_name") and then give all the field mappings with @Column(name="column_name")

Is there a way where if I give table name, It needs to query that table retrieve data, columnNames and create Entity class Dynamically.

In this scenario, I don't want to write "SELECT" Query as my anization won't allow me to write Queries. I don't have a restriction of using JPA. I can use any framework like Hibernate, JPA or any flexible framework which helps me to create Dynamic Entity with retrieved Column Names.

Share Improve this question asked Feb 15 at 20:21 Sairam MamidalaSairam Mamidala 111 bronze badge 5
  • Do you want runtime class generation, or are you happy with a map of data or a row object of some sort? – scrhartley Commented Feb 17 at 18:31
  • If we can able to retrieve the data and map the data to java.util.Map, that should work. Only requirement is I shouldn’t write Query manually in my Java code. – Sairam Mamidala Commented Feb 18 at 2:44
  • EclipseLink has dynamic/virtual entities: wiki.eclipse./EclipseLink/Examples/JPA/Dynamic Is that what you might be looking for? – Chris Commented Feb 18 at 15:07
  • @Chris - The link you provided is moving the configuration to a new xml file. This seems to be an option but my requirement is different. In the xml file, I see SELECT Query is being hardcoded. In my scenario, viewName will come as part of POST request. Somehow, I need to use that view and retrieve the data. Is there a possible way to do this – Sairam Mamidala Commented Feb 18 at 16:02
  • I don't quite understand your concern. Queries in JPA can be defined upfront in the persistence unit via orm.xml or annotations, or at runtime via EntityManager.createQuery api. You can always dynamically create queries using your own JPQL strings or criteria api. To be honest though, if your database schema is fixed, you are better off generating fixed entities upfront and avoiding the inevitable errors and security concerns when uses give in invalid data to your endpoint – Chris Commented Feb 18 at 16:28
Add a comment  | 

1 Answer 1

Reset to default 1

Not 100% sure, but have you looked at Jooq library? It has a functionality to generate classes based on available database schema. I'm not sure that you can generate classes at runtime (so while your server running to just modify database and your server on fly will change implementation). But it seems that it might be good fit for you.

I used this library multiple times. You just run maven/gradle job and Jooq goes to database and based on tables there generates Java classes that you can use to make query/modify data in database.

Whenever you have change in database and it affects your code you'll need to rerun the job so Jooq will regenerate classes so you can continue make queries.

发布评论

评论列表(0)

  1. 暂无评论