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();