forked from AOL-archive/micro-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataService.java
More file actions
49 lines (34 loc) · 1.05 KB
/
DataService.java
File metadata and controls
49 lines (34 loc) · 1.05 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
package app1.simple;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.codahale.metrics.annotation.Timed;
import com.google.common.collect.ImmutableList;
@Component
public class DataService {
private final SessionFactory sf;
@Autowired
public DataService(SessionFactory factory) {
sf = factory;
}
@Timed
public void createEntity(String name, String value) {
final Session session = sf.openSession();
session.save(new Entity(name, value));
session.flush();
}
@Timed
public ImmutableList<Entity> findAll(String name){
return ImmutableList.copyOf(searchByName(name));
}
private List<Entity> searchByName(String name) {
final Session session = sf.openSession();
Criteria criteria = session.createCriteria(Entity.class)
.add(Example.create(new Entity(name)));
return criteria.list();
}
}