我遇到了一个无法找到答案的大问题。
我在两个实体之间有一个OneToOne双向Lazy获取关系,但当我查询其中一个时,它急切地获取另一个,即使我明确地说它是懒惰的。
根据我的发现,几乎所有人都从2010年开始回来说,这就是hibernate在OneToOne关系上的表现方式。这是我第一次使用Hibernate,但我非常怀疑这是真的,如果它仍然是真的。但我找不到任何有关它的最新信息。 Post1,Post2,Post3如您所见,它们并不是最近的。所以我想知道是否仍然如此,或者可以指导我到哪里找到答案,例子,任何事情都会有所帮助。
这就是我正在做的事情,以防万一我做错了什么。 请注意,类的属性比原始属性少,但它们都是实体的列,没有FK,PK或类似的东西。
用户实体
@Entity
@Table(name = "\"User\"")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Id", unique = true, nullable = false)
private long id;
@Column(name = "Username", nullable = false, unique = true, length = 100)
private String username;
@Valid
@OneToOne(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL)
@JsonBackReference
private UserProfile userProfile;
//Getters and Setters
}
UserProfile实体
@Entity
@Table(name = "UserProfile")
public class UserProfile {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@NotBlank
@Size(max = 100)
@Column(name = "FirstName", nullable = false, unique = false, length = 100)
private String firstName;
@NotBlank
@Size(max = 100)
@Column(name = "LastName", nullable = false, unique = false, length = 100)
private String lastName;
@Valid
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "UserId")
@JsonManagedReference
private User user;
//Getters and Setters
}
测试控制器方法
@RequestMapping(value = "/test", method = RequestMethod.GET)
public ResponseEntity<Void> test(){
User user = userService.findUserByUsername("admin");
if(user.getUserProfile() == null){
return new ResponseEntity<Void>(HttpStatus.OK);
}
return new ResponseEntity<Void>(HttpStatus.CONFLICT);
}
UserService只调用UserDao方法,即
@Override
public User findByUsername(String username) {
Criteria criteria = createEntityCriteria();
criteria.add(Restrictions.eq("username", username));
return (User) criteria.uniqueResult();
}
该服务的结果始终是冲突。即使我正在调用UserService(这是Transactional),并且直接关系在UserProfile中。控制器不是交易性的。
日志:
2016-02-29 18:50:58 DEBUG SQL::logStatement:92 -
select
this_.Id as Id1_2_0_,
this_.Username as Username5_2_0_
from
Public.[User] this_
where
this_.Username=?
Hibernate:
select
this_.Id as Id1_2_0_,
this_.Username as Username5_2_0_
from
Public.[User] this_
where
this_.Username=?
2016-02-29 18:50:58 DEBUG Loader::debugf:384 - Result set row: 0
2016-02-29 18:50:58 DEBUG Loader::getRow:1514 - Result row: EntityKey[com.portal.model.user.User#1]
2016-02-29 18:50:58 DEBUG TwoPhaseLoad::doInitializeEntity:144 - Resolving associations for [com.portal.model.user.User#1]
2016-02-29 18:50:58 DEBUG Loader::loadEntity:2186 - Loading entity: [com.portal.model.user.UserProfile#1]
2016-02-29 18:50:58 DEBUG SQL::logStatement:92 -
select
userprofil0_.id as id1_18_0_,
userprofil0_.FirstName as FirstNam5_18_0_,
userprofil0_.LastName as LastName7_18_0_,
userprofil0_.UserId as UserId10_18_0_
from
Public.UserProfile userprofil0_
where
userprofil0_.UserId=?
Hibernate:
select
userprofil0_.id as id1_18_0_,
userprofil0_.FirstName as FirstNam5_18_0_,
userprofil0_.LastName as LastName7_18_0_,
userprofil0_.UserId as UserId10_18_0_
from
Public.UserProfile userprofil0_
where
userprofil0_.UserId=?
有什么问题吗?我做错了吗?
感谢您提供的任何帮助!如果您需要更多信息,请告诉我们!