-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStoreModel.java
More file actions
68 lines (63 loc) · 2.57 KB
/
StoreModel.java
File metadata and controls
68 lines (63 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.doing.more.java.example;
import com.doing.more.java.example.User;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.lang.Exception;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* Created by lee on 8/27/15.
*/
public class StoreModel {
private HibernateConfig theHibernateConfiguration;
public StoreModel(){
this.theHibernateConfiguration = new HibernateConfig();
}
public void addUser(User aUser){
Session theSession = this.theHibernateConfiguration.getCurrentSession();
Transaction transaction = theSession.beginTransaction();
theSession.save(aUser);
transaction.commit();
}
public void updateUser(User aUser){
Session theSession = this.theHibernateConfiguration.getCurrentSession();
Transaction transaction = theSession.beginTransaction();
theSession.merge(aUser);
transaction.commit();
}
public ArrayList getAllUsers(){
return this.getAll("");
}
public ArrayList getAllCustomers(){
return this.getAll(" where u.manager_level == 0");
}
public ArrayList getAllManagers(){
return this.getAll(" where u.manager_level > 0");
}
public ArrayList getAllManagersByLevel(int aLevel){
return this.getAll(" where u.manager_level == "+aLevel);
}
private ArrayList getAll(String whereClause){
Session theSession = this.theHibernateConfiguration.getCurrentSession();
Transaction transaction = theSession.beginTransaction();
Query allUsersQuery = theSession.createQuery("select u from User as u order by u.id" + whereClause);
List userList = allUsersQuery.list();
return new ArrayList(userList);
}
public User getUser(String aName, String aPassword){
Session theSession = this.theHibernateConfiguration.getCurrentSession();
Transaction transaction = theSession.beginTransaction();
Query singleUserQuery = theSession.createQuery("select u from User as u where u.uname="+aName+ "and u.pword = "+aPassword);
User theUser = (User)singleUserQuery.uniqueResult();
return theUser;
}
public User getUserBySessionID(String aSessionID){
Session theSession = this.theHibernateConfiguration.getCurrentSession();
Transaction transaction = theSession.beginTransaction();
Query singleUserQuery = theSession.createQuery("select u from User as u where u.session="+aSessionID);
User theUser = (User)singleUserQuery.uniqueResult();
return theUser;
}
}