View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.samples.sprhib.dao;
20  
21  import org.apache.shiro.samples.sprhib.model.User;
22  import org.springframework.stereotype.Repository;
23  import org.springframework.util.Assert;
24  
25  import java.util.List;
26  
27  @Repository("userDAO")
28  @SuppressWarnings("unchecked")
29  public class HibernateUserDAO extends HibernateDao implements UserDAO {
30  
31      public User getUser(Long userId) {
32          return (User) getSession().get(User.class, userId);
33      }
34  
35      public User findUser(String username) {
36          Assert.hasText(username);
37          String query = "from User u where u.username = :username";
38          return (User) getSession().createQuery(query).setString("username", username).uniqueResult();
39      }
40  
41      public void createUser(User user) {
42          getSession().save( user );
43      }
44  
45      public List<User> getAllUsers() {
46          return getSession().createQuery("from User order by username").list();
47      }
48  
49      public void deleteUser(Long userId) {
50          User user = getUser(userId);
51          if( user != null ) {
52              getSession().delete(user);
53          }
54      }
55  
56      public void updateUser(User user) {
57          getSession().update(user);
58      }
59  
60  }
61