Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updating an entity does not work using the repository pattern #11733

Closed
ealcantara22 opened this issue Aug 31, 2020 · 3 comments
Closed

updating an entity does not work using the repository pattern #11733

ealcantara22 opened this issue Aug 31, 2020 · 3 comments
Labels
kind/question Further information is requested

Comments

@ealcantara22
Copy link
Contributor

Describe the bug
I'm writing a REST API in quarkus and today I've noticed that all the update methods stop working. As mentioned in the issue title I'm using the repository pattern (personal choise).

Basically, performing a PUT or PATCH request does not save the changes into the database no matter if the action is marked as Transactional.

To Reproduce
Steps to reproduce the behavior:

@Entity
public class Department {

    @Id
    @GeneratedValue
    public Long id;

    @NotBlank(message = "Department name is required")
    @Column(nullable = false)
    public String name;

}
@ApplicationScoped
public class DepartmentRepository implements PanacheRepository<Department> {
}
// DepartmentService Class
public Department update(Department department, DepartmentDto data) {
   departmentMapper.map(data, department);  //MapStruct
   validateDepartment(department); //ConstraintValidator
   departmentRepository.persist(department);
   return department;
}
@Path("/departments")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class DepartmentResource {
   @Inject
   DepartmentMapper departmentMapper;

   @Inject
   DepartmentService departmentService;

   @PUT
   @Path("/{id}")
   @Transactional
   public DepartmentDto update(@PathParam("id") Long id, DepartmentDto data) {
       Department department = departmentService.getById(id);

       if (department == null)
            throw new NotFoundException("Department not found");

       return departmentMapper.toDto(departmentService.update(department, data));
    }
}

Configuration

# Add your application.properties here, if applicable.
quarkus.datasource.db-kind = postgresql
quarkus.datasource.username = postgres
quarkus.datasource.password = *********
quarkus.datasource.jdbc.url = jdbc:postgresql://localhost:5432/quarkus

quarkus.hibernate-orm.database.generation = update

Environment (please complete the following information):

  • Output of uname -a or ver: Linux OP790 5.4.0-42-generic Add proper logging #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
  • Output of java -version: openjdk 11.0.8 2020-07-14

Additional context
I adjusted the logic above to use the active record pattern and it worked without problems.

Thanks for the great work!! 👍🏼

@ealcantara22 ealcantara22 added the kind/bug Something isn't working label Aug 31, 2020
@loicmathieu
Copy link
Contributor

@ealcantara22 if your entity didn't extends PanacheEntity or PanacheEntityBase it must be a valid JPA entity with it's getters and setters. If you don't want to write those getters and setters you can have your entity extends PanacheEntity or PanacheEntitybase even if you are using the repository pattern.
More info in the guide: https://quarkus.io/guides/hibernate-orm-panache#solution-2-using-the-repository-pattern

@ealcantara22
Copy link
Contributor Author

I see, yeah both suggestions worked just fine. Thanks @loicmathieu

@loicmathieu loicmathieu added kind/question Further information is requested and removed kind/bug Something isn't working labels Aug 31, 2020
@loicmathieu
Copy link
Contributor

OK, so I close the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants