Skip to content

Commit 07d7af8

Browse files
committed
add Transactional
1 parent b503190 commit 07d7af8

File tree

1 file changed

+89
-91
lines changed

1 file changed

+89
-91
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,89 @@
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 com.lofidewanto.demo.server.service.person;
20-
21-
import com.lofidewanto.demo.server.domain.Address;
22-
import com.lofidewanto.demo.server.domain.AddressImpl;
23-
import com.lofidewanto.demo.server.domain.Person;
24-
import com.lofidewanto.demo.server.domain.PersonImpl;
25-
import com.lofidewanto.demo.server.exception.CreatePersonException;
26-
import com.lofidewanto.demo.server.repository.AddressRepository;
27-
import com.lofidewanto.demo.server.repository.PersonRepository;
28-
import org.slf4j.Logger;
29-
import org.slf4j.LoggerFactory;
30-
import org.springframework.beans.factory.annotation.Autowired;
31-
import org.springframework.data.domain.PageRequest;
32-
import org.springframework.data.domain.Pageable;
33-
import org.springframework.stereotype.Service;
34-
35-
import java.util.ArrayList;
36-
import java.util.Collection;
37-
38-
@Service
39-
public class PersonServiceImpl implements PersonService {
40-
41-
private static final Logger logger = LoggerFactory
42-
.getLogger(PersonServiceImpl.class);
43-
44-
@Autowired
45-
private PersonRepository personRepository;
46-
47-
@Autowired
48-
private AddressRepository addressRepository;
49-
50-
@Override
51-
public Person createAddressFromPerson(Address address, Person person)
52-
throws CreatePersonException {
53-
// Create a Person and add an Address to it
54-
Address addressSaved = addressRepository
55-
.save((AddressImpl) address);
56-
57-
logger.info(
58-
"Following address created: " + addressSaved.getStreet());
59-
60-
person.addAddress(address);
61-
62-
try {
63-
Person personSaved = personRepository.save((PersonImpl) person);
64-
65-
logger.info("Following person created: " + personSaved.getName());
66-
67-
return personSaved;
68-
} catch (Exception e) {
69-
logger.error(
70-
"Error saving the person and address - exception: " + e);
71-
throw new CreatePersonException();
72-
}
73-
}
74-
75-
@Override
76-
public Collection<Person> findAllPersons(Integer start, Integer length) {
77-
Collection<Person> personsAsCollection = new ArrayList<>();
78-
Pageable pageable = new PageRequest(start, length);
79-
Iterable<PersonImpl> persons = personRepository.findAll(pageable);
80-
81-
persons.forEach(person -> {
82-
personsAsCollection.add(person);
83-
});
84-
85-
logger.info("Find all persons with start and length amount: "
86-
+ personsAsCollection.size());
87-
88-
return personsAsCollection;
89-
}
90-
91-
}
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 com.lofidewanto.demo.server.service.person;
20+
21+
import java.util.ArrayList;
22+
import java.util.Collection;
23+
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.data.domain.PageRequest;
28+
import org.springframework.data.domain.Pageable;
29+
import org.springframework.stereotype.Service;
30+
import org.springframework.transaction.annotation.Transactional;
31+
32+
import com.lofidewanto.demo.server.domain.Address;
33+
import com.lofidewanto.demo.server.domain.AddressImpl;
34+
import com.lofidewanto.demo.server.domain.Person;
35+
import com.lofidewanto.demo.server.domain.PersonImpl;
36+
import com.lofidewanto.demo.server.exception.CreatePersonException;
37+
import com.lofidewanto.demo.server.repository.AddressRepository;
38+
import com.lofidewanto.demo.server.repository.PersonRepository;
39+
40+
@Service
41+
public class PersonServiceImpl implements PersonService {
42+
43+
private static final Logger logger = LoggerFactory.getLogger(PersonServiceImpl.class);
44+
45+
@Autowired
46+
private PersonRepository personRepository;
47+
48+
@Autowired
49+
private AddressRepository addressRepository;
50+
51+
@Override
52+
@Transactional
53+
public Person createAddressFromPerson(Address address, Person person) throws CreatePersonException {
54+
// Create a Person and add an Address to it
55+
Address addressSaved = addressRepository.save((AddressImpl) address);
56+
57+
logger.info("Following address created: " + addressSaved.getStreet());
58+
59+
person.addAddress(address);
60+
61+
try {
62+
Person personSaved = personRepository.save((PersonImpl) person);
63+
64+
logger.info("Following person created: " + personSaved.getName());
65+
66+
return personSaved;
67+
} catch (Exception e) {
68+
logger.error("Error saving the person and address - exception: " + e);
69+
throw new CreatePersonException();
70+
}
71+
}
72+
73+
@Override
74+
@Transactional
75+
public Collection<Person> findAllPersons(Integer start, Integer length) {
76+
Collection<Person> personsAsCollection = new ArrayList<>();
77+
Pageable pageable = new PageRequest(start, length);
78+
Iterable<PersonImpl> persons = personRepository.findAll(pageable);
79+
80+
persons.forEach(person -> {
81+
personsAsCollection.add(person);
82+
});
83+
84+
logger.info("Find all persons with start and length amount: " + personsAsCollection.size());
85+
86+
return personsAsCollection;
87+
}
88+
89+
}

0 commit comments

Comments
 (0)