Java Code Examples for org.hibernate.HibernateException.printStackTrace()

The following are Jave code examples for showing how to use printStackTrace() of the org.hibernate.HibernateException class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: FlashBoard   File: Driver2.java   View Source Code Vote up 6 votes
public static void p2w(String username) {
	Session session = HibernateUtil.getSession();
	Transaction tx = null;
	
	User newUser = new User("jeff", "myers", username, "jeff", "jeff");
	newUser.setApproved(true);
	newUser.setAvgScore(80.0);
	try {
		tx = session.beginTransaction();
		session.save(newUser);
		tx.commit();
		System.out.println("User: '" + newUser.getUsername() + "' has been successfully created!");
	} catch (HibernateException he) {
		if (tx != null) {
			tx.rollback();
		}
		System.out.println("User creation failed!");
		he.printStackTrace();
		
	} finally {
		session.close();
	}
}
 
Example 2
Project: userinterface   File: HibernateUtil.java   View Source Code Vote up 6 votes
public static SessionFactory getSessionFactory() {
        if (sessionFactory == null || sessionFactory.isClosed()) {
            try {
                Configuration configuration = new Configuration().configure();
                StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
                serviceRegistryBuilder.applySettings(configuration.getProperties());
                ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
                sessionFactory = configuration.buildSessionFactory(serviceRegistry);
//            sessionFactory = new Configuration().configure().buildSessionFactory();
            } catch (HibernateException ex) {
                ex.printStackTrace();
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
            }
        }
        return sessionFactory;
    }
 
Example 3
Project: FlashBoard   File: Driver2.java   View Source Code Vote up 6 votes
public static void testUserCreation() {
	Session session = HibernateUtil.getSession();
	Transaction tx = null;
	
	User newUser = new User("jef", "jeff", "jeff", "jeff", "jeff");
	//SQLIntegrityConstraintViolationException
	try {
		tx = session.beginTransaction();
		session.save(newUser);
		tx.commit();
		System.out.println("User: '" + newUser.getUsername() + "' has been successfully created!");
	} catch (HibernateException he) {
		if (tx != null) {
			tx.rollback();
		}
		System.out.println("User creation failed!");
		he.printStackTrace();
		
	} finally {
		session.close();
	}
}
 
Example 4
Project: FlashBoard   File: QuizDaoImpl.java   View Source Code Vote up 6 votes
@SuppressWarnings("unchecked")
public List<Quiz> getAllQuizzes(){
	Session session = HibernateUtil.getSession();
	Query query;
	String hql;
	Transaction tx = null;
	List<Quiz> quizzes = null;
	try {
		tx = session.beginTransaction();
		hql = "FROM io.flashboard.beans.quiz.Quiz";
		query = session.createQuery(hql);
		quizzes = query.list();

	} catch (HibernateException e) {
		if (tx != null) {
			tx.rollback();
		}
		e.printStackTrace();
	} finally {
		session.close();
	}
	return quizzes;		
}
 
Example 5
Project: FlashBoard   File: QuizDaoImpl.java   View Source Code Vote up 6 votes
public boolean addCommentByQuizId(int quizId, String username, String content) {
	Session session = HibernateUtil.getSession();
	Transaction tx = null;
	Boolean success = false;
	Quiz quiz = new Quiz();
	Comment comment = new Comment(username, content);
	
	try {
		tx = session.beginTransaction();
		quiz = getQuizById(quizId);
		quiz.addComment(comment);
		session.save(quiz);
		System.out.println("Comment '" + comment.getCommentId() + "' successfully added to Quiz '" + quiz.getTestId() + "'.");
		success = true;
		tx.commit();
	} catch (HibernateException e) {
		if (tx != null) {
			tx.rollback();
		}
		success = false;
		e.printStackTrace();
	} finally {
		session.close();
	}
	return success;
}
 
Example 6
Project: FlashBoard   File: QuizDaoImpl.java   View Source Code Vote up 6 votes
public Integer insertQuiz(Quiz quiz) {
	Session session = HibernateUtil.getSession();
	Transaction tx = null;
	Integer quizId = null;
	
	try {
		// Attemps to add and get quiz Id back
		tx = session.beginTransaction();
		quizId = (Integer) session.save(quiz);
		System.out.println(quizId);
		tx.commit();
	} catch (HibernateException e) {
		if (tx != null) {
			// Roll back IF transaction fails
			tx.rollback();
		}
		e.printStackTrace();
	} finally {
		session.close();
	}
	
	return quizId;
}
 
Example 7
Project: FlashBoard   File: QuizDaoImpl.java   View Source Code Vote up 6 votes
@Override
public Quiz getQuizById(int quizId) {
	Session session = HibernateUtil.getSession();
	Criteria criteria;
	Quiz test = null;
	
	try {
		criteria = session.createCriteria(Quiz.class);
		//Adds like restriction to search for a particular username
		test = (Quiz)criteria.add(Restrictions.like("quizId", quizId)).uniqueResult();
	}
	catch(HibernateException he) {
		he.printStackTrace();
	}finally {
		session.close();
	}
	return test;
}
 
Example 8
Project: FlashBoard   File: QuizDaoImpl.java   View Source Code Vote up 6 votes
@Override
public Quiz getQuizByTitle(String quizTitle) {
	Session session = HibernateUtil.getSession();
	Criteria criteria;
	Quiz test = null;
	
	try {
		criteria = session.createCriteria(Quiz.class);
		//Adds like restriction to search for a particular username
		test = (Quiz)criteria.add(Restrictions.like("quizTitle", quizTitle)).uniqueResult();
	}
	catch(HibernateException he) {
		he.printStackTrace();
	}finally {
		session.close();
	}
	return test;
}
 
Example 9
Project: FlashBoard   File: StudyGuideDaoImpl.java   View Source Code Vote up 6 votes
@Override
public StudyGuide getStudyGuide(int guideId) {
	
	Session session = HibernateUtil.getSession();
	Criteria criteria;
	StudyGuide studyGuide = null;
	try {
		criteria = session.createCriteria(StudyGuide.class);
		studyGuide = (StudyGuide) criteria.add(Restrictions.like("studyGuideId", guideId)).uniqueResult();

	} catch (HibernateException he) {
		he.printStackTrace();
	} finally {
		session.close();
	}
	return studyGuide;
}
 
Example 10
Project: FlashBoard   File: StudyGuideDaoImpl.java   View Source Code Vote up 6 votes
@SuppressWarnings("unchecked")
@Override
public List<StudyGuide> getAllStudyGuides() {
	Session session = HibernateUtil.getSession();
	Query query;
	String hql;
	Transaction tx = null;
	List<StudyGuide> guides = null;
	try {
		tx = session.beginTransaction();
		hql = "FROM io.flashboard.beans.studyguide.StudyGuide";
		query = session.createQuery(hql);
		guides = query.list();

	} catch (HibernateException e) {
		if (tx != null) {
			tx.rollback();
		}
		e.printStackTrace();
	} finally {
		session.close();
	}
	return guides;
}
 
Example 11
Project: FlashBoard   File: MessageDaoImpl.java   View Source Code Vote up 6 votes
@Override
public boolean createMessage(String username, String content) {
	Session session = HibernateUtil.getSession();
	Transaction tx = null;
	Boolean bool = false;
	Message message = new Message(username, content);
	
	try {
		tx = session.beginTransaction();
		session.save(message);
		tx.commit();
		bool = true;
		System.out.println("Message: '" + message.getMessageId() + "' has been successfully created!");
	} catch (HibernateException he) {
		if (tx != null) {
			tx.rollback();
		}
		System.out.println("Message creation failed!");
		he.printStackTrace();
		
	} finally {
		session.close();
	}
	return bool;
}
 
Example 12
Project: FlashBoard   File: UserDaoImpl.java   View Source Code Vote up 6 votes
/**
 * Gets a unique user from the database, if he/she exists
 * Utilizes Criterias
 * 
 * @param username username to be searched for
 * @return the user if he/she exists, otherwise null
 */
@Override
public User getUserByUsername(String username) {
	Session session = HibernateUtil.getSession();
	Criteria criteria;
	User user = null;
	
	try {
		criteria = session.createCriteria(User.class);
		//Adds like restriction to search for a particular username
		user = (User)criteria.add(Restrictions.like("username", username)).uniqueResult();
	}
	catch(HibernateException he) {
		he.printStackTrace();
	}finally {
		session.close();
	}
	return user;
}
 
Example 13
Project: FlashBoard   File: UserDaoImpl.java   View Source Code Vote up 6 votes
@SuppressWarnings("unchecked")
@Override
public List<String> getAllPromotees(){
	Session session = HibernateUtil.getSession();
	List<String> promotees = new ArrayList<>();
	
	Criteria criteria;
	
	try {
		criteria = session.createCriteria(User.class);
		promotees = criteria.add(Restrictions.ge("avgScore", 70.0))
				.add(Restrictions.like("approved", true))
				.setProjection(Projections.property("username")).list();
	}
	catch(HibernateException he) {
		he.printStackTrace();
	}finally {
		session.close();
	}
	
	if(promotees.isEmpty()) {
		promotees = null; 
	}
	
	return promotees;
}
 
Example 14
Project: FlashBoard   File: QuizDaoImpl.java   View Source Code Vote up 5 votes
@Override
public List<Comment> getQuizComments(int quizId) {
	List<Comment> comments = new ArrayList<>();
	Session session = HibernateUtil.getSession();
	Criteria criteria = null;
	Quiz q = null;
	
	Query query = null;
	StudyGuide sg = null;
	String hql = "FROM StudyGuide WHERE studyGuideId = :id";
	
	try {
		criteria = session.createCriteria(Quiz.class);
		q = (Quiz)criteria.add(Restrictions.like("quizId", quizId)).uniqueResult();
		
		comments = q.getComments();
	}
	catch(HibernateException he) {
		he.printStackTrace();
	}
	
	if(comments.isEmpty()) {
		return null;
	}
	
	return comments;
}
 
Example 15
Project: FlashBoard   File: QuizDaoImpl.java   View Source Code Vote up 5 votes
/**
 * Adds comments to a quiz
 * quiz is found by id and comments are inserted
 * 
 * @param comment contents
 * @param quiz id
 * 
 * @return true if comment add, otherwise false
 * 
 */
@Override
public boolean addQuizComment(Comment comment, int quizId) {
	Quiz q = getQuizById(quizId);
	
	if(q == null) {
		return false;
	}
	
	Session session = HibernateUtil.getSession();
	Transaction tx = null;
	q.getComments().add(comment);
	
	try {
		tx = session.beginTransaction();
		
		session.update(q);
		
		tx.commit();
		
		return true;
	}
	catch(HibernateException he) {
		if(tx != null) {
			tx.rollback();
		}
		he.printStackTrace();
	}
	finally {
		session.close();
	}
	
	
	return false;
}
 
Example 16
Project: FlashBoard   File: StudyGuideDaoImpl.java   View Source Code Vote up 5 votes
/**
 * gets title, body, img url, and author and inserts guid into database
 * 
 * @param guide
 *            information
 * @return status of transaction
 */
@Override
public boolean addStudyGuide(String title, String[] body, String imgUrl, String author) {

	boolean success = false;
	Session session = HibernateUtil.getSession();
	Transaction tx = null;

	ArrayList<String> arrListBody = new ArrayList<String>(Arrays.asList(body));
	LocalDateTime createdDate = LocalDateTime.now();
	
	try {
		StudyGuide newStudyGuide = new StudyGuide(title, arrListBody, imgUrl, author, null, null, createdDate);
		tx = session.beginTransaction();
		session.save(newStudyGuide);
		success = true;
		System.out.println(newStudyGuide);
		tx.commit();
	} catch (HibernateException he) {
		if (tx != null) {
			tx.rollback();
		}
		he.printStackTrace();
	} finally {
		session.close();
	}
	return success;
}
 
Example 17
Project: FlashBoard   File: StudyGuideDaoImpl.java   View Source Code Vote up 5 votes
/**
 * Gets the comments associated with a study guide
 * 
 * @param id for study guide
 * 
 * @return list of comments
 * 
 */
@Override
public List<CommentSG> getStudyGuideComments(int guideId) {
	List<CommentSG> comments = new ArrayList<>();
	Session session = HibernateUtil.getSession();
	Query query = null;
	StudyGuide sg = null;
	String hql = "FROM StudyGuide WHERE studyGuideId = :id";
	
	try {
		query = session.createQuery(hql);
		query.setParameter("id", guideId);
		
		sg = (StudyGuide)query.uniqueResult();
		
		comments = sg.getComments();
	}
	catch(HibernateException he) {
		he.printStackTrace();
	}
	
	if(comments.isEmpty()) {
		return null;
	}
	
	return comments;
}
 
Example 18
Project: FlashBoard   File: StudyGuideDaoImpl.java   View Source Code Vote up 5 votes
@Override
public boolean addGuideComment(CommentSG comment, int guideId) {
	StudyGuide sg = getStudyGuide(guideId);
	
	if(sg == null) {
		return false;
	}
	
	Session session = HibernateUtil.getSession();
	Transaction tx = null;
	sg.getComments().add(comment);
	
	try {
		tx = session.beginTransaction();
		
		session.update(sg);
		
		tx.commit();
		
		return true;
	}
	catch(HibernateException he) {
		if(tx != null) {
			tx.rollback();
		}
		he.printStackTrace();
	}
	finally {
		session.close();
	}
	
	
	return false;
}
 
Example 19
Project: FlashBoard   File: UserDaoImpl.java   View Source Code Vote up 5 votes
@Override
public boolean createNewUser(String firstName, String lastName, String username,
	String email, String password) {
	Boolean bool = false;
	Session session = HibernateUtil.getSession();
	Transaction tx = null;
	
	User newUser = new User(firstName, lastName, username, email, password);
	
	try {
		tx = session.beginTransaction();
		session.save(newUser);
		tx.commit();
		bool = true;
		System.out.println("User: '" + newUser.getUsername() + "' has been successfully created!");
	} catch (HibernateException he) {
		if (tx != null) {
			tx.rollback();
		}
		System.out.println("User creation failed!");
		he.printStackTrace();
		
	} finally {
		session.close();
	}
	return bool;
}
 
Example 20
Project: Hibernate-Versioning_HQL_Maven   File: ClientApp_withdraw.java   View Source Code Vote up 4 votes
public static void main(String[] args) {

		Session ses = null;			//creating the session object
		Transaction tx = null;		//creating transaction object
		Scanner sc=new Scanner(System.in);		//creating scanner for user input

		ses = HibernateUtil.getSession();			//get the cgf,factory,session
		
				//creating the domain class object
			System.out.print("\n\t\tEnter the Account Holder Number:");
			int no=sc.nextInt();
			float bal = 0;
			boolean flag=false;
				
				Object ob=ses.get(Emp.class.getName(),no);
				Emp e=(Emp)ob;
				if(ob!=null)
				{
					flag=true;
				System.out.println("\n\t\t"+e);
				System.out.println("\n\t\tTransactions Left: "+e.getVersion());
		try {
			tx = ses.beginTransaction();

				
			if (e.getVersion()>5)
			{
				System.out.println("\n\t\tNo Free Transactions");
			}
			
				System.out.print("\n\t\tEnter the Amount to Withdraw: ");
				int amt1=sc.nextInt();
				bal=e.getBalance()-(amt1);
			
			e.setBalance(bal);
			ses.update(e);
			tx.commit();
			System.out.println("\n\t\tWithdrawn Money: "+amt1);
			System.out.println("\n\t\tBalance: "+e.getBalance());
			if((5-e.getVersion())!=0)
			{			System.out.println("\n\t\tTransactions Left: "+(5-e.getVersion()));
			}else
			System.out.println("\t\tNo Free Transactions Available");
				

			} //try
			catch (HibernateException he) {
			System.out.println("\t\tRECORD NOT FOUND");
			tx.rollback();
			he.printStackTrace();
			flag=false;
		}//catch
		catch (Exception e3) {
			tx.rollback();
			e3.printStackTrace();
			flag=false;
		}//catch
		finally
		{
			try {
				
				ses.close();
				HibernateUtil.closeFactory();
				flag=false;
			}//catch
			catch (Exception e2) {
				e2.printStackTrace();
				flag=false;
			}//catch
		}//finally
		
				System.out.print("\n\t\t ==PROGRAMME BY PRATIK DIMBLE==\n\n\n");
	}//main
}