-->
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.  [ 8 posts ] 
Author Message
 Post subject: How to update the primary key on a master table?
PostPosted: Thu Aug 12, 2004 12:26 pm 
Newbie

Joined: Wed Dec 10, 2003 11:49 am
Posts: 17
Hibernate version:
2.1

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:
Oracle 9i

Debug level Hibernate log excerpt:

Hello, can i use "update tablename set sn='newValue' where sn='oldValue' as a query string in hibernate to update my master table's primary key that should also update the child tables.
I have a master table that is linked to 2 child tables. But in my hibernate map, i have only master and child1 linked using many-to-one. The other child (child2) is not linked. But i want to do an update. I can use oracle trigger to update child2. Any help would be arreciated.
Thanks


Top
 Profile  
 
 Post subject: Re: How to update the primary key on a master table?
PostPosted: Thu Aug 12, 2004 4:46 pm 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
elango wrote:
Hello, can i use "update tablename set sn='newValue' where sn='oldValue' as a query string in hibernate to update my master table's primary key that should also update the child tables.

I have a master table that is linked to 2 child tables. But in my hibernate map, i have only master and child1 linked using many-to-one. The other child (child2) is not linked. But i want to do an update. I can use oracle trigger to update child2. Any help would be arreciated.
Thanks


If the links between your tables are backed up by foreign key constraints, then you can't perform that update from a SQL Plus prompt. You'll get an ORA-02922: Integrity Constraint Violated error. There are much more knowledgeable people around than me, but I think primary keys should be immutable for the life of the relation. That caveat aside, you can create a new master record, copy the fields from the old to the new, then update the child records to to the new master, then finally delete the original master. Here's a code snippet that works for me even with foreign keys enabled:

Code:
    Session session = sessionFactory.openSession();
    Transaction t = session.beginTransaction();
    Iterator masterIterator = session.iterate("from MasterTable as m where m.masterId = 1");
    MasterTable mt1;
    if (masterIterator.hasNext()) {
      mt1 = (MasterTable)masterIterator.next();
      MasterTable mt2 = new MasterTable();
      mt2.setMasterId(new BigDecimal(4));
      mt2.setMasterDescription(mt1.getMasterDescription());
      session.save(mt2);
      Set childTables = mt1.getChildTables();
      Iterator childIterator = childTables.iterator();
      while (childIterator.hasNext()) {
        ChildTable ct = (ChildTable)childIterator.next();
        ct.setMasterTable(mt2);
        session.update(ct);
      }
      session.delete(mt1);
    }
    t.commit();


Here's the data before the run:
Code:
SQL> select *
  2  from master_table;

MASTER_ID MASTER_DESCRIPTION
---------- ------------------------------
         1 Test Record 1
         2 Test Record 2

SQL> select *
  2  from child_table;

  CHILD_ID CHILD_DESCRIPTION               MASTER_ID
---------- ------------------------------ ----------
         1 Child_record 1                          1
         2 Child_record 2                          2
         3 Child_record 3                          1

SQL>


And here's the data after the run:
Code:
SQL> select *
  2  from master_table;

MASTER_ID MASTER_DESCRIPTION
---------- ------------------------------
         2 Test Record 2
         4 Test Record 1

SQL> select *
  2  from child_table;

  CHILD_ID CHILD_DESCRIPTION               MASTER_ID
---------- ------------------------------ ----------
         1 Child_record 1                          4
         2 Child_record 2                          2
         3 Child_record 3                          4

SQL>


I'm pretty new here, so there may be better/cleaner ways to do this.

HTH,
Maury


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 13, 2004 12:23 pm 
Newbie

Joined: Wed Dec 10, 2003 11:49 am
Posts: 17
Thanks Maury. But mine is little complicated. I have totally 4 tables. Say Table A is the master table. I have child tables A1 and A2 to master table A. Here A2 is also a master table for table say B.
A is the master for A1 and A2
A2 is master for B
But i have only A and A1 linked using hibernate map. Other tables are not linked.
I wrote oracle trigger (After update on tableA) to update the child tables. I know that is not a good way. but just to make it work. But i can't even update the master table using hibernate.


Thanks for your help anyway


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 13, 2004 1:13 pm 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
I think the code I posted could be changed to do what you want. You could add all tables to the Hibernate map, then create a new master record and save it. Then probably work from the bottom of the graph up and change the records, finally deleting the original master. Just a thought.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 13, 2004 2:16 pm 
Newbie

Joined: Wed Dec 10, 2003 11:49 am
Posts: 17
Yes, but our's is kind of big project and if i change my map now, i would end up changing code in bunch of different places. But i keep that thought for next release of our software.
But again if i can directly execute the SQL query "update tableA set serial_number = 'oldserialno' where serial_number = 'newserialno' in hibernate createSQLQuery, that would solve my problem given that i have trigger to take care of the child tables.
I tried it and it keep throwing oracle error "[Oracle]ORA-00971: missing SET keyword". It has nothing to do with oracle because i tested it from running the sql from sql> prompt and it worked fine.
Why wound't hibernate support native sql (Particularly the update query)?

Thanks once again.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 13, 2004 2:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can just use a normal JDBC query using session.connection()


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 9:37 am 
Newbie

Joined: Wed Dec 10, 2003 11:49 am
Posts: 17
That's what i am doing now. RAW jdbc update call from java.
Thanks


Top
 Profile  
 
 Post subject: Re: How to update the primary key on a master table?
PostPosted: Fri Dec 07, 2012 7:46 am 
Newbie

Joined: Fri Dec 07, 2012 7:40 am
Posts: 1
I also have similar scenario to update master key. When I update it using native query, Hibernate does not recognize my update.
For example, In user Login History table I have primary key upto 100 already. So I changed 100 to 101 on some condition.
When next user login, it tries to insert a record to the table with 101 as id. So exception occurs.

I am using "increment" strategy for my primary key. 2nd level cache is also disabled. Also I am using hibernate with spring.

Any help is much appreciated.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.