aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes How to get user name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How to get user name" Watch "How to get user name" New topic
Author

How to get user name

Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 435
Hi,

Below are my two annotated classes, namely, Requisition and User, that represent two tables requisitions and users. What I want is that I get
to get username from users table in my requisition query instead of user id which is stored in the requisitions.request_by.

Table field requisitions.request_by hold the ids of users ,ie, users.id values.

Here are my tables:

users

+ id int(11)
username varchar(15)
password varchar(15)
role int(11)
last_login timestamp



requisitions

id int(11)
post varchar(50)
+ request_by int(11)
platform varchar(50)
experience_required int(11)
comments text
date_requested date




Here is my Query for fetching requsition object in the RequisitionServiceimpl.java

public List getRequisitions(User loggedInUser){

Query queryRequsitions = em.createQuery("Select req FROM Requisition as req");
List list = queryRequsitions.getResultList();
return list;
}




Below are my two annotated classes Requisition and User:

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;
import javax.persistence.JoinColumns;
import javax.persistence.JoinColumn;

@Table(name="requisitions")
@SecondaryTable(name="users")
@Entity
public class Requisition {

@Id
private int id;
private String post;

@Column(name = "request_by")
private int requestBy;

private String platform;

@Column(name = "experience_required")
private int experienceRequired;

private String comments;

@Column(name = "date_requested")
private Date dateRequested;


// Name from users Table

@Column(table="users", name="username")
private String username;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getPost() {
return post;
}

public void setPost(String post) {
this.post = post;
}

public int getRequestBy() {
return requestBy;
}

public void setRequestBy(int requestBy) {
this.requestBy = requestBy;
}

public String getPlatform() {
return platform;
}

public void setPlatform(String platform) {
this.platform = platform;
}

public int getExperienceRequired() {
return experienceRequired;
}

public void setExperienceRequired(int experienceRequired) {
this.experienceRequired = experienceRequired;
}

public String getComments() {
return comments;
}

public void setComments(String comments) {
this.comments = comments;
}

public Date getDateRequested() {
return dateRequested;
}

public void setDateRequested(Date dateRequested) {
this.dateRequested = dateRequested;
}

// Only Getter for Users Table field username
public String getUsername() {
return username;
}
}




//Users.java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name="users")
@Entity
public class User {


@Id
private Long id;

@Column(name="username")
private String name;

private String password;
private int role;


public String getName() {
return name;


}


public void setName(String user) {
this.name = user;
}


public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public int getRole() {
return role;
}

public void setRole(int role){
this.role = role;
}
}



Thank you in advance,

Maki Jav
[ July 22, 2008: Message edited by: Maki Jav ]

Help gets you when you need it!
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 435
I made this changes to Requisition.java and now I can get user name

package com.arpatech.hrms.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;



@Table(name="requisitions")
@Entity
public class Requisition {

@Id
private int id;

private String post;

@Column(name = "request_by")
private int requestBy;

private String platform;

@Column(name = "experience_required")
private int experienceRequired;

private String comments;

@Column(name = "date_requested")
private Date dateRequested;


@OneToOne
@JoinColumn( name="request_by", insertable=false, updatable=false)
private User user;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getPost() {
return post;
}

public void setPost(String post) {
this.post = post;
}

public int getRequestBy() {
return requestBy;
}

public void setRequestBy(int requestBy) {
this.requestBy = requestBy;
}

public String getPlatform() {
return platform;
}

public void setPlatform(String platform) {
this.platform = platform;
}

public int getExperienceRequired() {
return experienceRequired;
}

public void setExperienceRequired(int experienceRequired) {
this.experienceRequired = experienceRequired;
}

public String getComments() {
return comments;
}

public void setComments(String comments) {
this.comments = comments;
}

public Date getDateRequested() {
return dateRequested;
}

public void setDateRequested(Date dateRequested) {
this.dateRequested = dateRequested;
}


public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}



}
[ July 23, 2008: Message edited by: Maki Jav ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get user name
 
Similar Threads
Duplicated rows on the database
stored procedure exception (primary key null)
how to update OneToMany
Column 'NEXT_HI' is either not in any table in the FROM list or appears
how can i display my data on jsp page