简体   繁体   English

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

[英]not getting data from multiple 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 2 other tables references and messgaes.In java project i have referances.java and Messages.java and i have created the mapping tables as user_referances_mapping(user_id, referance_id) and user_messages_mapping(user_id, reference_id). 我面临的问题是:我以用户身份在db中有一个表,以user.java在java项目中有一个表,并且我还有2个其他表引用和messgaes。在java项目中我有referenceances.java和Messages.java,并且我已经创建了映射表为user_referances_mapping(user_id,referance_id)和user_messages_mapping(user_id,reference_id)。

and i have one to many relationship between user and referances table and user and messages table. 我和用户和引用表之间以及用户和消息表之间有一对多关系。

I want to get data from both references and messgaes table when i am getting user data. 当我获取用户数据时,我想同时从引用和消息表中获取数据。

when i am using "FROM User u LEFT JOIN FETCH u.referances WHERE u.contactNo=:contactNo or u.email=:email" i get reference table data. 当我使用“ FROM用户u左侧联接u。引用WHERE u.contactNo =:contactNo或u.email =:email”时,我获得了参考表数据。

So can anyone help me getting the solution, that how i can get the data of the referances table and messages table at the same time and what should be the solution to it. 因此,任何人都可以帮助我获得解决方案,即如何同时获取引用表和消息表的数据,以及应该如何解决。

mapping of referances table and messgaes 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>();

@OneToMany
@Basic(optional = true)
@BatchSize(size = 5)
@Cascade(CascadeType.ALL)
@Cache(region = IAppConstants.CACHE_MESSAGES, usage = CacheConcurrencyStrategy.READ_WRITE)
@JoinTable(name = "user_messages_mapping", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "message_id") })
private List<Messages> messages = new ArrayList<Messages>();

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;

} }

Use @LazyCollection(LazyCollectionOption.FALSE) 使用@LazyCollection(LazyCollectionOption.FALSE)

with the relationship entry in the User class for messages. 与消息的用户类中的关系条目。

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

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