在Spring Data JPA中,如何在运行时添加实体? [英] In Spring Data JPA, How to add an Entity during runtime ?

查看:190
本文介绍了在Spring Data JPA中,如何在运行时添加实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的企业中,我将为MySQL动态创建模型和存储库. 我已经成功创建了Java文件并进行了编译.然后使用beanfactory为存储库和服务注册bean. 但是我不知道如何映射实体.我搜索了一些类似的问题,但是没有一个干净的解决方案. 谁能帮我看看一些代码示例?

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 ?

推荐答案

JPA主要与静态模式一起使用.这样做有充分的理由,请参见www.java.net/node/666078上的讨论.

JPA works mostly with static schema. There are good reasons for this, see discussion on www.java.net/node/666078 .

我只知道3种解决方案:

I know only about 3 solutions:

1)将域模型重构/更改为静态(最好的方法,但在现实世界中,您实际上至少需要1或2个动态表)

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)对动态表使用本机查询和@SqlResultSetMapping(这是我在这种特殊情况下使用的解决方案)

2) use native queries and @SqlResultSetMapping for dynamic tables ( this is solution I am using in such special cases)

3)使用Jpa提供程序的非便携式特定方式(实际上不是JPA),例如,参见 http://www.infoq.com/articles/hibernate-custom-fields

3) use Jpa provider non portable specific ways ( in fact not JPA) see for example http://www.infoq.com/articles/hibernate-custom-fields

这篇关于在Spring Data JPA中,如何在运行时添加实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆