| |

VerySource

 Forgot password?
 Register
Search
View: 921|Reply: 5

Regarding the update of the joint primary key table in hibernate, please enlighten me! anxious!

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-24 14:00:01
| Show all posts |Read mode
I am using hibernate now, and I want to update a field of a table with a joint primary key, and the result gives me an exception: org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.persistent.Consumpatch # com.persistent .ConsumpatchId @ 68fd7db3]
I'm waiting online!

My method is as follows:
public boolean updateGroup (int typeid, int producterid, int specid, String branchid) {
Session session = HibernateSessionFactory.currentSession ();
Transaction tx = session.beginTransaction ();
try {
ConsumpatchId conid = new ConsumpatchId ();
conid.setTypeid (typeid);
conid.setSpecificid (specid);
conid.setBranchid (branchid);
conid.setConmpatchid (conid.getConmpatchid () + 1);
Consumpatch cons = (Consumpatch) session.load (Consumpatch.class, conid);
cons.setProducerid (producterid);
session.saveOrUpdate (cons);
session.flush ();
tx.commit ();
} catch (HibernateException he) {
if (tx! = null)
tx.rollback ();
he.printStackTrace ();
return false;
} finally {
session.close ();
}
return true;
}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-31 05:15:01
| Show all posts
The primary key problem, look for it carefully
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-1 11:00:01
| Show all posts
The ID already exists, so you can simulate it

public boolean updateGroup(int typeid,int producterid,int specid,String branchid){
Session session=HibernateSessionFactory.currentSession();
Transaction tx=session.beginTransaction();
try{
ConsumpatchId conid=new ConsumpatchId();

conid.setTypeid(typeid);
conid.setSpecificid(specid);
conid.setBranchid(branchid);
conid.setConmpatchid([color=#FF0000]conid.getConmpatchid()+1); [/color]
Consumpatch cons=(Consumpatch)session.load(Consumpatch.class,conid);
cons.setProducerid(producterid);
session.saveOrUpdate(cons);
session.flush();
tx.commit();
}catch(HibernateException he){
if(tx!=null)
tx.rollback();
he.printStackTrace();
return false;
}finally{
session.close();
}
return true;
}
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-1 11:30:01
| Show all posts
Host, are you sure there are records that meet the requirements in your database?

Judging from your exception information, there is no record in your database that meets the requirements.
When you press the primary key to query and the corresponding record is not found, the program will appear ObjectNotFoundException-this is one of the characteristics of the load() method.

By the way, a suggestion:
Replace load() with get(). The characteristic of get() is to return null when no corresponding record is found, instead of throwing an exception.

So, the solution is:
[b] 1. Make sure that there are records that meet the requirements in your database
2. Replace load() with get()[/b]
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-1 11:45:01
| Show all posts
[Next to the 3rd floor]
Suddenly found a more serious problem: the original poster's code is a bit illogical.
If you can successfully load() before, the record must exist. Then it must be update, why use saveOrUpdate() again? In saveOrUpdate(), you will press the main key to check once, the host, you have plunged yourself into a quagmire...

If you want to saveOrUpdate(), it means that the record you want to insert may or may not exist. Then the previous query is not only meaningless, but it will definitely bring an exception: if you use load(), you will set ObjectNotFoundException when the record does not exist. If you use get() and return null when the record does not exist, then a NullPointerException will follow

So, the supplement to the solution:
[b] If you want to saveOrUpdate(), you should remove the previous query, directly new Consumpatch() and assign values ​​to its attributes, and then saveOrUpdate(). [/b]
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-1 12:00:01
| Show all posts
ConsumpatchId conid=new ConsumpatchId();
conid.setTypeid(typeid);
conid.setSpecificid(specid);
conid.setBranchid(branchid);
conid.setConmpatchid(conid.getConmpatchid()+1);
The original code is very problematic. First instantiate a conid, which is an empty object at this time, conid.getConmpatchid(), this method is either empty or 0. Think about what you get every time you come here. If it doesn’t work, just use find instead of load
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list