In my business, I will to dynamically create model and repository for MySQL. I have successfully create the java file and complied it. then register the bean for repository and service using beanfactory. But I don't have an idea about how to map the Entity. I have searched some similar questions but do not have a clean solution. can anyone help and show me some code samples ?
Asked
Active
Viewed 3,144 times
0
-
Are you sure that your use-cases requires dynamic adding of new entities (and changing the database schema) at runtime. I would assume that you are trying to build a design that does not match relational databases very well. – Andreas Jul 30 '14 at 17:09
-
Yes, this is the requirement. I need to support both NoSQL and RDBMS – Xiran Li Jul 31 '14 at 01:32
1 Answers
0
JPA works mostly with static schema. There are good reasons for this, see discussion on www.java.net/node/666078 .
I know only about 3 solutions:
1) refactor/ change your domain model to be static ( best way but there are some real world situations where you really need at least 1 or 2 dynamic tables)
2) use native queries and @SqlResultSetMapping for dynamic tables ( this is solution I am using in such special cases)
3) use Jpa provider non portable specific ways ( in fact not JPA) see for example http://www.infoq.com/articles/hibernate-custom-fields

kulatamicuda
- 1,603
- 2
- 21
- 40
-
-
Yes. For example via em.createNativeQuery("CREATE TABLE SAMPLE_TABLE(SOME_ID BIGINT NOT NULL)").executeUpdate(); And later on using ALTER TABLE ... – kulatamicuda Jul 31 '14 at 09:39