关于hibernate加锁的问题?大家进来看一下。
我使用hibernate访问ORACLE9i数据库,首先数据要分页然后要加悲观锁,语句如下:
String hqlStr = "from MessageEntity mes where mes.messageChannel=:mcv ";
quMessage = session.createQuery(hqlStr);
quMessage.setLockMode( "mes ", LockMode.UPGRADE_NOWAIT);
quMessage.setString( "mcv ", mesChannel);
quMessage.setFirstResult(beginNumber);
quMessage.setMaxResults(numberOfEachPage);
运行是报如下错误:
17:20:57,453 INFO [STDOUT] Hibernate: select * from ( select messageent0_.mid as mid0_, messageent0_.MDATE as MDATE0_, messageent0_.MCHANNEL as MCHANNEL0_, messageent0_.MEMAIL as MEMAIL0_, messageent0_.MTITLE as MTITLE0_, messageent0_.MCONTENT as MCONTENT0_, messageent0_.MREPLY as MREPLY0_, messageent0_.MSTATUS as MSTATUS0_ from MESSAGE messageent0_ where messageent0_.MCHANNEL=? and (messageent0_.MREPLY is null) and substr(messageent0_.MSTATUS, 1, 1)= '0 ' and substr(messageent0_.MSTATUS, 2, 1)= '0 ' ) where rownum <= ? for update of messageent0_.mid nowait
17:20:57,515 WARN [JDBCExceptionReporter] SQL Error: 904, SQLState: 42000
17:20:57,515 ERROR [JDBCExceptionReporter] ORA-00904: "MESSAGEENT0_ ". "MID ": 无效的标识符
我把分页的两句
quMessage.setFirstResult(beginNumber);
quMessage.setMaxResults(numberOfEachPage);
去掉就好了,难道hibernate分页就不能加锁了?
请高人指点。
这是MessageEntity
public class MessageEntity implements Serializable {
private static final long serialVersionUID = -9876321L;
private String mid;
private Calendar messageDate;
private String messageChannel;
private String messageEmail;
private String messageTitle;
private String messageContent;
private String messageReply;
private String messageStatus;
public MessageEntity() {
}
private void readObject(ObjectInputStream ois) throws IOException,
ClassNotFoundException {
ois.defaultReadObject();
}
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
}
public void setMid(String mid) {
this.mid = mid;
}
public void setMessageDate(Calendar messageDate) {
this.messageDate = messageDate;
}
public void setMessageChannel(String messageChannel) {
this.messageChannel = messageChannel;
}
public void setMessageEmail(String messageEmail) {
this.messageEmail = messageEmail;
}
public void setMessageTitle(String messageTitle) {
this.messageTitle = messageTitle;
}
public void setMessageContent(String messageContent) {
this.messageContent = messageContent;
}
public void setMessageReply(String messageReply) {
this.messageReply = messageReply;
}
public void setMessageStatus(String messageStatus) {
this.messageStatus = messageStatus;
}
public String getMid() {
return mid;
}
public Calendar getMessageDate() {
return messageDate;
}
public String getMessageChannel() {
return messageChannel;
}
public String getMessageEmail() {
return messageEmail;
}
public String getMessageTitle() {
return messageTitle;
}
public String getMessageContent() {
return messageContent;
}
public String getMessageReply() {
return messageReply;
}
public String getMessageStatus() {
return messageStatus;
}
}
<?xml version= "1.0 "?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN " "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
<hibernate-mapping package= "message.hibernate ">
<class name= "MessageEntity " table= "MESSAGE " lazy= "false ">
<comment> Message elements. </comment>
<!--
<id name= "MID ">
<generator class= "increment "/>
</id>
-->
<property name= "messageId " column= "MID " type= "string " not-null= "true "/>
<property name= "messageDate " column= "MDATE " type= "date " not-null= "true "/>
<property name= "messageChannel " column= "MCHANNEL " type= "string " not-null= "true "> </property>
<property name= "messageEmail " column= "MEMAIL " type= "date "> </property>
<property name= "messageTitle " column= "MTITLE " type= "string "> </property>
<property name= "messageContent " column= "MCONTENT " type= "string "> </property>
<property name= "messageReply " column= "MREPLY " type= "string "> </property>
<property name= "messageStatus " column= "MSTATUS " type= "string "> </property>
</class>
</hibernate-mapping>
[解决办法]
好像sql语句有问题,把for update of messageent0_.mid nowait去掉试试