-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Creating table without primary key possible?
PostPosted: Mon Oct 27, 2008 1:05 pm 
Beginner
Beginner

Joined: Thu Oct 04, 2007 12:22 pm
Posts: 48
Hello!

For legacy reasons I want to map a table of a mysql db which has no primary. But using no PK seems to conflict with the expectations from Hibernate. Is a PK always required?

Hibernate version:
3.3


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2008 11:32 am 
Newbie

Joined: Fri Oct 10, 2008 11:17 am
Posts: 9
If you ever want to insert or update this table, you must have a primary key.

If you just want to select, you can get around it by using a native SQL portion in your code. For example:

Code:
        String sql = "";
        sql += "  select name as Name from phonelist where username = '" + username.trim() + "' ";
        SQLQuery query = session.createSQLQuery(sql);

        query.addScalar("Name", Hibernate.STRING);

        List list = query.list();
        String user = "";
        for (int i = 0; i < list.size(); i++) {
            user = (String) list.get(i);
        }



If you select multiple fields, it returns a List<Object[]>, eg.2:

Code:
            sql += " select handset as handset, hcode as hcode, ddesc as hcodeDesc,";
            sql += " hdate as hdate, hval as description";
            sql += " from hshistory , histdesc";
            sql += " where handset = '" + handsetNo + "' and hcode = dcode";
            sql += " order by hdate DESC";

        query = session.createSQLQuery(sql);
        query.addScalar("handset", Hibernate.STRING);
        query.addScalar("hcode", Hibernate.STRING);
        query.addScalar("hcodeDesc", Hibernate.STRING);
        query.addScalar("hdate", Hibernate.DATE);
        query.addScalar("description", Hibernate.STRING);
        List res = query.list();

        for (int i = 0; i < res.size(); i++)
        {
            Object[] objects = (Object[]) res.get(i);
            HandsetHistoryVO hHistory = new HandsetHistoryVO();
            hHistory.setHandsetNo((String) objects[0]);
            hHistory.setHcode((String) objects[1] + " " + (String) objects[2]);
            hHistory.setHdate((Date) objects[3]);
            hHistory.setDescription((String) objects[4]);
            list.add(hHistory);
        }



Please note: Hibernate is CRAP at casting correctly, that's why I'm adding all those scalars.

You could, in theory, construct insert and update statements via createSQLQuery as well.

HTH


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2008 2:21 pm 
Beginner
Beginner

Joined: Thu Oct 04, 2007 12:22 pm
Posts: 48
Thanks for you answer. The big drawback in your solution is the native sql query. But I assume this could be the only solution, if tables whith no PK are only allowed in some databases.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2008 2:41 pm 
Beginner
Beginner

Joined: Mon Mar 17, 2008 2:50 pm
Posts: 24
Serethos wrote:
Thanks for you answer. The big drawback in your solution is the native sql query. But I assume this could be the only solution, if tables whith no PK are only allowed in some databases.

Actually you are not right. Tables without primary keys are allowed in most databases. It is just considered to be a bad practice.
However, people often confuse PRIMARY KEY with the surrogate primary key.
You might not have primary key defined in your DDL but do you have any unique constraint ( or UNIQUE KEY speaking in MySQL terms)?
If so, you can specify <natural-id> or <composite-id> in your HBM file and it is as good as a primary key for what Hibernate is concerned.
You can even use a <formula> in it and have a derived key (I haven't try it myself, though).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 30, 2008 6:38 am 
Beginner
Beginner

Joined: Thu Oct 04, 2007 12:22 pm
Posts: 48
Perhaps we have a misunderstanding using some terms. I use the term primary key as one/ a set of colums which identify the entry in a unique way and moreover are marked for the schema as primary keys. If you choose a surrogate attribute as primary key or an natural one is a technical detail, but nonetheless it/they represent the primary key of the table.

So the problem I mentioned is based on a legacy database I did not create but have to work with. Within this mysql database there exists a table declaring no primary key. Mysql allows that leaving the danger of duplicate entries which can not be differed in any ways up to the programmer.

Hibernate does not seem to allow this - this is the basis of my posting. Using a natural-id, a composite-id etc is not the same as declaring no primary key, because there are side effects like indexing, uniqueness etc.

You are completely right to state that using no primary key is bad practice and it is clear to say that each entry should be distinct to another by its nature. But as I said there can be legacy issues which have to be respected (temporary).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 30, 2008 7:21 am 
Newbie

Joined: Fri Oct 10, 2008 11:17 am
Posts: 9
It is possible to pretend to hibernate that there is a unique key on a table by writing the hbm files/Annotations yourself, but if it's not guaranteed as unique by the database, it's dangerous, you'll probably get a whole bunch of errors whenever you try to do anything with the table.

There's no real harm in writing native SQL queries, as long as the SQLs are ANSI compliant (ie will work on any ANSI compliant database = all of them).

I believe you can also do sqlQuery.addEntity(yourClass) to avoid adding all those scalars, but you have to be very careful in naming the fields because it requires 100% accuracy (including case, and it does funny things with underscores) for it to cast correctly.

Of course, if you can write ANSI sql which is compatible with all modern databases, it begs the question: What's the point of HQL? Why didn't the hibernate designers just force you to use ANSI standard SQL instead of creating yet another SQL variant?


Top
 Profile  
 
 Post subject: Re: Creating table without primary key possible?
PostPosted: Thu Oct 04, 2012 9:29 am 
Newbie

Joined: Thu Oct 04, 2012 9:27 am
Posts: 1
I would do this only when you are reading data (not writing it). When you have a DB like oracle, you can have statements like

Code:
  select DOKUMENT.*, ROWID from DOKUMENT


=> and thus, you can add this statement into the Hibernate mapping:

Code:
    <id column="ROWID" type="string" />


subsequently, you define all other columns as

Code:
    <property...


When you use the reverse engineering Wizard, you can 1.) remove the composite-key tag, 2.) search and replace key-property for property and 3.) insert above line


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.