Does iBatis enforce a primary key on my table? - java

I'm working on a legacy project. The database is poorly designed. I want to change the DB layer now. The first think go through my mind is hibernate, but I hibernate need a primary key on my table. In fact some of my table does not have primary key. So I did a google search and I find iBatis, it's sounds very good with it. But I don't know whether iBatis enforce a primary key on my table?
Thanks.

No. IBatis does not force you to have a primary key on your table. Its primary job and strength is to map data from resultsets to java objects. The SQL statements to retrieve the data from the database are written by hand (you), so you have almost no constraints or limitations here.

Related

JPA Composite Key for one Table and a Primary Key for another Table - Possible?

Is it possible to have both a composite key and a primary key in the same Domain Model (Entity Class) so that some tables (queries) are joined using the composite key and other tables (queries) are joined using the primary key?
I'm dealing with legacy applications and I have limited access to changing the underlying database. Some of our queries are expecting a single row result but are getting many rows because of flaws in our database design. We can fix this problem by introducing a composite key to one of our Domain Models but doing so will affect many (many) other components that rely on the original primary key.
From my understand of JPA and the reading I've done so far on this matter I do not think this is possible but I thought it would be worth a shot to reach out to others who may have had a similar problem.
The table has only one primary key, so you have no options to choose which primary key to use. Also, i can't understand why you going to have differences between database original model and JPA. Actually, getting single row instead of many rows is where clause's task.
You said some of your queries fails after adding composite pk, so may be you just made your composite pk in wrong way?
Anyway, here is nice example or implementation composite pk, may be it will help you:
Mapping ManyToMany with composite Primary key and Annotation:
Maybe you should give a different look at your problem.
If your queries are returning multiple and different rows, then you should be able to resolve this using a more specific WHERE clause;
If your queries are returning multiple and equal rows, you should try the DISTINCT clause inside your query, example:
SELECT DISTINCT e FROM br.com.stackoverflow.Entity e

java servlet inserting data into mysql db with composite key

I'm working on a servlet that needs to insert some data to the db table with a composite primary key consists of the userid, dataid and CURRENT_TIMESTAMP.
however im getting the following error when executing the query
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry'13-7-2013-09-13 23:22:24' for key 'PRIMARY'
I think this is caused by the multiple insertion of rows to the same table in the same time, though with different dataid. Is there any solution to this problem? Should I cancel CURRENT_TIMESTAMP as a part of the primary key to do the trick or there are some other better workarounds?
Thanks a lot and appreciate for any help!
If it's a log table, it is recommended not to use a primary key. If you want to accelerate some search in this table, create the appropriate indexes.
If you need a primary key (for example, if you plan to use it with JPA), it would be best to use a number, e.g.
ID int AUTO_INCREMENT PRIMARY KEY
For example, log4j can insert each event log into a database using a org.apache.log4j.jdbc.JDBCAppender.
See also MySQL storage engine for a large log table.

Anyway to do Hibernate reverse engineering without putting foreign key in objects

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...

Hibernate Tools 4.0.0 Detect many-to-many tables NOT working with MySQL Db

I am using Hibernate Tools to reverse engineer from a MySQL 5.5.27 using STS 2.9.2 and Hibernate Tools 4.0.0.
It generates the POJO classes correctly, but is NOT picking up on the foreign key relationships defined across the tables (i.e. no sets nor relationships created at all!).
Any idea from your experience whether this tool works with MySQL or what I am doing wrong?
Thanks,
Bob
OK, I got it work at last!
The problem was, as with all things of a technical nature both simple and profound.
In a word: CaseSensitive
I had created my tables with capitaliZatioN (i.e. Person, Phone) and I used the MySQL Workbench to create the foreign keys and they looked like this:
CONSTRAINT FK_Phone_Person FOREIGN KEY (person_id) REFERENCES Person (person_id)
instead of
CONSTRAINT FK_Phone_Person FOREIGN KEY (person_id) REFERENCES person (person_id)
Notice the capitalization.
SOO happy after spending a lot of blood, sweat and tears trying to get that damn thing to work when all along the case was the problem!
many times the default engine of mysql also causes this issue i,e if the default engine through which the tables are create is "MyISAM" then please change it to "InnoDB"; the query to change is ALTER TABLE table_name ENGINE = InnoDB;, hope this works :)
My experience is that the reverse engineering tool needs specific constraints to work, if you have #OneToMany relationships they must have an intermediary table with the ids of both tables and the constraint that you cannot have null values nor orphan rows.
Also the tables will need to be named accordingly to your entities and intermediary tables must be named like owning_entity_entity2 to work.

A couple of questions about the use of Hibernate and ORM with Oracle

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.

Categories

Resources