I have created a simple program that will ask the user for their username and everything was working correctly until the point of the authentication. I have manually inserted data into the database for testing purposes and now I want to authenticate the login of a user by checking if they have entered the corresponding username as the same one on the database. However, I am being returned a null value when I ask for the getters of the administrator object.
Can someone please help me on why null is being returned whilst data is in the database? Thank you.
For reference purposes:
public void actionPerformed(ActionEvent e) {
//objects for connection
//Testing
Administrator cevo = new Administrator();
//==========================================//
Session session = HibernateUtility.getSessionFactory().openSession();
session.beginTransaction();
if (username.getText().equals(cevo.getGivenName())) { //Returned Value is Null, but data is present in DB.
System.out.println("MATCH");
}
else{
System.out.println(cevo.getGivenName());
System.out.println(username.getText());
System.out.println("NO MATCH");
}
session.clear();
session.close();
}
});
}
This is my Administrator class, for reference purposes:
@Entity()
@Table(name = "ADMINISTRATOR", schema = "registrationsystem")
public class Administrator implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@JoinColumn(name ="ID", nullable = false)
protected int ID;
@Column(name = "NAME")
protected String name;
@JoinColumn(name = "FAMILYNAME")
//@OneToOne(mappedBy = "familyname", fetch = FetchType.LAZY)
protected String familyName;
@JoinColumn(name = "AGE")
//@OneToOne(mappedBy = "age", fetch = FetchType.LAZY)
protected int age;
@JoinColumn(name = "OCCUPATION")
// @OneToOne(mappedBy = "occupation", fetch = FetchType.LAZY)
protected Occupation occupation;
public Administrator() {
}
public String getGivenName() {
return name;
}
public void setGivenName(String givenName) {
this.name = givenName;
}
public String getFamilyName() {
return familyName;
}
public void setFamilyName(String familyName) {
this.familyName = familyName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Occupation getOccupation() {
return occupation;
}
public void setOccupation(Occupation occupation) {
this.occupation = occupation;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
Your code seems to be a bit confusing, since you dont actually query for anything in the database.
Administrator cevo = new Administrator();
if (username.getText().equals(cevo.getGivenName())) {
System.out.println("MATCH");
}
It is obvious that cevo.getGivenName()
is returning null, since it does not have been initialised yet. Just because you call a method from an arbitary object inside a transaction does not mean that it will automatically fetch your corresponding database object. Hibernate is feature-rich, but in the end it is not magic.
You should try to query for an Administrator. Here are some hints:
Query query = session.createQuery("select Administrator where id = :admin_id");
query.setParameter("admin_id", 1);
Administrator result = (Administrator) query.getSingleResult();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.