Sonic and Sega Retro Message Board: Can't Get Hibernate (Java) Working - Sonic and Sega Retro Message Board

Jump to content

Hey there, Guest!  (Log In · Register) Help
Loading News Feed...
 
Page 1 of 1

Can't Get Hibernate (Java) Working

#1 User is offline saxman 

Posted 21 June 2012 - 11:24 AM

  • S2HD Staff - Tools & Assistant Programmer
  • Posts: 2582
  • Joined: 08-April 04
  • Gender:Male
  • Location:United States of America
  • Project:http://www.youtube.com/watch?v=oSkQoKRovEk
  • Wiki edits:136
I must be doing this all wrong, but I don't know quite how to make it do exactly what I want. I have three files:


RunMe.java - something to test my code with
package hib;

public class RunMe {

	public static void main(String[] args) {
		DbCommunicator db = new DbCommunicator();
		
		//db.register("John", "Smith", "me", "qwerty", "[email protected]");
		System.out.println(db.checkCredentials("user", "pass"));
		System.out.println(db.getFirst());
		System.out.println(db.getLast());
	}

}


UserDatabase.java - represents my database
package hib;

public class UserDatabase {
	
	private int id;
	private String username;
	private String password;
	private String first;
	private String last;
	private String email;
	private long attributes;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getFirst() {
		return first;
	}
	public void setFirst(String first) {
		this.first = first;
	}
	public String getLast() {
		return last;
	}
	public void setLast(String last) {
		this.last = last;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public long getAttributes() {
		return attributes;
	}
	public void setAttributes(long attributes) {
		this.attributes = attributes;
	}
	
}


DbCommunicator.java - used to do transactions
package hib;

import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class DbCommunicator {
	
	public int register(String first, String last, String username, String password, String email)
	{
		Session session = null;
		try {
			SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
			session = sessionFactory.openSession();
			
			// Find the next available ID number
			int I = 0;
			while(true)
			{
				List<?> list = session.createQuery("select db.id from UserDatabase db where db.id='" + I + "'").list();
				Iterator<?> iter = list.iterator();
				
				if(!iter.hasNext())
				{
					//int id = (Integer)iter.next();
					System.out.println(I);
					break;
				}
				
				I++;
			}
			
			// Check to see if the username is available
			List<?> list = session.createQuery("select db.username from UserDatabase db where db.username='" + username + "'").list();
			Iterator<?> iter = list.iterator();
			
			if(!iter.hasNext())
			{
				session.flush();
				session.close();
				return 2;		// Username already exists
			}
			
			Transaction tx = session.beginTransaction();
			UserDatabase db = new UserDatabase();
			
			db.setId(I);
			db.setFirst(first);
			db.setLast(last);
			db.setUsername(username);
			db.setPassword(password);
			db.setEmail(email);
			db.setAttributes(0);
			session.save(db);
			
			tx.commit();
		}
		catch(Exception e) {
			e.printStackTrace();
			
			session.flush();
			session.close();
			return 1;		// Exception thrown
		}
		
		session.flush();
		session.close();
		return 0;		// No errors!
	}
	
	public int checkCredentials(String username, String password)
	{
		Session session = null;
		
		try {
			SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
			session = sessionFactory.openSession();
			
			List<?> list = session.createQuery("from UserDatabase").list();
			Iterator<?> iter = list.iterator();
			UserDatabase db = null;
			
			while(iter.hasNext())
			{
				db = (UserDatabase)iter.next();
				if(username.equals(db.getUsername()))
				{
					if(password.equals(db.getPassword()))
					{
						session.flush();
						session.close();
						return 0;
					}
					else
					{
						// Password mismatch!
						session.flush();
						session.close();
						return 1;
					}
				}
			}
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		
		// Could not connect to database, or username is wrong
		session.flush();
		session.close();
		return 2;
	}
	
	public int getId() {
		UserDatabase db = new UserDatabase();
		return db.getId();
	}
	public String getUsername() {
		UserDatabase db = new UserDatabase();
		return db.getUsername();
	}
	public String getPassword() {
		UserDatabase db = new UserDatabase();
		return db.getPassword();
	}
	public String getFirst() {
		UserDatabase db = new UserDatabase();
		return db.getFirst();
	}
	public String getLast() {
		UserDatabase db = new UserDatabase();
		return db.getLast();
	}
	public String getEmail() {
		UserDatabase db = new UserDatabase();
		return db.getEmail();
	}
	public long getAttributes() {
		UserDatabase db = new UserDatabase();
		return db.getAttributes();
	}

}



output
0
null
null




The checkCredentials() code works just fine. But I want to be able to retrieve the data from the database afterwards. I thought the UserDatabase was populated with values. Evidently, it's not working the way I expected it to. Can anyone help me understand where I'm going wrong and what I can do with my code to make it work?

#2 User is offline Glitch 

Posted 21 June 2012 - 01:21 PM

  • Posts: 130
  • Joined: 22-September 08
  • Gender:Male
  • Project:Sonic 2 LD
  • Wiki edits:22
Your UserDatabase objects are not bound to a hibernate session so there's nothing to retrieve. You need to use hibernate's query mechanism to actually retrieve values from the database. Typically, this is done using the Data Access Object pattern. I'll try and cook up a quick example for you that I can post here. Give me an hour or two :)

#3 User is offline Glitch 

Posted 21 June 2012 - 02:37 PM

  • Posts: 130
  • Joined: 22-September 08
  • Gender:Male
  • Project:Sonic 2 LD
  • Wiki edits:22
Ok, so this is pretty much a bare minimum Hibernate example. I hope it makes sense. If not just give me a shout.

http://www.mediafire...b5p5aqi36tr1qpe

Page 1 of 1
    Locked
    Locked Forum

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users