Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

How to get user name

 
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Maki Jav
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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'm not sure if I approve of this interruption. But this tiny ad checks out:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic