是否使用休眠? [英] Use Hibernate or not?
问题描述
您好我有一个应用程序,我想从表中选择数据,如果没有找到,则生成新值并插入到数据库中,然后再次循环统计检查下一个值,等等。对于新的请求,这将花费数十亿次。
因此,我应该使用 hibernate
还是简单 JDBC
??
在hibernate中,我应该使用
session.saveOrUpdate(obj);
或
session.save(obj);
还有一件事我想问的是,对于每个插入操作,我必须初始化我的表类对象( new Mytable
)?只设定值会做什么?
像
MyTable obj = new Mytable();
obj.setName(dasd);
obj.set ...
obj.save();
或者我可以只初始化一次
<$ ($);
,在循环中放
(...
obj.setName() dasda);
obj.save
)
我假设/希望大约10亿次是一个调整数据,因为如果不是这样,那肯定会有很多所以你可能想重新检查你的设计。就这么说,至于你的第一个问题,我会推荐Hibernate,因为它基本上是建立在JDBC之上的,为您提供了一些更方便的功能,例如让您编写独立于数据库的查询,这意味着如果您更改数据库,则无需重新编写查询。
根据你的第二个问题,这实际上取决于你想做什么:
根据这个API:
< a href =http://docs.jboss.org/hibernate/orm/3.5/api/org/hiber nate / Session.html#saveOrUpdate%28java.lang.Object%29rel =nofollow> saveOrUpdate
$ b
void saveOrUpdate(Object对象)
抛出HibernateException
根据
未保存的分辨率,保存(Object)或更新(Object)给定实例价值检查(参见手册讨论未保存价值的
检查)。如果
关联映射为cascade =save-update,则此操作级联到关联的实例。
$ b参数:object - 临时或分离的实例,包含new
或updated状态抛出:HibernateException另请参见:
save(java.lang.Object),update(Object object)
保存
可序列化保存(Object对象)
抛出HibernateException
坚持给定的瞬态实例,首先分配一个生成的
标识符。 (或者如果使用
分配的生成器,则使用标识符属性的当前值。)如果关联映射为cascade =save-update,则此操作会级联到关联的
实例。
参数:object - 持久化类的临时实例
返回:生成的标识符抛出:HibernateException
根据你的第三个问题,你应该提供更多的信息。您可以设置值以避免在数据库中写入 null
值,但这取决于您的应用程序和实现它的上下文。
Hi i have an application in which i want to select data from the table and if not found generate new value and insert into database,and then again the loops stats over check for the next value and so on. This will be done around billion of times for a new request.
So should i use hibernate
or simple JDBC
??
and in hibernate should i use
session.saveOrUpdate(obj);
or
session.save(obj);
and one more thing i would like to ask is that for each insert do i have to initialize my table class object(new Mytable
)? of only set the values will do?
like
MyTable obj=new Mytable();
obj.setName("dasd");
obj.set...
obj.save();
or can i just initialize it once
`MyTable obj=new Mytable();`
and in a loop put
for(...
obj.setName("dasda");
obj.save
)
I am assuming/hoping that around a billion times
is a figure of speech because if it is not, that is sure a lot of operations so you might want to re-check your design.
That being said, as for your first question I would recommend Hibernate since it is basically built on top of JDBC and provides you with some handier things such as making you write database independent queries which means that you do not need to re-write your queries should you change your Database.
As per your second question, it really depends on what you want to do:
According to this API:
void saveOrUpdate(Object object) throws HibernateException
Either save(Object) or update(Object) the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking). This operation cascades to associated instances if the association is mapped with cascade="save-update".
Parameters: object - a transient or detached instance containing new or updated state Throws: HibernateException See Also: save(java.lang.Object), update(Object object)
save
Serializable save(Object object) throws HibernateException
Persist the given transient instance, first assigning a generated identifier. (Or using the current value of the identifier property if the assigned generator is used.) This operation cascades to associated instances if the association is mapped with cascade="save-update". Parameters: object - a transient instance of a persistent class Returns: the generated identifier Throws: HibernateException
As per your third question you should provide more information. You could have set values to avoid writing null
values to your database but this really depends on your application and the context within which it will be implemented.
这篇关于是否使用休眠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!