[jboss-user] [EJB 3.0] - Re: EJB3 entity bean query always returns null

mxshrestha do-not-reply at jboss.com
Wed Mar 11 13:01:13 EDT 2009


"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>
  | 		com.entities.Customer
  | 		
  |         	 
  |           	 
  |           	
  |             
  | 		
  | 	</persistence-unit>
  | 
  | 
  | 

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4217074#4217074

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4217074



More information about the jboss-user mailing list