-->
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.  [ 5 posts ] 
Author Message
 Post subject: Why does Hibernate SELECT before INSERT and UPDATE?
PostPosted: Thu Mar 10, 2011 6:33 pm 
Newbie

Joined: Tue Feb 01, 2011 2:06 am
Posts: 8
I am using Hibernate 3.6. I have a simple OneToOne mapping between NewUser and UserStatusType.

Code:
@Entity
@Table( name = "USER" )
@org.hibernate.annotations.Entity( selectBeforeUpdate = false)
public class NewUser extends AbstractTimestampEntity implements Serializable  {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name= "user_id")
   private Long userId;
   
   @Column(name= "user_name")
   private String userName;
   
   @Column(name= "password")
   private String password;
   
   
   @OneToOne()
   @JoinColumn(name="user_status_type_id")
   private UserStatusType userStatusType;


Code:
@Entity
@Immutable
@Table( name = "USER_STATUS_TYPE" )
public class UserStatusType {

   @Id
   @Column(name= "user_status_type_id")      
   Long userStatusTypeId;

   @Column(name= "user_status_name")
   String userStatusName;
   
   public UserStatusType(){}
   public UserStatusType(long id){
      this.setUserStatusTypeId(id);
   }

   public Long getUserStatusTypeId() {
      return userStatusTypeId;
   }
   
   private void setUserStatusTypeId(Long userStatusTypeId) {
      this.userStatusTypeId = userStatusTypeId;
   }


Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
    <property name="show_sql">true</property>
    <property name="format_sql">false</property>
    <property name="hibernate.show.sql">true</property>
    <property name="connection.pool_size">1</property>
    <property name="current_session_context_class">thread</property>

    <!-- Mapping files will go here.... -->   
    <!--   mapping class="com.scd.entity.AbstractTimestampEntity"/ -->
    <mapping class="com.scd.dao.entity.NewUser"/>
    <mapping class="com.scd.dao.entity.UserRegistration"/>
    <mapping class="com.scd.dao.entity.UserStatusType"/>
  </session-factory>
</hibernate-configuration>


UserStatusType is just a lookup "type" table in the database, and that's why it's marked as immutable in the class. The problem I am having is that just before I insert a NewUser instance, or update a NewUser instance, Hibernate is doing a SELECT just before the INSERT and/or UPDATE and I don't know why. I'd like to prevent it for performance reasons so I added selectBeforeUpdate = false annotation in NewUser but this didn't make any difference.

Here is how I am inserting and updating. Am I missing an annotation or property in the XML config?

Code:
Session session = sessionFactory.openSession();
session.beginTransaction();
NewUser nu = new NewUser();
nu.setUserName(randomUserId);
nu.setPassword("123abc");
nu.setUserStatusType(new UserStatusType(101);
      
session.persist( nu);         
session.getTransaction().commit();


Top
 Profile  
 
 Post subject: Re: Why does Hibernate SELECT before INSERT and UPDATE?
PostPosted: Fri Mar 11, 2011 10:25 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

I suppose that the SELECT you're talking about is on the USER_STATUS_TYPE table ...
In your example this is due to :
Code:
new UserStatusType(101)

Since you are using a new, hibernate needs to verify if the corresponding object really exists in the database! That's why you have a SELECT!
You should load the UserStatusType from the DB. You can use the getReference() if you're sure the object exists instead of get() for performance issues.


Top
 Profile  
 
 Post subject: Re: Why does Hibernate SELECT before INSERT and UPDATE?
PostPosted: Fri Mar 11, 2011 2:47 pm 
Newbie

Joined: Tue Feb 01, 2011 2:06 am
Posts: 8
overmeulen wrote:
Hi,

I suppose that the SELECT you're talking about is on the USER_STATUS_TYPE table ...
In your example this is due to :
Code:
new UserStatusType(101)

Since you are using a new, hibernate needs to verify if the corresponding object really exists in the database! That's why you have a SELECT!
You should load the UserStatusType from the DB. You can use the getReference() if you're sure the object exists instead of get() for performance issues.


If I load all UserStatusType's from the database (there's only 3 rows), I would like to do that only once at the start of my application. Is there a way I can cache it and then use getReference() to assign an instance to my User object? A code snippet would be very helpful here.

Thank you very much, great info!


Top
 Profile  
 
 Post subject: Re: Why does Hibernate SELECT before INSERT and UPDATE?
PostPosted: Fri Mar 11, 2011 2:56 pm 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Sorry my previous post wasn't very clear ...
You don't even need to really "load" the UserStatusType from your DB!
If you use getReference() Hibernate will create a proxy for the correspondong object without actually fetching it in the database!
That's why you can only use it if you are sure that the object do exist in the database, otherwise you will have Exceptions!


Top
 Profile  
 
 Post subject: Re: Why does Hibernate SELECT before INSERT and UPDATE?
PostPosted: Sat Mar 12, 2011 1:49 am 
Newbie

Joined: Tue Feb 01, 2011 2:06 am
Posts: 8
overmeulen wrote:
Sorry my previous post wasn't very clear ...
You don't even need to really "load" the UserStatusType from your DB!
If you use getReference() Hibernate will create a proxy for the correspondong object without actually fetching it in the database!
That's why you can only use it if you are sure that the object do exist in the database, otherwise you will have Exceptions!


Ok, I got it working, but not with getReference() since that's used with EntityManager, I used Session.load() instead. load() gets a proxy for the object and, as long as I don't access any members from the proxy, HB does not hit the database with a SELECT for the proxy reference when INSERTING.

Thanks
SAFX


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