List of usage examples for org.hibernate.criterion Restrictions and
public static Conjunction and(Criterion... predicates)
From source file:com.quakearts.webapp.hibernate.HibernateBean.java
License:Open Source License
private Criterion handleJunction(Map<String, Serializable> junctionParameters, String type, QueryContext context) {//from w ww. j a va 2 s . c om ArrayList<Criterion> criteria = null; criteria = new ArrayList<>(); for (Entry<String, Serializable> entry : junctionParameters.entrySet()) { criteria.add(handleParameter(entry.getKey(), entry.getValue(), context)); } Criterion[] criteriaArray = criteria.toArray(new Criterion[criteria.size()]); if (type == ParameterMapBuilder.CONJUNCTION) return (Restrictions.and(criteriaArray)); else if (type == ParameterMapBuilder.DISJUNCTION) return (Restrictions.or(criteriaArray)); else throw new HibernateException("Invalid conjuction type:" + type); }
From source file:com.tapestry.stackoverflowclone.dao.QuestionDaoImpl.java
/** * Searches all Questions, by checking if caption or text contains passed text. * Then returns the result.// ww w .j a v a 2 s . c o m * @param query * @return */ @Override public List<Question> getQuestionByQuery(String query) { Criteria criteria = session.createCriteria(Question.class); Criterion rest1 = Restrictions.and(Restrictions.like("questionText", "%" + query + "%")); Criterion rest2 = Restrictions.and(Restrictions.like("questionCaption", "%" + query + "%")); criteria.add(Restrictions.or(rest1, rest2)); return criteria.list(); //return session.createCriteria(Question.class).add(Restrictions.like("questionText", "%" + caption + "%")).list(); }
From source file:hbapp1.HbApp1.java
public static void main5(String[] args) { Session session = HbUtil.getSessionFactory().openSession(); Transaction trans = session.beginTransaction(); List<Dept> depts = session.createCriteria(Dept.class).add(Restrictions.eq("deptNo", (short) 10)) .add(Restrictions.and(Restrictions.eq("deptName", "aBC"))).list(); if (!depts.isEmpty()) { for (Dept d : depts) { System.out.println(d.getDeptNo() + " " + d.getDeptName()); }//from w w w .ja v a 2 s . co m } else { System.out.println("Not found"); } }
From source file:models.db.acentera.impl.DeviceImpl.java
License:Open Source License
public Device getDevice(int guidid) { Session session = (Session) HibernateSessionFactory.getSession(); Criteria criteria = session.createCriteria(Device.class); Device device = (Device) criteria.add(Restrictions.and(Restrictions.eq("id", guidid))).uniqueResult(); return device; }
From source file:models.db.acentera.impl.ProjectImpl.java
License:Open Source License
public static List<ProjectSshKey> getAvailableSshKeys(Long projectId) { Session s = (Session) HibernateSessionFactory.getSession(); Criteria criteria = s.createCriteria(ProjectSshKey.class); List<ProjectSshKey> lstSshKeys = (List<ProjectSshKey>) criteria .add(Restrictions.and(Restrictions.eq("projects.id", projectId))).list(); return lstSshKeys; }
From source file:models.db.acentera.impl.ProjectImpl.java
License:Open Source License
public static List<UserProjects> getProjectForUser(User u) throws Exception { Session s = (Session) HibernateSessionFactory.getSession(); Criteria criteria = s.createCriteria(UserProjects.class); return (List<UserProjects>) criteria.add(Restrictions.and(Restrictions.eq("user", u))).list(); }
From source file:models.db.acentera.impl.ProjectImpl.java
License:Open Source License
public static JSONObject getProjectUserDetails(Long projectId, Long userId, User u) throws Exception { Logger.debug("getProjectUserDetails B1 "); Session s = (Session) HibernateSessionFactory.getSession(); Criteria criteria = s.createCriteria(UserProjects.class); JSONArray jsoProjectUserArray = new JSONArray(); JSONArray jsoUserArray = new JSONArray(); JSONArray jsoProjectIdArray = new JSONArray(); jsoProjectIdArray.add(projectId);//w w w . j av a2s . com Set<User> proessedUsers = new HashSet<User>(); //Get informations about the current User if its in this project... //User theUser = UserImpl.getUserById(userId); //Verry Bad but we couldn't get the user / project mapping to work properly in restrictions... Logger.debug("getProjectUserDetails B2 "); List<UserProjects> lstUp = (List<UserProjects>) criteria .add(Restrictions.and(Restrictions.eq("project.id", projectId))).list(); Logger.debug("getProjectUserDetails B2a " + lstUp); UserProjects up = null; Iterator<UserProjects> itrUp = lstUp.iterator(); while (itrUp.hasNext() && up == null) { UserProjects tmpUp = itrUp.next(); if (tmpUp.getUser().getId().longValue() == userId) { up = tmpUp; } } Logger.debug("getProjectUserDetails Bb " + up); if (up == null) { Logger.debug("Returning null 2..."); return null; } ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false); mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); ObjectWriter ow = mapper.writer(); User projectUser = up.getUser(); jsoProjectUserArray.add(projectUser.getId()); JSONObject jsoUser = JSONObject.fromObject(mapper.writeValueAsString(projectUser)); jsoUser.put("project", jsoProjectIdArray); jsoUser.put("project_id", projectId); //get current user Tag only.. //Other tags will be gathered if end-user click on them.. List<ProjectUserTags> tags = ProjectImpl.getUserProjectTags(up); JSONArray jsoTagsArr = new JSONArray(); for (int i = 0; i < tags.size(); i++) { jsoTagsArr.add(JSONObject.fromObject(mapper.writeValueAsString(tags.get(i)))); } //jsoUser.put("tags", mapper.writeValueAsString(userProject.getTags())); jsoUser.put("tags", jsoTagsArr); //Get the current user roles for this project... JSONArray jsRolesArray = new JSONArray(); Set<ProjectTags> userRoles = ProjectImpl.getUserProjectRoles(up); Iterator<ProjectTags> itrRoles = userRoles.iterator(); while (itrRoles.hasNext()) { ProjectTags userProjectRole = itrRoles.next(); JSONObject role = JSONObject.fromObject(ow.writeValueAsString(userProjectRole)); jsRolesArray.add(role); } jsoUser.put("roles", jsRolesArray); jsoUserArray.add(jsoUser); proessedUsers.add(projectUser); JSONObject jso = new JSONObject(); jso.put("users", jsoUserArray); //jsoProject.put("users", jsoProjectUserArray); return jso; }
From source file:models.db.acentera.impl.ProjectImpl.java
License:Open Source License
public static List<UserProjects> getUsersForProject(Project p) { Session s = (Session) HibernateSessionFactory.getSession(); Criteria criteria = s.createCriteria(UserProjects.class); List<UserProjects> lst = (List<UserProjects>) criteria.add(Restrictions.and(Restrictions.eq("project", p))) .list();/*from ww w. j av a 2 s . com*/ return lst; }
From source file:models.db.acentera.impl.ProjectImpl.java
License:Open Source License
public static List<Project> getAvailableProjects(User u) { Session s = (Session) HibernateSessionFactory.getSession(); Criteria criteria = s.createCriteria(UserProjects.class); List<UserProjects> lst = (List<UserProjects>) criteria.add(Restrictions.and(Restrictions.eq("user", u))) .list();/*from ww w . j ava2s.co m*/ //Get only active Projects List<Project> projects = extract(lst, on(UserProjects.class).getProject()); Logger.debug("GOT PROJECTS? : " + projects); return projects; }
From source file:models.db.acentera.impl.ProjectImpl.java
License:Open Source License
public static Set<ProjectTags> getProjectTags(UserProjects uproject) { Set<ProjectTags> projectTags = new HashSet<ProjectTags>(); Session s = (Session) HibernateSessionFactory.getSession(); Criteria criteria = s.createCriteria(ProjectTags.class); List<ProjectTags> lstTags = (List<ProjectTags>) criteria .add(Restrictions.and(Restrictions.eq("project", uproject.getProject()))).list(); return new HashSet(lstTags); }