Я хотел бы послушать совета, хорошо ли я делаю преобразование Entity в DTO. В моем случае преобразование на уровне обслуживания затруднено. "Lorem ipsum dolor sit amet, conctetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud упражнение ullamco labouris nisi ut aliquip ex ea Commodo Conquat. Duis a volute irhenure dolor conquat. velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est Laborum. "
Класс объекта:
@Entity
@Table(name="authorities")
public class Authorities {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private Integer id;
@Column(name="username")
private String username;
@Column(name="authority")
private String authority;
@ManyToOne
@JoinColumn(name="users_id")
private Users users;
@JsonFormat(timezone = "Europe/Warsaw")
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name="added_day",nullable = false, updatable = false)
private Date addedDay;
@JsonFormat(timezone = "Europe/Warsaw")
@UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "last_modification")
private Date lastModification;
@Column(name = "last_modifier")
private String lastModifier;
public Authorities() {
}
public Authorities(Integer id, String username, String authority, Users users, Date addedDay, Date lastModification,
String lastModifier) {
this.id = id;
this.username = username;
this.authority = authority;
this.users = users;
this.addedDay = addedDay;
this.lastModification = lastModification;
this.lastModifier = lastModifier;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
public Date getAddedDay() {
return addedDay;
}
public void setAddedDay(Date addedDay) {
this.addedDay = addedDay;
}
public Date getLastModification() {
return lastModification;
}
public void setLastModification(Date lastModification) {
this.lastModification = lastModification;
}
public String getLastModifier() {
return lastModifier;
}
public void setLastModifier(String lastModifier) {
this.lastModifier = lastModifier;
}
@Override
public String toString() {
return "Authorities [id=" + id + ", username=" + username + ", authority=" + authority + ", users=" + users
+ ", addedDay=" + addedDay + ", lastModification=" + lastModification + ", lastModifier=" + lastModifier
+ "]";
}
}
Класс DTO:
public class AuthoritiesDTO {
private Integer id;
private String username;
private String authority;
private Users users;
private String addedDay;
private String lastModification;
private String lastModifier;
public AuthoritiesDTO()
{
}
public AuthoritiesDTO(Integer id, String username, String authority, Users users, String addedDay,
String lastModification, String lastModifier) {
this.id = id;
this.username = username;
this.authority = authority;
this.users = users;
this.addedDay = addedDay;
this.lastModification = lastModification;
this.lastModifier = lastModifier;
}
public void setUsers(Users users) {
this.users = users;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
public String getAddedDay() {
return addedDay;
}
public void setAddedDay(String addedDay) {
this.addedDay = addedDay;
}
public String getLastModification() {
return lastModification;
}
public void setLastModification(String lastModification) {
this.lastModification = lastModification;
}
public Users getUsers() {
return users;
}
public String getLastModifier() {
return lastModifier;
}
public void setLastModifier(String lastModifier) {
this.lastModifier = lastModifier;
}
@Override
public String toString() {
return "AuthoritiesDTO [id=" + id + ", username=" + username + ", authority=" + authority + ", users=" + users
+ ", addedDay=" + addedDay + ", lastModification=" + lastModification + ", lastModifier=" + lastModifier
+ "]";
}
}
Класс DAO:
@Repository
@ComponentScan(basePackages = {"cms.app.entity"})
public class AuthoritiesDAOImpl extends CommonClass implements AuthoritiesDAO{
@Autowired
private EntityManager entityManager;
@Override
public List<Authorities> getAuthorities() {
Session currentSession = entityManager.unwrap(Session.class);
String username=getPrincipal();
Query<Authorities> theQuery =
currentSession.createQuery("from Authorities",
Authorities.class);
List<Authorities> authorities = theQuery.getResultList();
//logger.info("Query getAuthorities() in table Authorities in day: "+dateTimeFormatter.format(LocalDateTime.now())+" by: "+username);
return authorities;
}
@Override
public void saveAuthorities(Authorities theAuthorities) {
Session currentSession = entityManager.unwrap(Session.class);
String username=getPrincipal();
theAuthorities.setLastModifier(username);
//logger.info("Record saved in table Authorities in day: "+dateTimeFormatter.format(LocalDateTime.now())+", data: "+theAuthorities.toString()+" by: "+username);
currentSession.saveOrUpdate(theAuthorities);
}
@Override
public Authorities getAuthorities(int theId) {
Session currentSession = entityManager.unwrap(Session.class);
String username=getPrincipal();
Authorities theAuthorities = currentSession.get(Authorities.class, theId);
logger.info("Query getAuthorities(int theId) in table Authorities in day: "+dateTimeFormatter.format(LocalDateTime.now())+"with ID: "+theId+" by: "+username);
return theAuthorities;
}
@Override
public void deleteAuthorities(int theId) {
Session currentSession = entityManager.unwrap(Session.class);
String username=getPrincipal();
Query theQuery =
currentSession.createQuery("delete from Authorities where id=:authoritiesId");
theQuery.setParameter("authoritiesId", theId);
logger.info("Record deleted in table Authorities in day: "+dateTimeFormatter.format(LocalDateTime.now())+", with ID: "+theId+" by: "+username);
theQuery.executeUpdate();
}
@Override
public List<String> getRoleDropDownList()
{
Session currentSession = entityManager.unwrap(Session.class);
Query<String> theQuery = currentSession.createQuery("SELECT roleName FROM Role");
List<String> role = theQuery.getResultList();
return role;
}
}
Класс обслуживания:
@Service
@ComponentScan(basePackages = {"cms.app.dao"})
public class AuthoritiesServiceImpl implements AuthoritiesService {
@Autowired
public AuthoritiesDAOImpl authoritiesDAOImpl;
@Override
@Transactional
public List<Authorities> getAuthorities() {
return authoritiesDAOImpl.getAuthorities();
}
@Override
@Transactional
public void saveAuthorities(Authorities theAuthorities) {
authoritiesDAOImpl.saveAuthorities(theAuthorities);
}
@Override
@Transactional
public Authorities getAuthorities(int theId) {
return authoritiesDAOImpl.getAuthorities(theId);
}
@Override
@Transactional
public void deleteAuthorities(int theId) {
authoritiesDAOImpl.deleteAuthorities(theId);
}
}
Класс контроллера:
@Controller
@RequestMapping("/authorities")
public class AuthoritiesController {
@Autowired
private AuthoritiesService authoritiesService;
@Autowired
private ModelMapper modelMapper;
private AuthoritiesDTO convertToDTO(Authorities authorities)
{
AuthoritiesDTO authoritiesDTO = modelMapper.map(authorities, AuthoritiesDTO.class);
return authoritiesDTO;
}
private Authorities convertToEntity(AuthoritiesDTO authoritiesDTO) throws ParseException
{
Authorities authorities = modelMapper.map(authoritiesDTO, Authorities.class);
if(authoritiesDTO.getId() != null)
{
Authorities oldAuthorities = authoritiesService.getAuthorities(authoritiesDTO.getId());
}
return authorities;
}
@GetMapping("/list")
public String listAuthorities(Model theModel) {
List<Authorities> theAuthorities = authoritiesService.getAuthorities();
theAuthorities.stream().map(this::convertToDTO).collect(Collectors.toList());
theModel.addAttribute("authorities", theAuthorities);
return "/list/list-authorities";
}
@GetMapping("/showFormForAdd")
public String showFormForAdd(Model theModel) {
Authorities theAuthorities = new Authorities();
theModel.addAttribute("authorities", theAuthorities);
return "/form/authorities-form";
}
@PostMapping("/saveAuthorities")
public String saveAuthorities(@ModelAttribute("authorities") Authorities theAuthorities)
{
authoritiesService.saveAuthorities(theAuthorities);
return "redirect:/authorities/list";
}
@GetMapping("/showFormForUpdate")
public String showFormForUpdate(@RequestParam("authoritiesId") int theId,
Model theModel) {
AuthoritiesDTO theAuthorities = convertToDTO(authoritiesService.getAuthorities(theId));
theModel.addAttribute("authorities", theAuthorities);
return "/form/authorities-form";
}
@GetMapping("/delete")
public String deleteAuthorities(@RequestParam("authoritiesId") int theId) {
authoritiesService.deleteAuthorities(theId);
return "redirect:/authorities/list";
}
}