Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i want query two table in hibernate . featch 3 table (User - Role - Profile ) in user Entity . query with hql :

query= "select ue, ue.roleEntity.roleId from UserEntity ue ,RoleEntity re  fetch all properties where ue.roleEntity.roleId=re.roleId and ue.username ='reza' and ue.password='123456'";

and run query :

  try {
        sessionFactory = HibernateUtil.getSessionFactory();
        session = sessionFactory.getCurrentSession();
        transaction = session.beginTransaction();
        userEntityList = (List<UserEntity>) session.createQuery(query).list();
        transaction.commit();
    } catch (HibernateException e) {
        try {
            throw new DaoException("Fetal exception in", e);
        } catch (DaoException e1) {
            e1.printStackTrace();
        }
    }

userentity class : this class is geteer and seter :

public class UserEntity {
private int userId;
private long personalCode;
private String username;
private String password;
private short active;
private String question;
private String passive;
private ProfileEntity  profileEntity;
private RoleEntity roleEntity;

hibernate maping for userEntity.hbm.xml

        <?xml version="1.0" encoding="utf-8" ?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

    <hibernate-mapping package="entity">
        <class name="UserEntity" table="TABLE_USER">
            <id name="userId" type="java.lang.Integer" column="USER_ID">
                <generator class="increment" />
            </id>

            <property name="personalCode" type="java.lang.Long" column="PERSONALCODE">

            </property>

            <property name="username" type="java.lang.String" column="USERNAME">

            </property>

            <property name="password" type="java.lang.String" column="PASSWORD">

            </property>

            <property name="active" type="java.lang.Short" column="ACTIVE">

            </property>

            <property name="question" type="java.lang.String" column="QUCTION">

            </property>

            <property name="passive" type="java.lang.String" column="PASSIVE">

            </property>


            <many-to-one name="roleEntity" class="entity.RoleEntity" column="ROLE_ID" cascade="none" fetch="select" />
            <many-to-one name="profileEntity" class="ProfileEntity" cascade="delete" column="profile_id"/>

    </class>
</hibernate-mapping>

and class hibernateutil for create sesstion :

  import org.hibernate.SessionFactory;
    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
    import org.hibernate.cfg.Configuration;

    public class HibernateUtil {
        private static SessionFactory sessionFactory;

        static {
            try {
                Configuration configuration = new Configuration().configure();
                StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
                sessionFactory = configuration.buildSessionFactory(builder.build());
            } catch (Throwable th) {

                System.err.println("Enitial SessionFactory creation failed" + th);

                throw new ExceptionInInitializerError(th);

            }

        }

        /**
         * @return
         */
        public static SessionFactory getSessionFactory() {

            return sessionFactory;

        }
    }
share|improve this question
    
It will return you a List<Object> which will inturn contain your data. If you want UserEntity to be returned then just use "select ue from UserEntity ue ,RoleEntity re fetch all properties where ue.roleEntity.roleId=re.roleId and ue.username ='reza' and ue.password='123456'" –  mprabhat Dec 3 '14 at 20:22
    
Why you catch a HibernateException and do nothing more than throwing a DaoException which re-throws the causing exception? catch (HibernateException e) { try { throw new DaoException("Fetal exception in", e); } catch (DaoException e1) { e1.printStackTrace(); } } –  Sal Dec 3 '14 at 23:09
    
hi mprabhat . I want all the data table for role and user get. –  reza rostami Dec 4 '14 at 5:48

1 Answer 1

up vote 1 down vote accepted

Because you are using a multi-selection projection, you are actually fetching an array of objects, so you need to change the query result processing logic to:

List<Object[]> tuples = (List<Object[]>) session.createQuery(query).list();

for(Object[] tuple : tuples) {
    UserEntity ue = tuple[0];
    Number roleId = tuple[1];
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.