使用字段ID而不提取实体

时间:2016-12-27 12:58:57

标签: java hibernate jpa fetch

我有两个与某个字段相关的实体,如果没有记录整个父记录,我就无法获取父节点的id。

例如:

@Entity
public class Country implements Serializable {
    @Id
    @Column(name="COD_COUNTRY")
    private String codCountry;

    private String nameCountry;
}

@Entity
public class User implements Serializable {
    @EmbeddedId
    private UserPK id;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="COD_COUNTRY")
    private Country country;
}

然后我选择了这个:

SELECT U.ID, U.NAME, U.COD_COUNTRY FROM TB_USERS U WHERE U.ID = 25

当我调试结果时,我可以看到检索到的字段country已准备好被提取,但我只想要在handler变量中显示的ID:

lastUser    User  (id=559)  
    country Country_$$_jvstaaf_f  (id=566)  
        codCountry  null    
        handler JavassistLazyInitializer  (id=578)  
            componentIdType null    
            constructed true    
            entityName  "com.company.project.persistence.entity.Country" (id=583)   
            getIdentifierMethod null    
            id  "ITA" (id=584)
            initialized true    
            interfaces  Class<T>[1]  (id=585)   
            overridesEquals false   
            persistentClass Class<T> (com.company.project.persistence.entity.Country) (id=126)  
            readOnly    false   
            readOnlyBeforeAttachedToSession null    
            replacement null    
            session null    
            sessionFactoryUuid  null    
            setIdentifierMethod null    
            specjLazyLoad   false   
            target  Country  (id=593)   
            unwrap  false   
        nameCountry null    
    userFields  "bla bla" (id=567)  

如何在不影响整个国家/地区记录的情况下获取用户ID?

谢谢:)

0 个答案:

没有答案