Hibernate OneToOne映射在插入之前执行select语句;不知道为什么 [英] Hibernate OneToOne mapping executes select statement before insert; not sure why

查看:158
本文介绍了Hibernate OneToOne映射在插入之前执行select语句;不知道为什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的实体中配置了一个简单的OneToOne映射,但是当我坚持这个对象时,我看到一个选择语句由Hibernate在Insert之前执行,我不知道为什么。

  @Entity 
@Table(name =USER)
public class NewUser {

@OneToOne
@JoinColumn(name =user_status_type_id,nullable = false)
private UserStatusType userStatusType;

UserStatusType只是一个只读查询表,所以当我保存User时没有任何东西存在于表中。

  User u = new User(); 
u.setUserStatusType(new UserStatusType(101));
session.persis(u);

但是,当我坚持User对象时,Hibernate的输出如下所示:

  Hibernate:从USER_STATUS_TYPE userstatus_中选择userstatus_.user_status_type_id,userstatus_.user_status_name作为user2_3_,其中userstatus_.user_status_type_id =? 
Hibernate:插入到USER(创建的,名字,姓氏,密码,用户名,用户状态类型)值(?,?,?,?,?,?)

我不知道Hibernate是否正常。我想,因为这是一个坚持的操作,我只会看到一个插入语句;不确定选择的目的。



映射是否不正确?

解决方案

  User u = new User(); 
u.setUserStatusType(session.load(UserStatusType.class,new Long(id));
session.persis(u);

session.load()方法最初不会加载任何东西,它只是为对象创建一个代理并避免命中数据库。在会话上下文中的一个成员(除id字段外),那么Hibernate将会去数据库。在我的情况下,我只需要INSERT的引用对象的主键,所以我避免了数据库命中。


I configured a simple OneToOne mapping in my entity, but when I persist the object I see a "Select" statement being executed by Hibernate just before the Insert and I don't know why.

@Entity 
@Table( name = "USER" )
public class NewUser {

   @OneToOne
   @JoinColumn(name="user_status_type_id", nullable=false)
   private UserStatusType userStatusType;

UserStatusType is just a read-only lookup table, so nothing persists in that table when I save User.

User u = new User();
u.setUserStatusType( new UserStatusType(101));
session.persis(u);

But when I persist the User object, the output from Hibernate reads as follows:

Hibernate: select userstatus_.user_status_type_id, userstatus_.user_status_name as user2_3_ from USER_STATUS_TYPE userstatus_ where userstatus_.user_status_type_id=?
Hibernate: insert into USER (created, first_name, last_name, password, user_name, user_status_type_id) values (?, ?, ?, ?, ?, ?)

I don't know if that's normal for Hibernate. I figured since it was a "persist" operation I would only see an Insert statement; not sure of the purpose for the Select.

Is the mapping incorrect?

解决方案

OK, I was able to get an answer from the Hibernate forums. To prevent the SELECT before INSERT, if you are sure the referenced row exists in the database, then use Session.load() or EntityManager.getReference().

User u = new User();
u.setUserStatusType( session.load(UserStatusType.class, new Long(id));
session.persis(u);

The session.load() method doesn't initially load anything, it just creates a proxy for the object and avoids a hit to the database. Now, if you want to access a member (other than the id field) from the class within the context of the session, then Hibernate will go to the database. In my case I just needed the primary key from the referenced object for the INSERT, so I avoided the database hit.

这篇关于Hibernate OneToOne映射在插入之前执行select语句;不知道为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆