File tree Expand file tree Collapse file tree
java/ru/javawebinar/topjava
test/java/ru/javawebinar/topjava/service Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1616 <project .build.sourceEncoding>UTF-8</project .build.sourceEncoding>
1717 <project .reporting.outputEncoding>UTF-8</project .reporting.outputEncoding>
1818 <spring .version>5.3.39</spring .version>
19+ <spring-data-jpa .version>2.7.18</spring-data-jpa .version>
1920 <tomcat .version>9.0.111</tomcat .version>
2021
2122 <!-- Hibernate -->
9697 <dependency >
9798 <groupId >org.springframework</groupId >
9899 <artifactId >spring-context</artifactId >
99- <version >${spring.version} </version >
100100 </dependency >
101101 <dependency >
102- <groupId >org.springframework</groupId >
103- <artifactId >spring-orm </artifactId >
104- <version >${spring.version} </version >
102+ <groupId >org.springframework.data </groupId >
103+ <artifactId >spring-data-jpa </artifactId >
104+ <version >${spring-data-jpa .version} </version >
105105 </dependency >
106106
107107 <!-- - ORM -->
148148 <dependency >
149149 <groupId >org.springframework</groupId >
150150 <artifactId >spring-test</artifactId >
151- <version >${spring.version} </version >
152151 <scope >test</scope >
153152 </dependency >
154153 <dependency >
199198 </profiles >
200199
201200 <dependencyManagement >
201+ <dependencies >
202+ <dependency >
203+ <groupId >org.springframework</groupId >
204+ <artifactId >spring-framework-bom</artifactId >
205+ <version >${spring.version} </version >
206+ <type >pom</type >
207+ <scope >import</scope >
208+ </dependency >
209+ </dependencies >
202210 </dependencyManagement >
203211</project >
Original file line number Diff line number Diff line change 55public class Profiles {
66 public static final String
77 JDBC = "jdbc" ,
8- JPA = "jpa" ;
8+ JPA = "jpa" ,
9+ DATAJPA = "datajpa" ;
910
10- public static final String REPOSITORY_IMPLEMENTATION = JPA ;
11+ public static final String REPOSITORY_IMPLEMENTATION = DATAJPA ;
1112
1213 public static final String
1314 POSTGRES_DB = "postgres" ,
Original file line number Diff line number Diff line change 11package ru .javawebinar .topjava .model ;
22
3+ import org .springframework .data .domain .Persistable ;
34import org .springframework .util .Assert ;
45
56import javax .persistence .*;
910@ MappedSuperclass
1011// http://stackoverflow.com/questions/594597/hibernate-annotations-which-is-better-field-or-property-access
1112@ Access (AccessType .FIELD )
12- public abstract class AbstractBaseEntity {
13+ public abstract class AbstractBaseEntity implements Persistable < Integer > {
1314 public static final int START_SEQ = 100000 ;
1415
1516 @ Id
@@ -30,6 +31,7 @@ public void setId(Integer id) {
3031 this .id = id ;
3132 }
3233
34+ @ Override
3335 public Integer getId () {
3436 return id ;
3537 }
@@ -40,6 +42,7 @@ public int id() {
4042 return id ;
4143 }
4244
45+ @ Override
4346 public boolean isNew () {
4447 return getId () == null ;
4548 }
Original file line number Diff line number Diff line change 1+ package ru .javawebinar .topjava .repository .datajpa ;
2+
3+ import org .springframework .data .jpa .repository .JpaRepository ;
4+ import ru .javawebinar .topjava .model .Meal ;
5+
6+ public interface CrudMealRepository extends JpaRepository <Meal , Integer > {
7+ }
Original file line number Diff line number Diff line change 1+ package ru .javawebinar .topjava .repository .datajpa ;
2+
3+ import org .springframework .data .jpa .repository .JpaRepository ;
4+ import org .springframework .data .jpa .repository .Modifying ;
5+ import org .springframework .data .jpa .repository .Query ;
6+ import org .springframework .data .repository .query .Param ;
7+ import org .springframework .transaction .annotation .Transactional ;
8+ import ru .javawebinar .topjava .model .User ;
9+
10+ @ Transactional (readOnly = true )
11+ public interface CrudUserRepository extends JpaRepository <User , Integer > {
12+ @ Transactional
13+ @ Modifying
14+ // @Query(name = User.DELETE)
15+ @ Query ("DELETE FROM User u WHERE u.id=:id" )
16+ int delete (@ Param ("id" ) int id );
17+
18+ User getByEmail (String email );
19+ }
Original file line number Diff line number Diff line change 1+ package ru .javawebinar .topjava .repository .datajpa ;
2+
3+ import org .springframework .stereotype .Repository ;
4+ import ru .javawebinar .topjava .model .Meal ;
5+ import ru .javawebinar .topjava .repository .MealRepository ;
6+
7+ import java .time .LocalDateTime ;
8+ import java .util .List ;
9+
10+ @ Repository
11+ public class DataJpaMealRepository implements MealRepository {
12+
13+ private final CrudMealRepository crudRepository ;
14+
15+ public DataJpaMealRepository (CrudMealRepository crudRepository ) {
16+ this .crudRepository = crudRepository ;
17+ }
18+
19+ @ Override
20+ public Meal save (Meal meal , int userId ) {
21+ return null ;
22+ }
23+
24+ @ Override
25+ public boolean delete (int id , int userId ) {
26+ return false ;
27+ }
28+
29+ @ Override
30+ public Meal get (int id , int userId ) {
31+ return null ;
32+ }
33+
34+ @ Override
35+ public List <Meal > getAll (int userId ) {
36+ return null ;
37+ }
38+
39+ @ Override
40+ public List <Meal > getBetweenHalfOpen (LocalDateTime startDateTime , LocalDateTime endDateTime , int userId ) {
41+ return null ;
42+ }
43+ }
Original file line number Diff line number Diff line change 1+ package ru .javawebinar .topjava .repository .datajpa ;
2+
3+ import org .springframework .data .domain .Sort ;
4+ import org .springframework .stereotype .Repository ;
5+ import ru .javawebinar .topjava .model .User ;
6+ import ru .javawebinar .topjava .repository .UserRepository ;
7+
8+ import java .util .List ;
9+
10+ @ Repository
11+ public class DataJpaUserRepository implements UserRepository {
12+ private static final Sort SORT_NAME_EMAIL = Sort .by (Sort .Direction .ASC , "name" , "email" );
13+
14+ private final CrudUserRepository crudRepository ;
15+
16+ public DataJpaUserRepository (CrudUserRepository crudRepository ) {
17+ this .crudRepository = crudRepository ;
18+ }
19+
20+ @ Override
21+ public User save (User user ) {
22+ return crudRepository .save (user );
23+ }
24+
25+ @ Override
26+ public boolean delete (int id ) {
27+ return crudRepository .delete (id ) != 0 ;
28+ }
29+
30+ @ Override
31+ public User get (int id ) {
32+ return crudRepository .findById (id ).orElse (null );
33+ }
34+
35+ @ Override
36+ public User getByEmail (String email ) {
37+ return crudRepository .getByEmail (email );
38+ }
39+
40+ @ Override
41+ public List <User > getAll () {
42+ return crudRepository .findAll (SORT_NAME_EMAIL );
43+ }
44+ }
Original file line number Diff line number Diff line change 44 xmlns : context =" http://www.springframework.org/schema/context"
55 xmlns : jdbc =" http://www.springframework.org/schema/jdbc"
66 xmlns : tx =" http://www.springframework.org/schema/tx"
7+ xmlns : jpa =" http://www.springframework.org/schema/data/jpa"
78 xsi : schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
89 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
910 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
10- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" >
11+ http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
12+ http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" >
1113
12- <context : component-scan base-package =" ru.javawebinar.**.repository.jpa" />
14+ <!-- <context:component-scan base-package="ru.javawebinar.**.repository.jpa"/>-->
15+
16+ <context : component-scan base-package =" ru.javawebinar.**.repository.datajpa" />
17+ <jpa : repositories base-package =" ru.javawebinar.**.repository.datajpa" />
1318
1419 <jdbc : initialize-database data-source =" dataSource" enabled =" ${database.init}" >
1520 <jdbc : script location =" ${jdbc.initLocation}" />
8590 p : username =" ${database.username}"
8691 p : password =" ${database.password}" />
8792 </beans >
93+
94+ <beans profile =" jdbc" >
95+ </beans >
96+ <beans profile =" jpa" >
97+ </beans >
98+ <beans profile =" datajpa" >
99+ </beans >
88100</beans >
Original file line number Diff line number Diff line change 11package ru .javawebinar .topjava .service ;
22
3- import org .junit .AfterClass ;
4- import org .junit .Assert ;
5- import org .junit .Rule ;
6- import org .junit .Test ;
3+ import org .junit .*;
74import org .junit .rules .Stopwatch ;
85import org .junit .runner .Description ;
96import org .junit .runner .RunWith ;
3633@ RunWith (SpringRunner .class )
3734@ Sql (scripts = "classpath:db/populateDB.sql" , config = @ SqlConfig (encoding = "UTF-8" ))
3835@ ActiveProfiles (resolver = ActiveDbProfileResolver .class )
36+ @ Ignore
3937public class MealServiceTest {
4038 private static final Logger log = getLogger ("result" );
4139
You can’t perform that action at this time.
0 commit comments