Our current database (MySQL) already has Indexes for its foreign keys since these use InnoDB, which (afaik) automatically generates IndexTables for foreign keys. Hibernate wasnt used to create these tables. So my question is: Is MySQL using Indexes for their foreign keys automatically, when I use Hibernate to fetch Data from its tables? Or do I have to configure Hibernate to use these Indexes?
Thank you,
ymene
Yes, the query optimizer decides to use or not to use an index. Use EXPLAIN on your SELECT statements to see what it uses. There is no difference in a query created by Hibernate or a hardcoded query.
Is MySQL using Indexes for their foreign keys automatically, when I use Hibernate to fetch Data from its tables? Or do I have to configure Hibernate to use these Indexes?
There is nothing to configure in Hibernate to make the SQL backend use indices for joins. Let Hibernate perform its queries and if the optimizer decides that using an index is appropriate, it will use it.
PS: I'm not talking about Index Hints syntax here, see HHH-2736 - support for native/SQL query hints in HQL/Criteria (Oracle SELECT hints for example), this is something different.
Related
I am auditing a Java object using Hibernate Envers annotations, but initial object creation occurs directly in the database using Pentaho (ETL).
I want to create the object using ETL and add a table entry to the Envers generated object_AUD and REVINFO tables.
I have been trying to find the generation strategy for the REV column from the REVINFO table, but I must be looking in the wrong places. Would someone help me find an effective generation strategy so I can manually insert records into the audited tables without causing possible collisions or weird behavior in the future?
What you seek is going to depend on whether or not you are configuring your application to take the default for org.hibernate.envers.use_revision_entity_with_native_id.
The default value (true) tells Envers to ask Hibernate to create the REVINFO table using a native-based primary key which will either be IDENTITY or SEQUENCE depending upon your database platform. If you look at the table definition for REVINFO in your database, you should be able to deduce this information.
If this property is configured using false, Envers will construct its own sequence metadata and provide that to Hibernate. The sequence is called REVISION_NUMBER and is stored in a table called REVISION_GENERATOR. The sequence is initialized to 1 and incremented by 1 as the default.
I am currently trying to set the Storage Engine for a table, because from case to case MyISAM and InnoDB should be used. Unfortunately I did not find a way to set this in Hibernate, and I do not want to create each table by hand. My prefered way would be to anotate it in the Java-POJO, but I couldn't find a way to do so.
I've found Hibernate: what's the difference between MySQLDialect and MySQLInnoDBDialect?, which tells me that setting the Dialect to org.hibernate.dialect.MySQLMyISAMDialect would help (it says this for the InnoDB-Dialect, but it seems like one could choose the default dialect with this), but this method has two shortcommings: First, the dialect is chosen for all tables, and second, it creates a query like CREATE TABLE xy(..) type=MyISAM but the query would be correct with engine=MyISAM.
Also Hibernate mysql innodb says that there are defaults, and that I can overwrite them when creating tables, but not how - does somebody know how to do this?
Instead of org.hibernate.dialect.MySQLMyISAMDialect or org.hibernate.dialect.MySQLInnoDBDialect, you should use org.hibernate.dialect.MySQL5MyISAMDialect or org.hibernate.dialect.MySQL5InnoDBDialect.
This way, it creates the table using engine=MyISAM or engine=InnoDB.
But this configuration is global and if you really have a reason to configure it for a single table, you could try:
Hibernate import SQL (hibernate.hbm2ddl.import_files). Add the path to a SQL file which contains the alter table into it.
Anyway to do Hibernate reverse engineering without putting foreign key in objects.
I am trying to do a Hibernate reverse engineering on my mysql database but I dont want the objects to show foreign keys.. Can this be done?
Exactly same problem here, you should use a new acces to this DB where alter table is not allowed.
So: create a new user with a DBA not add alter table permission and use this connection parameter to acces to this DB, this way alter table will not possible!
(workaround i know)
And the cause can be for this workaround e.g. Liferay doesn't use foreign keys and if hibernate mess up the table, the whole system fall apart...
I am thinking of writing a tool that will list all the tables in an oracle database. Clicking on any of the links for each of the table will load the data from that table.
Usually i just use plain old jdbc with standard sql queries or stored procedures in the code. This has worked fine for me so far but i am wondering if using hibernate will help and reduce the work load. It is also a good way to learn about hibernate.
Could you please let me know if hibernate can help and how. i can think of the following reasonings
No need to write the queries
No need to manage queries
Managing the transactions will probably be easier.
There are also some issues which im not sure what the answers are. For example, the database i will be working with is quite old and not all the table have primary keys. Reading up on tutorials about hibernate, the following questions have arisen
Do all tables have to have a primary key named "id"? None of my tables have a column called id. The primary keys are named something else
Do tables have to have primary keys? Not all of my tables have primary keys. Especially tables with standing data.
Some tables have primary keys as compound keys (The primary key is comprised of 2 columns). Would these be allowed?
I would also be interested in any simple tutorials. I have seen a couple but none are intended for newbies to hiberate.
Thanks
Could someone please provide situations/example scenarios where hibernate becomes invaluable
Thanks
I think Hibernate will increase your workload. ORM means objects, so you'll have to write objects to map the Oracle tables to.
I don't think this is an appropriate use of Hibernate. JDBC is fine for this case. By all means proceed if you'd like to learn Hibernate, but I can think of several smaller problems where it would be more appropriate.
Usually i just use plain old jdbc with standard sql queries or stored procedures in the code. This has worked fine for me so far but i am wondering if using hibernate will help and reduce the work load.
This would be pretty straightforward to implement with JDBC and won't require writing many queries if you use DatabaseMetaData and ResultSetMetaData to do things dynamically.
With Hibernate, you would have to generate entities from the physical model. That's possible, Hibernate provides tooling for this. But I'm not convinced Hibernate will give you any advantage here (not a CRUD app, JDBC would just work). So I agree with #duffy, Hibernate might even give you more work.
It is also a good way to learn about hibernate.
I can understand that. But this is not the best application to learn Hibernate in my opinion.
Do all tables have to have a primary key named "id"? None of my tables have a column called id. The primary keys are named something else
No, this is not required, you can map any column name as primary key.
Do tables have to have primary keys? Not all of my tables have primary keys. Especially tables with standing data.
If you don't have any unique column, it will be a problem, Hibernate expects entities to have an identifier.
Some tables have primary keys as compound keys (The primary key is comprised of 2 columns).
That's supported.
Consider Hibernate as an investment. Just like learning Spannish or Martial Arts. It's hard at the beginning, but after you passed the dip, you'll get huge benefits.
To answer your questions:
You can use any primary key you want with Hibernate
Hibernate doesn't support tables without a primary key, but why you would want that?
Yes compound keys are supported
Consider buying Hibernate in Action. Start from there.
In some cases we opt for using unique constraints over primary keys to improve performance especially on tables that have no relationship to any other tables. In some cases we avoid primary keys and even normalising a table to prevent unique indexes being created and the need for Oracle to update and re-calculate indexes when there are changes.
I am not a dba and i know there are several arguments for and against the use of primary keys but i dont really want to go into that for now.
To be honest, the reason i want to use hibernate is purely for my own benefit in that i would like to learn how to use it. Given that i have an opportunity to write this tool and that it is not a mission critical tool and will only be used "in-house" i decided that i should try hibernate and by learning it i might find it usefull later on if i need it on other projects.
I dont currently have a requirement to write to the database so ill only be reading from the database. As not all tables have primary keys, is it possible to trick hibernate into using the the unique column as the primary key? I think it might be possible to create VIEWS and add an id column in the view for tables that dont have a primary key or a unique constraint.
There is a UNIQUE database constraint on an index which doesn't allow more than one record having identical columns.
There is a piece of code, managed by Hibernate (v2.1.8), doing two DAO
getHibernateTemplate().save( theObject )
calls which results two records entered into the table mentioned above.
If this code is executed without transactions, it results INSERT, UPDATE, then another INSERT and another UPDATE SQL statements and works fine. Apparently, the sequence is to insert the record containing DB NULL first, and then update it with the proper data.
If this code is executed under Spring (v2.0.5) wrapped in a single Spring transaction, it results two INSERTS, followed by immediate exception due to UNIQUE constraint mentioned above.
This problem only manifests itself on MS SQL due to its incompatibility with ANSI SQL. It works fine on MySQL and Oracle. Unfortunately, our solution is cross-platform and must support all databases.
Having this stack of technologies, what would be your preferred workaround for given problem?
You could try flushing the hibernate session in between the two saves. This may force Hibernate to perform the first update before the second insert.
Also, when you say that hibernate is inserting NULL with the insert, do you mean every column is NULL, or just the ID column?
I have no experience in Hibernate, so I don't know if you are free to change the DB at your will or if Hibernate requires a specific DB structure you cannot change.
If you can make changes then you can use this workaround in MSSQL tu emulate the ANSI behaviour :
drop the unique index/constraint
define a calc field like this:
alter table MyTable Add MyCalcField as
case when MyUniqueField is NULL
then cast(Myprimarykey as MyUniqueFieldType)
else MyUniqueField end
add the unique constraint on this new field you created.
Naturally this applies if MyUniqueField is not the primary key! :)
You can find more details in this article at databasejournal.com