可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
)
回答1:
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:
saveOrUpdate
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
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.
回答2:
Hibernate is an OR Mapping tool. In simplest terms, it objectifies the data layer in your applications so that you don't have to deal with the database interaction intricacies.
Now having said that, there are situations wherein hibernate may not be good choice.
If you schema is very simple (less than 8-10 tables) and does not have very complex relationship among them, then hibernate usage will be slightly overkill for this scenario.
If the size of data you are trying work on is very large (in the order of mn/bn), there too usage of hibernate usage is discouraged. Good old JDBC is better over here
If you application uses a lot of static data then caching would be a good option and hibernate support caching transparently.
So it all depends on what type of scenario you have. You can always build saveOrUpdate(obj);
using JDBC as well.
回答3:
1) So should i use hibernate or simple JDBC ??
If you know your way around hibernate I would say to go for it, otherwise only use it if you have enough time and resources to learn it. The fact is that if you use it well, it can be your best friend, otherwise it will just be a constant source of pain.
2) and in hibernate should i use
session.saveOrUpdate(obj);
or
session.save(obj);
For this question you might want to read the JavaDoc found here. A snippet:
save(Object object)
Persist the given transient instance, first assigning a generated identifier.
and
saveOrUpdate(Object object)
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).
The third question I don't really understand, please expand it.
回答4:
As most of your questions has been answered above, let me answer your last question. Yes, you have to create a new object, each new object (ideally, if there is no associated dependency) will be a new row in db. So, you have to create a new object-->set values to it--> associate it with hibernate session--> and saves it in db.If you have want to insert different rows in db then yes, you have to create different objects.
Hope this helps.
回答5:
Note also the under carpet technical aspects of Hibernate.
- It build reflection schema to call properties (either getters and setters or fields
- Hibernate generates SQL query anyway and reads data from data set and then calls reflection methods in order to assign the queried out value.
- If you have filled any property which is a key- Hibernate will first take it for query forming(yet one operation)
So, hibernate does at least additional operation twice- when loading and persisting an object. Really- it does much more.
Thus, once you use work with database in functional-not worklflow mode, you can read data from data set (Hibernate will do the same)and keep it in local variables . Working with variable is faster than use lookup tables of objects.
Yet one circumstance: any JPA maps a class to table. It is good,once you build your database from scratch or if database has suitable design. What if you need merge fields from number o tables? However the Hibernate has @OneToOne annotation to insert and object corresponding to another table. And yet once the Hibernate will do the cycle of work for the one-to-one linked object,when you need just few fields.
In a nutshell- calculate operations if performance is the ultimate criteria: you must and least 2 time be needed to invoke dataset. Hibernate is for heavy workflow operations, when you have benefits because of:
- You many times read a field from object not from dataset
- It is better to release data set that is much heavy than any data object