在运行时动态创建表和Java类 [英] Dynamically create table and java classes at runtime

查看:599
本文介绍了在运行时动态创建表和Java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的申请有要求。例如,如果用户按照名称Student创建了一个表单,并添加了像name,roll no,subject,class等那样的属性,那么在运行时,应该有一个由student name列名称,卷号,主题,类以及它的相关类和它的hibernate映射文件。有什么方法可以这样做?



在此先感谢,



Rima Desai

解决方案

Hibernate支持动态模型,也就是在运行时定义的实体,但必须写出一个映射文件。您应该注意一些有关动态模型的内容:


  1. 您可能会限制 这些在运行时(即你必须直接使用 Session 而不是使用 HibernateTemplate 的帮助器方法)或者类似的东西)。

  2. 使用 Map s支持动态模型作为字段的容器因此在运行时你将失去输入和POJO风格的API(除了支持的动态模型外,没有其他的东西可以做)。

所有这些都说明了,你没有提及是否需要动态定义的表在应用程序会话中保持不变。这可能会使事情变得复杂。

I have a requirement in my application. My tables wont be defined beforehand.For Example,if the user creates a form by name Student,and adds its attributes like name,roll no,subject,class etc.. then on runtime,there should be a table created by name student with columns name,roll no,subject,class and also its related class and its hibernate mapping file. Is there any way of doing so?

Thanks in advance,

Rima Desai

解决方案

Hibernate supports dynamic models, that is, entities that are defined at run-time, but you have to write out a mapping file. You should note a couple things about dynamic models:

  1. You may be restricted in how you define these at run-time (viz. you will have to use the Session directly instead of using a helper method from HibernateTemplate or something like that).

  2. Dynamic models are supported using Maps as the container for the fields of an entity, so you will lose typing and a POJO-style API at run-time (without doing something beyond the baked-in dynamic model support).

All of that said, you didn't mention whether it was a requirement that the dynamically defined tables be persistent across application sessions. That could complicate things, potentially.

这篇关于在运行时动态创建表和Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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