17

Is there anybody help me in this case. I am using hibernate for select data from db, but when I check from sql debug log. I always see the update sql printed after I use "select" sql to get data from DB.

2013-08-13 13:39:08,054 DEBUG [http-0.0.0.0-8080-1-TASK]-[org.hibernate.SQL]
SELECT this_.id                                            AS id504_2_,
    this_.bridgedlinedialoguri                          AS bridgedL2_504_2_,
    this_.bridgedlineuri                                AS bridgedL3_504_2_,
    this_.currentsipsubscriberprofile                   AS currentS4_504_2_,
    this_.currentsipsubscriberprofiletype               AS currentS5_504_2_,
    this_.messageeventuri                               AS messageE6_504_2_,
    this_.nemobjectid                                   AS neMobjec7_504_2_,
    this_.objectid                                      AS objectId504_2_,
    this_.objecttype                                    AS objectType504_2_,
    this_.sipbridgedlineurienable                       AS sipBrid10_504_2_,
    this_.sipdirectconnecturi                           AS sipDire11_504_2_,
    this_.sipdirectconnecturienable                     AS sipDire12_504_2_,
    this_.sipidentityaddress                            AS sipIden13_504_2_,
    this_.sipidentitycontacturi                         AS sipIden14_504_2_,
    this_.sipidentitypassword                           AS sipIden15_504_2_,
    this_.sipidentityrealm                              AS sipIden16_504_2_,
    this_.sipiotparametersenablestatus                  AS sipIotP17_504_2_,
    this_.sipiotparametersprivacy                       AS sipIotP18_504_2_,
    this_.sipiotparametersrequire100rel                 AS sipIotP19_504_2_,
    this_.sipiotparameterssupport100rel                 AS sipIotP20_504_2_,
    this_.sipmessageeventurienable                      AS sipMess21_504_2_,
    this_.sippreferredid                                AS sipPref22_504_2_,
    this_.sippreferredidmode                            AS sipPref23_504_2_,
    this_.sipsubscribertemplateenable                   AS sipSubs24_504_2_,
    this_.sipusername                                   AS sipUser25_504_2_,
    this_.versipsubscriberprofile_id                    AS verSipS26_504_2_,
    versioneds2_.id                                     AS id507_0_,
    versioneds2_.configurationprofileid                 AS configur4_507_0_,
    versioneds2_.description                            AS descript2_507_0_,
    versioneds2_.version                                AS version507_0_,
    configurat3_.id                                     AS id505_1_,
    configurat3_.description                            AS descript2_505_1_,
    configurat3_.NAME                                   AS name505_1_,
    configurat3_.type                                   AS type505_1_,
    profilemob4_.versionedsipsubscriberconfigprofile_id AS Versione1_507_4_,
    profilemob4_.element                                AS element4_
FROM   test this_
    LEFT OUTER JOIN versionedsipsubscriberconfigprofile versioneds2_
                    ON this_.versipsubscriberprofile_id = versioneds2_.id
    LEFT OUTER JOIN configurationprofile configurat3_
                    ON versioneds2_.configurationprofileid = configurat3_.id
    LEFT OUTER JOIN versionedsipsubscriberconfigprofile_profilemobjectids
                    profilemob4_
                    ON versioneds2_.id =
                    profilemob4_.versionedsipsubscriberconfigprofile_id
WHERE  this_.objectid = ?  

2013-08-13 13:39:08,103 DEBUG [http-0.0.0.0-8080-1-TASK]-[org.hibernate.SQL] 
UPDATE test
SET    bridgedlinedialoguri = ?,
    bridgedlineuri = ?,
    currentsipsubscriberprofile = ?,
    currentsipsubscriberprofiletype = ?,
    messageeventuri = ?,
    nemobjectid = ?,
    objectid = ?,
    objecttype = ?,
    sipbridgedlineurienable = ?,
    sipdirectconnecturi = ?,
    sipdirectconnecturienable = ?,
    sipidentityaddress = ?,
    sipidentitycontacturi = ?,
    sipidentitypassword = ?,
    sipidentityrealm = ?,
    sipiotparametersenablestatus = ?,
    sipiotparametersprivacy = ?,
    sipiotparametersrequire100rel = ?,
    sipiotparameterssupport100rel = ?,
    sipmessageeventurienable = ?,
    sippreferredid = ?,
    sippreferredidmode = ?,
    sipsubscribertemplateenable = ?,
    sipusername = ?,
    versipsubscriberprofile_id = ?
WHERE  id = ?  

Thanks

Edit: Code for getting the data:

 @Override public MObjectId findResult(final MObjectId id, boolean isLocked)
 { 
    Criteria c = getSession().createCriteria(Test.class); 
    c.add(Expression.eq("objectId", id)); 
    if(isLocked)
    { 
      c.setLockMode("this", LockMode.UPGRADE); 
    } 
    Test extension = (Test) c.uniqueResult(); 
    return (extension != null)
      ? new MObjectId("/ontPotsSipExtNr=" + extension.getId()) 
      : null; 
 } 

And entity class as below:

@Entity
public class OntPotsSipExtension implements Serializable {

private static final long serialVersionUID = 1L;


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

public OntPotsSipExtension() {

}

@Column(unique = true, nullable = false)
@Type(type = "com.container.persistence.MObjectIdUserType")
private MObjectId objectId;

@Column(unique = false, nullable = true)
@Type(type = "com.container.persistence.MObjectTypeUserType")
private MObjectType objectType;

private ToggleSwitch sipSubscriberTemplateEnable = ToggleSwitch.DISABLE;

@ManyToOne
private VersionedSipSubscriberConfigProfile verSipSubscriberProfile = null;

@Column(unique = false, nullable = true)
@Type(type = "com.container.persistence.MObjectIdUserType")
private MObjectId neMobjectId;
private String sipIdentityAddress;
private String sipIdentityRealm;
private Password sipIdentityPassword;
private String sipIdentityContactURI;

private String sipUsername;

private ToggleSwitch sipDirectConnectUriEnable = ToggleSwitch.DISABLE;
@EnabledExpression("sipDirectConnectUriEnable eq 'ENABLE'")
private String sipDirectConnectUri;

private ToggleSwitch sipMessageEventUriEnable = ToggleSwitch.DISABLE;
@EnabledExpression("sipMessageEventUriEnable eq 'ENABLE'")
private String messageEventUri;

private ToggleSwitch sipBridgedLineUriEnable = ToggleSwitch.DISABLE;
@EnabledExpression("sipBridgedLineUriEnable eq 'ENABLE'")
private String bridgedLineUri;
@EnabledExpression("sipBridgedLineUriEnable eq 'ENABLE'")
private String bridgedLineDialogUri;

@Column(unique = false, nullable = true)
@Type(type = "com.container.persistence.MObjectIdUserType")
private MObjectId currentSipSubscriberProfile;
@Column(unique = false, nullable = true)
@Type(type = "com.container.persistence.MObjectTypeUserType")
private MObjectType currentSipSubscriberProfileType;
/* SIP IOT Parameters*/
private PrivacySettings sipIotParametersPrivacy = PrivacySettings.NULL;
private ToggleSwitch sipIotParametersEnableStatus = ToggleSwitch.ENABLE;
private ToggleSwitch sipPreferredIDMode = ToggleSwitch.DISABLE;
private String sipPreferredID;
private ToggleSwitch sipIotParametersSupport100Rel = ToggleSwitch.DISABLE ;
private ToggleSwitch sipIotParametersRequire100Rel = ToggleSwitch.DISABLE;

public String getBridgedLineDialogUri() {
return bridgedLineDialogUri;
}
public void setBridgedLineDialogUri(String bridgedLineDialogUri) {
this.bridgedLineDialogUri = bridgedLineDialogUri;
}
public String getBridgedLineUri() {
return bridgedLineUri;
}
public void setBridgedLineUri(String bridgedLineUri) {
this.bridgedLineUri = bridgedLineUri;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getMessageEventUri() {
return messageEventUri;
}
public void setMessageEventUri(String messageEventUri) {
this.messageEventUri = messageEventUri;
}
public MObjectId getObjectId() {
return objectId;
}
public void setObjectId(MObjectId objectId) {
this.objectId = objectId;
}
public String getSipDirectConnectUri() {
return sipDirectConnectUri;
}
public void setSipDirectConnectUri(String sipDirectConnectUri) {
this.sipDirectConnectUri = sipDirectConnectUri;
}
public String getSipIdentityAddress() {
return sipIdentityAddress;
}
public void setSipIdentityAddress(String sipIdentityAddress) {
this.sipIdentityAddress = sipIdentityAddress;
}
public String getSipIdentityContactURI() {
return sipIdentityContactURI;
}
public void setSipIdentityContactURI(String sipIdentityContactURI) {
this.sipIdentityContactURI = sipIdentityContactURI;
}
public Password getSipIdentityPassword() {
return sipIdentityPassword;
}
public void setSipIdentityPassword(Password sipIdentityPassword) {
this.sipIdentityPassword = sipIdentityPassword;
}
public String getSipIdentityRealm() {
return sipIdentityRealm;
}
public void setSipIdentityRealm(String sipIdentityRealm) {
this.sipIdentityRealm = sipIdentityRealm;
}
public String getSipUsername() {
return sipUsername;
}
public void setSipUsername(String sipUsername) {
this.sipUsername = sipUsername;
}
public ToggleSwitch getSipSubscriberTemplateEnable() {
return sipSubscriberTemplateEnable;
}
public void setSipSubscriberTemplateEnable(
ToggleSwitch sipSubscriberTemplateEnable) {
this.sipSubscriberTemplateEnable = sipSubscriberTemplateEnable;
}

public VersionedSipSubscriberConfigProfile getVerSipSubscriberProfile() {
return verSipSubscriberProfile;
}
public void setVerSipSubscriberProfile(
VersionedSipSubscriberConfigProfile verSipSubscriberProfile) {
this.verSipSubscriberProfile = verSipSubscriberProfile;
//TODO
/*
* This needs to be done so that the Usage State changes to DEPLOY at the first
* instance this profile is used
*/
//this.subscriberProfile.setUsageState( UsageState.USAGE_DEPLOY );
}
public ToggleSwitch getSipBridgedLineUriEnable() {
return sipBridgedLineUriEnable;
}
public void setSipBridgedLineUriEnable(ToggleSwitch sipBridgedLineUriEnable) {
this.sipBridgedLineUriEnable = sipBridgedLineUriEnable;
}
public ToggleSwitch getSipDirectConnectUriEnable() {
return sipDirectConnectUriEnable;
}
public void setSipDirectConnectUriEnable(ToggleSwitch sipDirectConnectUriEnable) {
this.sipDirectConnectUriEnable = sipDirectConnectUriEnable;
}
public ToggleSwitch getSipMessageEventUriEnable() {
return sipMessageEventUriEnable;
}
public void setSipMessageEventUriEnable(ToggleSwitch sipMessageEventUriEnable) {
this.sipMessageEventUriEnable = sipMessageEventUriEnable;
}
public MObjectId getNeMobjectId() {
return neMobjectId;
}
public void setNeMobjectId(MObjectId neMobjectId) {
this.neMobjectId = neMobjectId;
}
public MObjectId getCurrentSipSubscriberProfile() {
return currentSipSubscriberProfile;
}
public void setCurrentSipSubscriberProfile(MObjectId currentSipSubscriberProfile) {
this.currentSipSubscriberProfile = currentSipSubscriberProfile;
}
public MObjectType getCurrentSipSubscriberProfileType() {
return currentSipSubscriberProfileType;
}
public void setCurrentSipSubscriberProfileType(
MObjectType currentSipSubscriberProfileType) {
this.currentSipSubscriberProfileType = currentSipSubscriberProfileType;
}
public MObjectType getObjectType() {
return objectType;
}
public void setObjectType(MObjectType objectType) {
this.objectType = objectType;
}
public PrivacySettings getSipIotParametersPrivacy() {
return sipIotParametersPrivacy;
}
public void setSipIotParametersPrivacy(PrivacySettings privacy) {
this.sipIotParametersPrivacy = privacy;
}
public ToggleSwitch getSipIotParametersEnableStatus() {
return sipIotParametersEnableStatus;
}
public void setSipIotParametersEnableStatus(ToggleSwitch sipIotParametersEnableStatus) {
this.sipIotParametersEnableStatus = sipIotParametersEnableStatus;
}
public ToggleSwitch getSipIotParametersSupport100Rel() {
return sipIotParametersSupport100Rel;
}
public void setSipIotParametersSupport100Rel(ToggleSwitch support100Rel) {
this.sipIotParametersSupport100Rel = support100Rel;
}
public ToggleSwitch getSipIotParametersRequire100Rel() {
return sipIotParametersRequire100Rel;
}
public void setSipIotParametersRequire100Rel(ToggleSwitch require100Rel) {
this.sipIotParametersRequire100Rel = require100Rel;
}
public ToggleSwitch getSipPreferredIDMode() {
return sipPreferredIDMode;
}
public void setSipPreferredIDMode(ToggleSwitch sipPreferredIDMode) {
this.sipPreferredIDMode = sipPreferredIDMode;
}
public String getSipPreferredID() {
return sipPreferredID;
}
public void setSipPreferredID(String sipPreferredID) {
this.sipPreferredID = sipPreferredID;
}
public Properties getProperties(boolean enableSipIotParams)
{
String QUOTES= "\"";
Properties props = new Properties();

props.setProperty("AddressOfRecord",QUOTES + ( getSipIdentityAddress() != null ? getSipIdentityAddress() : "" )+ QUOTES);
props.setProperty("Realm", QUOTES + ( getSipIdentityRealm() != null ? getSipIdentityRealm() : "" )+ QUOTES);
props.setProperty("Password", QUOTES + ( getSipIdentityPassword() != null ? getSipIdentityPassword().getPassword() : "" )+ QUOTES);
props.setProperty("ContactURIUser",QUOTES + ( getSipIdentityContactURI() != null ? getSipIdentityContactURI() : "" )+ QUOTES);
props.setProperty("username",QUOTES + ( getSipUsername() != null ? getSipUsername() : "" )+ QUOTES);
if ( getSipDirectConnectUriEnable() != null && getSipDirectConnectUriEnable().equals( ToggleSwitch.ENABLE ))
{
props.setProperty("DirectConnectURI",QUOTES + ( getSipDirectConnectUri() != null ? getSipDirectConnectUri() : "" )+ QUOTES);
}
if ( getSipMessageEventUriEnable() != null && getSipMessageEventUriEnable().equals(ToggleSwitch.ENABLE) )
{
props.setProperty("message_event_uri",QUOTES + ( getMessageEventUri() != null ? getMessageEventUri() : "" )+ QUOTES);
}
if ( getSipBridgedLineUriEnable() != null && getSipBridgedLineUriEnable().equals( ToggleSwitch.ENABLE ) )
{
props.setProperty("bridged_line_uri",QUOTES + ( getBridgedLineUri() != null ? getBridgedLineUri() : "" )+ QUOTES);
props.setProperty("bridged_line_dialog_uri",QUOTES + ( getBridgedLineDialogUri() != null ? getBridgedLineDialogUri() : "" )+ QUOTES);
}
if(enableSipIotParams && getSipIotParametersEnableStatus() != null && getSipIotParametersEnableStatus() == ToggleSwitch.ENABLE ){

String privacy="";
if ( getSipIotParametersPrivacy() != null)
{
if(getSipIotParametersPrivacy() == PrivacySettings.ID)
privacy="id";
else if(getSipIotParametersPrivacy() == PrivacySettings.NONE)
privacy="none";
else if(getSipIotParametersPrivacy() == PrivacySettings.NULL)
privacy="";
}

props.setProperty("Privacy", QUOTES + privacy + QUOTES);
props.setProperty("Support100Rel", QUOTES + ( getSipIotParametersSupport100Rel() != null ?
getSipIotParametersSupport100Rel().getValue() : ToggleSwitch.DISABLE.getValue() )+ QUOTES);
props.setProperty("Require100Rel", QUOTES + ( getSipIotParametersRequire100Rel() != null ?
getSipIotParametersRequire100Rel().getValue() : ToggleSwitch.DISABLE.getValue() )+ QUOTES);
props.setProperty("Mode", QUOTES + ( getSipPreferredIDMode() != null ?
getSipPreferredIDMode().getValue() : ToggleSwitch.DISABLE.getValue() ) + QUOTES);
props.setProperty("ID", QUOTES + ( getSipPreferredID() != null ? getSipPreferredID() : "" )+ QUOTES);
}

//TODO Check this
// if ( sipSubscriberTemplateEnable != null && sipSubscriberTemplateEnable.equals(ToggleSwitch.ENABLE) )
// {
// Properties templProps = subscriberProfile.getProperties();
// Enumeration<?> propNames = templProps.propertyNames();
//
// while ( propNames.hasMoreElements() )
// {
// Object key = propNames.nextElement() ;
// props.setProperty(key.toString(), templProps.getProperty( key.toString() ));
// }
// }
return props;
}
}
Rudi Wijaya
  • 872
  • 1
  • 10
  • 25
user1525949
  • 211
  • 1
  • 3
  • 10

4 Answers4

17

Most probably, one of the properties of your entity does not return the exact same value that is set by hibernate after loading. Do you have some if statements like null handling or anything like this in a getter or setter?

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
  • The below is my code for getting data. Please help me check it. @Override public MObjectId findResult(final MObjectId id, boolean isLocked) { Criteria c = getSession().createCriteria(Test.class); c.add(Expression.eq("objectId", id)); if(isLocked){ c.setLockMode("this", LockMode.UPGRADE); } Test extension = (Test) c.uniqueResult(); return (extension != null) ? new MObjectId("/ontPotsSipExtNr=" + extension.getId()) : null; } – user1525949 Aug 13 '13 at 10:51
  • 2
    I don't think that the problem is in the code to get the entity from the database. It is in the entity itself. – Stefan Steinegger Aug 13 '13 at 12:13
  • I just update my entity class. Could you please help me check? – user1525949 Aug 14 '13 at 05:46
  • 9
    To find the problem, turn on dynamic update using `@org.hibernate.annotations.Entity(dynamicUpdate = true)`. Check the update statement again. You see only the modified fields being updated. This helps finding the field that accidentally changed. – Stefan Steinegger Aug 14 '13 at 06:05
  • I try to turn on dynamicUpdate = true and see the log printed: 2013-08-14 16:24:38,163 DEBUG [RMI TCP Connection(9)-192.168.95.114]-[org.hibernate.SQL] update VoIPExtension set currentSipClientProfileType=?, currentSipSubscriberProfileType=? where id=? – user1525949 Aug 14 '13 at 09:32
  • 2
    Now you know that in the class VoIPExtension the values currentSipClientProfileType and currentSipSubscriberProfileType changed. Check your code where these changes may happen. – Stefan Steinegger Aug 14 '13 at 10:03
  • Really, I found out all the code and I didn't any code for updating the values of these above fields. – user1525949 Aug 15 '13 at 07:14
  • 2
    Many thanks Stefan. I know why the update statement is always called when selecting the data because its value is null. The type of attribute is configured as MObjectTypeUserType and Hibernate will base on the its equal method to decide the loaded object has change or not, but the equal method in my code is really wrong, it always return "false" if the object is null :( – user1525949 Aug 19 '13 at 02:23
  • 9
    For Hibernate 4.3 / JPA 2.1, use `@org.hibernate.annotations.DynamicUpdate` instead of `@Entity(dynamicUpdate=true)` – Rudi Wijaya Oct 17 '14 at 04:17
  • A persistent entity has been associated with a database table row and it’s being managed by the current running Persistence Context. Any change made to such entity is going to be detected and propagated to the database (during the Session flush-time). – Eric Dec 14 '17 at 00:59
  • I had getter with if(property == null){ return "" }. so i was retuning empty string . So hibernate had loaded null and i was translating to "" empty string. so and added just return property; – Reddymails Jun 27 '23 at 13:17
0

An upgrade lock. Objects loaded in this lock mode are materialized using an SQL select ... for update.

LockOption.Upgrade

It is same as select for update in sql

If you dont want select for update remove the following code and run again

if(isLocked)
{ 
  c.setLockMode("this", LockMode.UPGRADE); 
}
Community
  • 1
  • 1
MayurB
  • 3,609
  • 2
  • 21
  • 36
  • I have already tried to remove LockMode.UPGRADE, but the issue was not still resolved. The update sql still appears in sql log. – user1525949 Aug 13 '13 at 17:10
0

I had the same issue where a select statement after firing select would end up in a update statement. This happened after I moved from websphere 7.0 to Jboss 7.0.

The Object had String property where as select was returning "Y" or "N" so hibernate triggered an updated thinking the value of object was changed.

ALL I did was in in my hbm.xml I marked that property to update="false".

<property name="someStringProperty" type="string" update="false"/>
Reddymails
  • 793
  • 1
  • 10
  • 24
0

My problem was instead having an additional update after every insert.

In my case I have a UserType column (org.hibernate.usertype.UserType) declared into the Entity and into the UserType implementation I have to add this check into the equals method:

public boolean equals(Object left, Object right) throws HibernateException {
  if (left == null && right == null)    // added!
    return true;                        // added!
  if (!(left instanceof PGbox))
    return false;
  if (!(right instanceof PGbox))
    return false;
  return ((PGbox) left).equals((PGbox) right);
}

Without that check Hibernate is convinced that the column has changed because the equals method returns false, so it runs an update statement after the insert.

Michele
  • 1,213
  • 2
  • 18
  • 36