0

I have created a Database Access Object (DAO) class, entity class, and table script, but I am getting an error that cannot be mapped to entity.
I am using hibernate framework and the connections are made properly with the database but still error occurs. please check the code below and help in any ways you can, all the files are provided below.

Table Script

DROP TABLE rmc_user;
CREATE TABLE rmc_user(
user_id VARCHAR(50) NOT NULL,
user_name VARCHAR(20) NOT NULL,
user_email VARCHAR(50) NOT NULL,
user_password VARCHAR(20),
CONSTRAINT rmc_user_user_id_pk PRIMARY KEY (user_id),
CONSTRAINT rmc_user_user_email_un UNIQUE (user_email) 
);

INSERT INTO rmc_user VALUES   ('101','yashik','[email protected]','gulati123');

SELECT * FROM rmc_user;

DAO Class

package rmc.dao;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import rmc.bean.User;
import rmc.entity.UserEntity;
import rmc.resources.HibernateUtility;

public class LoginDAOImpl implements LoginDAO {

    @SuppressWarnings("deprecation")
    public User getUserDetails(String userName, String password) {
        SessionFactory sessionFactory = HibernateUtility.createSessionFactory();
        Session session = null;
        User u1 = null;
        session = sessionFactory.openSession();
        session.beginTransaction();
        System.out.println("begin trx");
        Query q1 = session
                .createNativeQuery("select * from rmc_user where user_name=?");
        System.out.println("begin trx");
        q1.setParameter(0, userName);
        System.out.println("begin trx");
        @SuppressWarnings("unchecked")
        List<UserEntity> l1 = q1.list();
        System.out.println("begin trx");
        System.out.println("size is"+l1.size());
        if (l1.size() == 0) {
            System.out.println("no Such user Exist");
        } else if (!(l1.get(0).getPassword().equals(password))) {
            System.out.println("Invalid Password");
        }
        System.out.println("begin trx");
        u1 = new User();
        u1.setEmail(l1.get(0).getEmail());
        u1.setPassword(l1.get(0).getPassword());
        u1.setUserId(l1.get(0).getUserId());
        u1.setUserName(l1.get(0).getUserName());
        session.getTransaction().commit();
        if (session != null) {
            session.close();
        }
        return u1;
    }
}

Entity Class

package rmc.entity;
@Id
@Column(name="user_id")
private String userId;
@Column(name="user_name")
private String userName;
@Column(name="user_email")
private String email;
@Column(name="user_password")
private String password;

//getter and setter 
}

Error Message

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to rmc.entity.UserEntity
at rmc.dao.LoginDAOImpl.getUserDetails(LoginDAOImpl.java:32)
at rmc.test.UserInterface.main(UserInterface.java:9)

UPDATED

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
    <property name="hibernate.dialect">
        org.hibernate.dialect.MySQLDialect
    </property>
    <property name="hibernate.connection.driver_class">
        com.mysql.jdbc.Driver
    </property>

    <!-- Assume test is the database name -->
    <property name="hibernate.connection.url">
        jdbc:mysql://localhost:3306/rmc
    </property>
    <property name="hibernate.connection.username">
        ******
    </property>
    <property name="hibernate.connection.password">
        ******
    </property>


    <!-- List of XML mapping files -->
    <mapping class="rmc.entity.UserEntity"/>

</session-factory>

2
  • does the exception occur on this line : List<UserEntity> l1=q1.list(); ? Commented Jul 2, 2016 at 17:46
  • no the error occurs at this line } else if (!(l1.get(0).getPassword().equals(password))) { Commented Jul 3, 2016 at 8:34

2 Answers 2

2

If your result row is compatible with UserEntity the following modification might solve your problem:

Query q1 = session
        .createNativeQuery("select * from rmc_user where user_name=?", UserEntity.class);
Sign up to request clarification or add additional context in comments.

Comments

0

The problem probably lies here

Query q1 = session.createNativeQuery("select * from rmc_user where user_name=?");

When you execute this query, because it's an sql query, the return list of q1.list() will have this format : Object[] { row1col1, row1col2,row1,col3,row2col1,...} that is it spreads the columns , it doesn't map row to entity(UserEntity).

It doesn't map because that's not HQL nor JPQL, that's native SQL and SQL doesn't know about your entities.

You should instead do this :

Query q1=session.createQuery("select * from UserEntity where user_name=?");

This is HQL and with this Hibernate will map every row to entity so the return list of q1.list() will now have format : UserEntity[]{entity1,entity2,...}.

I hope that solves your issue.

4 Comments

hi thank you for the response, I did some changes but some error occurs, can you please take a look? org.hibernate.hql.internal.ast.QuerySyntaxException: UserEntity is not mapped [FROM UserEntity u where u.userName=?]
also the has been edited Query q1 = session .createQuery("FROM UserEntity u where u.userName=?"); q1.setParameter(0, userName); List<UserEntity> l1 = q1.list();
@Yashik are you by a chance using @Entity annotation from org.hibernate package ? you should use the one coming from javax.persistance package, see this
hmm strange, see this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.