休眠-双向@OneToOne FetchType.LAZY不起作用

时间:2018-07-31 23:03:17

标签: java sql database hibernate

为什么FetchType.LAZY在“双向@OneToOne”关系中不起作用?

  • 休眠5.1.15.Final
  • 用户Togheter Spring 4.2.4.RELEASE

然后查找不具有属性FetchType.LAZY的对象,它执行2 select并加入一个属性mapead lazy。

可以在Google中找到很多方法,但是任何解决方案都可以解决问题。

/* sql script tables */
CREATE TABLE bidirectional_user (
    USR_ID BIGINT,
    USER_NAME VARCHAR(255),
    USER_DET_ID BIGINT,
    PRIMARY KEY (USR_ID)
);

CREATE TABLE bidirectional_user_detail (
    USER_DET_ID BIGINT,
    DATE_CREATE DATE,
    EMAIL VARCHAR(255),
    USR_ID BIGINT,
    PRIMARY KEY (USER_DET_ID),
    CONSTRAINT FK_USER_ID FOREIGN KEY (USR_ID) REFERENCES bidirectional_user(USR_ID)
);

Java类映射

@Entity
@Table(name = "bidirectional_user")
public class BidirectionalUser {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq")
    @SequenceGenerator(name = "seq", sequenceName = "sequence_all", allocationSize = 1)
    @Column(name = "usr_id", length = 20, precision = 0)
    private Long id;

    @Column(name = "user_name")
    private String name;

    @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private BidirectionalUserDetail userDetail;
}

@Entity
@Table(name = "bidirectional_user_detail")
public class BidirectionalUserDetail {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq")
    @SequenceGenerator(name = "seq", sequenceName = "sequence_all", allocationSize = 1)
    @Column(name = "user_det_id", length = 20, precision = 0)
    private Long id;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "usr_id", nullable = true)
    private BidirectionalUser user;

    @Column(name = "date_create")
    @Temporal(TemporalType.DATE)
    private Date dateCreate;

    @Column(name = "email")
    private String email;
}

测试类SpringJUnit4ClassRunner

@RunWith(SpringJUnit4ClassRunner.class)
public class SysMapBidirectionalTest extends SysSpringConfigContextGeneric {

    @Autowired
    private SysInterfaceGenericService genericService;

    @Test
    public void setUp() {
        Assert.assertNotNull(genericService);
    }

    @Test
    public void validarCascadeLazy() {
        final BidirectionalUser user = new BidirectionalUser();
        user.setName("USR011");
        final BidirectionalUserDetail userDetail = new BidirectionalUserDetail();
        userDetail.setEmail("teste@example.com");
        userDetail.setDateCreate(new Date());
        userDetail.setUser(user);

        user.setUserDetail(userDetail);

        genericService.save(user);
    }

    @Test(expected = LazyInitializationException.class)
    public void validLazyInitializationExceptionTest() {
        final BidirectionalUser user = new BidirectionalUser();
        user.setName("USR012");

        final BidirectionalUserDetail userDetail = new BidirectionalUserDetail();
        userDetail.setEmail("marcelo@example.com");
        userDetail.setDateCreate(new Date());
        user.setUserDetail(userDetail);

        userDetail.setUser(user);

        genericService.save(user);
        Assert.assertNotNull(user.getId());
        final DetachedCriteria criteria = DetachedCriteria.forClass(BidirectionalUser.class);
        criteria.add(Restrictions.eq("id", user.getId()));
        final BidirectionalUser hasLazy = genericService.findOne(criteria);
        hasLazy.getUserDetail().getId();
    }

    @Test
    public void validLazyNotInitializationExceptionTest() {
        final BidirectionalUser user = new BidirectionalUser();
        user.setName("USR013");

        final BidirectionalUserDetail userDetail = new BidirectionalUserDetail();
        userDetail.setEmail("mar@example.com");
        userDetail.setDateCreate(new Date());
        userDetail.setUser(user);

        user.setUserDetail(userDetail);

        genericService.save(user);
        Assert.assertNotNull(user.getId());
        final DetachedCriteria criteria = DetachedCriteria.forClass(BidirectionalUser.class);
        criteria.add(Restrictions.eq("id", user.getId()));
        criteria.setFetchMode("userDetail", org.hibernate.FetchMode.JOIN);
        final BidirectionalUser hasLazy = genericService.findOne(criteria);
        Assert.assertNotNull(hasLazy.getUserDetail().getId());
    }

    @Test
    public void xcludeAllTest() {
        final DetachedCriteria criteria = DetachedCriteria.forClass(BidirectionalUser.class);
        criteria.add(Restrictions.like("name", "USR01", MatchMode.START));
        final List<BidirectionalUser> users = genericService.findAll(criteria);
        Assert.assertTrue(users.size() > 0);
        try {
            genericService.delete(users);
        } catch (final Exception e) {
            e.printStackTrace();
        }
        final List<BidirectionalUser> usersClean = genericService.findAll(criteria);
        Assert.assertTrue(CollectionUtils.isEmpty(usersClean));
    }

}
  

方法validLazyInitializationExceptionTest应该= LazyInitializationException.class

此测试没有成功

0 个答案:

没有答案