This content has been marked as final.
Show 3 replies
-
1. Re: EJB3 entity bean query always returns null
mxshrestha Mar 11, 2009 1:01 PM (in response to mxshrestha)"mxshrestha" wrote:
I have had this problem for a month now. I am trying to execute a JPA query from a session bean, and everytime it returns only null. Other functionalities of the entity bean like persist() works, and stores in database. What am i doing wrong here. My code is attached below:
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Stateless
public class LoginBean implements LoginBeanRemote, LoginBeanLocal {
public LoginBean() {
}
@PersistenceContext(unitName="CustomerUnit")
EntityManager manager;
public String validateLogin(String userName, String password)
{
try
{
Query q1 = manager.createQuery("SELECT c FROM Customer c WHERE c.userName = :username AND c.password = :password ");
q1.setParameter("username", userName);
q1.setParameter("password", password);
System.out.println(q1.getSingleResult());
}
catch(Exception e)
{
e.printStackTrace();
}
return "loginUser";
}
}
Also, my entity bean and persistence.xml is included. I am using oracle 10g.
package com.entities;
import java.io.Serializable;
import java.sql.Date;
import javax.persistence.*;
/**
* Entity implementation class for Entity: Customer
*
*/
@Entity
@Table(name="Customer")
@IdClass(com.entities.Customer.class)
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String userName;
private String password;
private String firstName;
private String middleInitial;
private String lastName;
private String email;
private Date birthDate;
private String homePhoneNo;
private String cellPhoneNo;
private Date lastLogin;
public Customer() {
super();
}
public Customer(String id, String userName, String password, String firstName, String middleInitial,
String lastName, String email, Date birthDate, String homePhoneNo, String cellPhoneNo,
Date lastLogin)
{
setId(id);
setUserName(userName);
setPassword(password);
setFirstName(firstName);
setMiddleInitial(middleInitial);
setLastName(lastName);
setEmail(email);
setBirthDate(birthDate);
setHomePhoneNo(homePhoneNo);
setCellPhoneNo(cellPhoneNo);
setLastLogin(lastLogin);
}
@Id
@Column(name="CUSTID")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Column(name="USERNAME")
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(name="PASSWORD")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name="FNAME")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name="MI")
public String getMiddleInitial() {
return middleInitial;
}
public void setMiddleInitial(String middleInitial) {
this.middleInitial = middleInitial;
}
@Column(name="LNAME")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Column(name="EMAIL")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Column(name="DOB")
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
@Column(name="HOMEPHONENO")
public String getHomePhoneNo() {
return homePhoneNo;
}
public void setHomePhoneNo(String homePhoneNo) {
this.homePhoneNo = homePhoneNo;
}
@Column(name="CELLPHONE")
public String getCellPhoneNo() {
return cellPhoneNo;
}
public void setCellPhoneNo(String cellPhoneNo) {
this.cellPhoneNo = cellPhoneNo;
}
@Column(name="LASTLOGIN")
public Date getLastLogin() {
return lastLogin;
}
public void setLastLogin(Date lastLogin) {
this.lastLogin = lastLogin;
}
}
persistence:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="CustomerUnit" transaction-type="JTA">
<jta-data-source>java:/OracleDS</jta-data-source>
<class>com.entities.Customer</class>
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/>
</properties>
</persistence-unit>
</persistence> -
2. Re: EJB3 entity bean query always returns null
jaikiran Mar 12, 2009 7:50 AM (in response to mxshrestha)Query q1 = manager.createQuery("SELECT c FROM Customer c WHERE c.userName = :username AND c.password = :password ");
q1.setParameter("username", userName);
q1.setParameter("password", password);
System.out.println(q1.getSingleResult());
Are you sure the database has an record for this username and password? And are you sure its not case-sensitive? -
3. Re: EJB3 entity bean query always returns null
mxshrestha Mar 12, 2009 8:46 PM (in response to mxshrestha)Yes, I am sure the database exists in the system, and even when i hard code the user name and the password, the query returns null. The database system i am using is oracle 10g and as far as i know it is not case sensitive. Do you think it has anything to do with my persistence.xml file and if not what is the problem???