简体   繁体   English

无法使用休眠从映射表获取数据

[英]not getting data from the mapping table using hibernate

Hi i am new to hibernate. 嗨,我是新来的冬眠。

I have a java web project and i am using spring mvc with hibernate in this project and for database i am using my sql. 我有一个Java Web项目,并且在此项目中将Spring MVC与Hibernate一起使用,对于数据库,我正在使用SQL。

The issue i am facing is that : i have a table in db as user and in java project as user.java and i have another table references and in java project i have referances.java and i have created a mapping table as user_referances_mapping(user_id, referance_id). 我面临的问题是:我以用户身份在db中有一个表,以user.java在java项目中有一个表,并且我有另一个表引用,在java项目中我有referenceances.java,并且我已经创建了一个映射表为user_referances_mapping(user_id ,referance_id)。

and i have one to many relationship between user and referances table. 而且我和用户表之间有一对多的关系。

Now when i try to get the user data it gives me user table coloumns data but not the referances data and returns only null list for referances table. 现在,当我尝试获取用户数据时,它为我提供了用户表列数据,但没有给参考表数据,并且仅为参考表返回空列表。

On the other hand i also have similar mapping tables with user table like role, address with one to many mapping only and data is getting retrieved from those tables. 另一方面,我也有类似的映射表,其中包含角色,用户表等用户表,仅具有一对多映射的地址,并且从这些表中检索数据。

So can anyone help me getting the solution, that why i am not getting the data of the referances table using the one to many relationship and what should be the solution to it. 任何人都可以帮助我获得解决方案,这就是为什么我不使用一对多关系来获取引用表的数据的原因以及应该采取什么解决方案。

mapping of referances table in user table: 用户表中引用表的映射:

@OneToMany
@Basic(optional = true)
@BatchSize(size = 5)
@Cascade(CascadeType.ALL)
@Cache(region = IAppConstants.CACHE_REFERANCES, usage = CacheConcurrencyStrategy.READ_WRITE)
@JoinTable(name = "user_referances_mapping", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "referance_id") })
private List<Referances> referances = new ArrayList<Referances>();

public List<Referances> getReferances() {
    return referances;
}

public void setReferances(List<Referances> referances) {
    this.referances = referances;
}

UserDao class function : UserDao类函数:

public User getC2SUserByContactNoOrEmail(final String value) throws ApplicationException {
    try{
    @SuppressWarnings("unchecked")
    Query query = currentSession().createQuery(
            IQueryConstants.FETCH_USER_BY_CONTACTNO_OR_EMAIL);
    query.setParameter("contactNo", value);
    query.setParameter("email", value);
    return (User) query.uniqueResult();
    }catch(Exception e){
        throw new ApplicationException(
                "Issue occurred while fetching user by: " + value, e);
    }
    //return null;
}

FETCH_USER_BY_CONTACTNO_OR_EMAIL = "FROM User WHERE contactNo=:contactNo or email=:email"; FETCH_USER_BY_CONTACTNO_OR_EMAIL =“从FROM用户那里contactNo =:contactNo或email =:email”;

If I'm right, the OneToMany relations are defined as "lazy" by default, which means you need to explicitly state that you want to fetch the related records. 如果我是对的,则默认情况下将OneToMany关系定义为“惰性”,这意味着您需要明确声明要获取相关记录。

Try modifying the query like this: 尝试像这样修改查询:

FETCH_USER_BY_CONTACTNO_OR_EMAIL = "FROM User u LEFT JOIN FETCH u.referances WHERE u.contactNo=:contactNo or u.email=:email";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM