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','yas@gmail.com','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>