hi my problem is when i do mvn clean package to build .jar files, i got some error that looked like this :
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/Cactus/Documents/GitHub/Microservice/b2b/b2b-warehouse/src/main/java/com/bit/microservices/b2b/warehouse/mapper/ICategoryBpsjMapper.java:[27,33] No property named "createdDate" exists in source parameter(s). Did you mean "reasonDeleted"?
[ERROR] /C:/Users/Cactus/Documents/GitHub/Microservice/b2b/b2b-warehouse/src/main/java/com/bit/microservices/b2b/warehouse/mapper/ICategoryBpsjMapper.java:[27,33] No property named "createdDate" exists in source parameter(s). Did you mean "reasonDeleted"?
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 16.438 s
[INFO] Finished at: 2020-11-30T10:55:21+07:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project b2b-warehouse: Compilation failure: Compilation failure:
[ERROR] /C:/Users/Cactus/Documents/GitHub/Microservice/b2b/b2b-warehouse/src/main/java/com/bit/microservices/b2b/warehouse/mapper/ICategoryBpsjMapper.java:[27,33] No property named "createdDate" exists in source parameter(s). Did you mean "reasonDeleted"?
[ERROR] /C:/Users/Cactus/Documents/GitHub/Microservice/b2b/b2b-warehouse/src/main/java/com/bit/microservices/b2b/warehouse/mapper/ICategoryBpsjMapper.java:[27,33] No property named "createdDate" exists in source parameter(s). Did you mean "reasonDeleted"?
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
but when i run my project through Boot Dashboard with right click and start my project it run normally. and my api work fine. i do some try.
first trying my code is looked like below, i have mapper file that looked like this :
#Mapper(componentModel = "spring")
public interface ICategoryBpsjMapper {
ICategoryBpsjMapper INSTANCE = Mappers.getMapper(ICategoryBpsjMapper.class);
#Mappings({
#Mapping(target = "createdDate", dateFormat = "dd-MM-yyyy HH:mm:ss"),
#Mapping(target = "modifiedDate",dateFormat = "dd-MM-yyyy HH:mm:ss")})
CategoryBpsjResponseDto entityToDto(CategoryBPSJ entity);
CategoryBPSJ DTOEntity(CategoryBPSJDto dto);
CategoryBPSJ updateEntityFromDto(CategoryBPSJDto dto, #MappingTarget CategoryBPSJ entity);
List<CategoryBpsjResponseDto> entityToDto(List<CategoryBPSJ> entityList);
List<CategoryBPSJ> DTOEnity(List<CategoryBPSJDto> dtoList);
}
and here is my entity :
#Data
#Entity
#Audited
#EntityListeners(AuditingEntityListener.class)
#EqualsAndHashCode(of = "id")
#ToString(of = {"id"})
#Table(name = "msCategoryBPSJ", uniqueConstraints = {
#UniqueConstraint(name = "uk_categoryBPSJName", columnNames = { "categoryBPSJName" })})
public class CategoryBPSJ extends AuditField implements Serializable {
private static final long serialVersionUID = 8062436315663828938L;
#Schema(description = "Id merupakan primary key dan harus disertakan ketika edit, tipe datanya Long", example = "0", required = true)
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Schema(description = "kode kategori pengiriman", example = "BPSJCTH1", required = true)
#NotBlank(message = "Tidak Boleh Kosong")
private String categoryBPSCode;
#Schema(description = "nama kategori pengiriman", example = "Toko/Gudang Tujuan Tutup", required = true)
#NotBlank(message = "Tidak Boleh Kosong")
private String categoryBPSJName;
}
and here is my extended AuditField class :
#MappedSuperclass
#Data
#Audited
#EntityListeners(AuditingEntityListener.class)
#JsonIgnoreProperties(value = { "createdDate", "modifiedDate", "isDeleted" }, allowGetters = true, allowSetters = false)
public class AuditField implements Serializable {
private static final long serialVersionUID = 6025082509703710749L;
#Schema(description = "Status di hapus", example = "false", required = false)
public Boolean isDeleted = false;
#Schema(description = "Alasan di hapus", example = "apa pun alasannya", required = false)
public String reasonDeleted = "";
#CreatedDate
private Date createdDate;
#LastModifiedDate
private Date modifiedDate;
#CreatedBy
private String createdBy;
#LastModifiedBy
private String modifiedBy;
}
and here is my DTO AuditFieldDto class :
#Data
public class CategoryBpsjResponseDto extends AuditFieldDto implements Serializable {
private static final long serialVersionUID = -422185482845439241L;
#Schema(description = "Id merupakan primary key dan harus disertakan ketika edit, tipe datanya Long", example = "0", required = true)
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
#Schema(description = "kode kategori pengiriman", example = "BPSJCTH1", required = true)
#NotBlank(message = "Tidak Boleh Kosong")
public String categoryBPSCode;
#Schema(description = "nama kategori pengiriman", example = "Toko/Gudang Tujuan Tutup", required = true)
#NotBlank(message = "Tidak Boleh Kosong")
public String categoryBPSJName;
}
and here is my AuditFieldDto class :
#Data
public class AuditFieldDto implements Serializable {
private static final long serialVersionUID = 3758121148184815303L;
#Schema(description = "Status di hapus", example = "false", required = false)
public Boolean isDeleted = false;
#Schema(description = "Alasan di hapus", example = "apa pun alasannya", required = false)
public String reasonDeleted = "";
private String createdDate;
private String modifiedDate;
private String createdBy;
private String modifiedBy;
}
As you can see the AuditField and AuditFieldDto have the same field, but when i do run mvn clean package it give me COMPILATION ERROR like the top of my question mentioned. in the error message it seems to mapstruct cannot find the field properly. Then i try to remove #Mappings and #Mapping annotation line, the build is success. seems fishy.. because when i run with Boot Dashboard is working fine with those annotation.
here is my pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bit.microservices</groupId>
<artifactId>b2b</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>b2b-warehouse</artifactId>
<groupId>com.bit.microservices.b2b</groupId>
<name>b2b-warehouse</name>
<description>B2B Warehouse Service</description>
<version>0.0.1-SNAPSHOT</version>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>http://www.spring.io</url>
</organization>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
<lombok.version>1.18.16</lombok.version>
<org.mapstruct.version>1.4.1.Final</org.mapstruct.version>
</properties>
<dependencies>
<dependency>
<groupId>com.bit.microservices</groupId>
<artifactId>model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-envers</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.bit.b2b</groupId>
<artifactId>security</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-ui -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.4.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.classgraph/classgraph -->
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>4.8.90</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.modelmapper/modelmapper -->
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.8</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>13</source> <!-- depending on your project -->
<target>13</target> <!-- depending on your project -->
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.3.4.RELEASE</version>
</path>
<!-- other annotation processors -->
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
What i missed in my project if i want to use #Mappings and #Mapping when i build my project to .jar files with mvn clean package?
i found the culprit here, but i am not sure why and is this the right things to do, since when i do this code, it worked. okay lets start, you can check my mapstruct version at my question, and here is how i fixed this :
first of all, in my AuditField and AuditFieldDto, as you can see i have 2 field that written in "public" data type, at first i write all of my field in public but then my friend come and refer me to make it private, but i accidentally i don't make all of it private, the two fields named public Boolean isDeleted; and public String reasonDeleted; is public, that's why the error go through them, they work fine and the error go to my another line that written in private.
and i just random miserably changed their data type back into public, as you can see there is 4 field that written in private fields, they are
#CreatedDate
private Date createdDate;
#LastModifiedDate
private Date modifiedDate;
#CreatedBy
private String createdBy;
#LastModifiedBy
private String modifiedBy;
so i changed all of them into public in my AuditFieldDto and AuditField class, the complete of code of my AuditField class is looked like this :
#MappedSuperclass
#Data
#Audited
#EntityListeners(AuditingEntityListener.class)
#JsonIgnoreProperties(value = { "createdDate", "modifiedDate", "isDeleted" }, allowGetters = true, allowSetters = false)
public class AuditField implements Serializable {
private static final long serialVersionUID = 6025082509703710749L;
#Schema(description = "Status di hapus", example = "false", required = false)
public Boolean isDeleted;
#Schema(description = "Alasan di hapus", example = "apa pun alasannya", required = false)
public String reasonDeleted;
#CreatedDate
public Date createdDate;
#LastModifiedDate
public Date modifiedDate;
#CreatedBy
public String createdBy;
#LastModifiedBy
public String modifiedBy;
}
it's apply the same for my AuditFieldDto class, which is looked like this :
#Data
public class AuditFieldDto implements Serializable {
private static final long serialVersionUID = 3758121148184815303L;
#Schema(description = "Status di hapus", example = "false", required = false)
public Boolean isDeleted = false;
#Schema(description = "Alasan di hapus", example = "apa pun alasannya", required = false)
public String reasonDeleted = "";
public String createdDate;
public String modifiedDate;
public String createdBy;
public String modifiedBy;
}
and when i do run mvn clean package the build is a success! and i ran the .jar files, it working! i guess why??? i stuck for days because of this. then i try to look into the generated files, you can find the generated files in your project located at target/generated-sources/annotations and fine the mapper class file. And here is mine, i only take one example of the generated code, since the other seems doesn't have any problem.
here is what inside the GENERATED FILE (we shouldn't edit this file, just for view) :
#Component
public class ICategoryBpsjMapperImpl implements ICategoryBpsjMapper {
#Override
public CategoryBpsjResponseDto entityToDto(CategoryBPSJ entity) {
if ( entity == null ) {
return null;
}
CategoryBpsjResponseDto categoryBpsjResponseDto = new CategoryBpsjResponseDto();
if ( entity.createdDate != null ) {
categoryBpsjResponseDto.createdDate = new SimpleDateFormat( "dd-MM-yyyy HH:mm:ss" ).format( entity.createdDate );
}
if ( entity.modifiedDate != null ) {
categoryBpsjResponseDto.modifiedDate = new SimpleDateFormat( "dd-MM-yyyy HH:mm:ss" ).format( entity.modifiedDate );
}
categoryBpsjResponseDto.isDeleted = entity.isDeleted;
categoryBpsjResponseDto.reasonDeleted = entity.reasonDeleted;
categoryBpsjResponseDto.createdBy = entity.createdBy;
categoryBpsjResponseDto.modifiedBy = entity.modifiedBy;
return categoryBpsjResponseDto;
}
}
you see they access the property without get and set (i mean getter setter), i think this is the culprit that cause my error since when i marked the field as private, and if the code is written like that, they won't can be accessed. for now this is the way i solved it.
if someone have better answer, i will take their answer as accepted answer, if mine code not right or appropriate, waiting for it.
I think you're missing the lombok-mapstruct-binding.
Related
When I issue a Postman Get request, it returns duplicate result. Even when I had only three records in my database, Postman returns hundreds of same record duplicated.
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.resource</groupId>
<artifactId>akademiks</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>akademiks</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Entity class:
#Entity
#Table(name = "Chemistry")
public class Chemistry {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column(name = "date")
private Date date;
#Column(name = "question_no")
private Integer questionNo;
#Column(name = "question")
private String question;
#OneToOne(mappedBy = "question",cascade = CascadeType.ALL)
private ChemistryAnswer answer = new ChemistryAnswer();
public Chemistry() {}
public Chemistry(Date date, Integer questionNo, String question) {
this.date = date;
this.questionNo = questionNo;
this.question = question;
this.answer.setDate(date);
this.answer.setQuestionNo(questionNo);
this.answer.setQuestion(this);
}//accessors
Entity class:
#Entity
#Table(name = "Chemistryanswer")
public class ChemistryAnswer {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column(name = "date")
private Date date;
#Column(name = "question_no")
private Integer questionNo;
#Column(name = "solution")
private String solution;
#Column(name = "solution_url")
private String solutionUrl;
#OneToOne
#JoinColumn(name = "question_id")
private Chemistry question;
public ChemistryAnswer() {}
public ChemistryAnswer(Integer questionNo, String solution, String solutionUrl) {
this.questionNo = questionNo;
this.solution = solution;
this.solutionUrl = solutionUrl;
}
public ChemistryAnswer(Date date, Integer questionNo) {
this.date = date;
this.questionNo = questionNo;
}
public Date getDate() {
return date;
}//accessors
jpaRepository:
#RepositoryRestResource(collectionResourceRel = "chemistry", path = "chemistry")
public interface ChemistryRepo extends JpaRepository<Chemistry, Integer> {
}
Service class:
public interface ChemistryService {
public List<Chemistry>findAll();
public void save(Chemistry chemistry);
}
Service implementation:
#Service
public class ChemistryServiceImpl implements ChemistryService {
private ChemistryRepo repo;
public ChemistryServiceImpl() {}
#Autowired
public ChemistryServiceImpl(ChemistryRepo repo) {
this.repo = repo;
}
#Override
public List<Chemistry> findAll() {
return repo.findAll();
}
#Override
public void save(Chemistry chemistry) {
Chemistry tempChemistry = new Chemistry(chemistry.getDate(),
chemistry.getQuestionNo(), chemistry.getQuestion());
ChemistryAnswer answer = tempChemistry.getAnswer();
tempChemistry.setAnswer(answer);
repo.save(tempChemistry);
}
}
RestController class:
#RestController
public class ChemistryController {
private ChemistryService service;
#Autowired
public ChemistryController(ChemistryService service) {
this.service = service;
}
#GetMapping("/chemistries")
public ResponseEntity<Object>findAll(){
return new ResponseEntity<Object>(service.findAll(), HttpStatus.OK);
}
#PostMapping("/chemistry")
public void save(#RequestBody Chemistry chemistry,
HttpServletResponse response) throws IOException {
service.save(chemistry);
response.sendRedirect("/chemistries");
}
}
Stacktrace Get request:
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178) ~[jackson-databind-2.12.5.jar:2.12.5]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728) ~[jackson-databind-2.12.5.jar:2.12.5]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:770) ~[jackson-databind-2.12.5.jar:2.12.5]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178) ~[jackson-databind-2.12.5.jar:2.12.5]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728) ~[jackson-databind-2.12.5.jar:2.12.5]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:770) ~[jackson-databind-2.12.5.jar:2.12.5]
The issue you are facing is quite abstract. "Postman returns hundreds of same record duplicated." does not tell us much. Still, I guess that the issue is your bi-directional relationship. Try adding #JsonManagedReference and #JsonBackReference to your bi-directional relationship in your model as follows:
#JsonManagedReference
#OneToOne(mappedBy = "question",cascade = CascadeType.ALL)
private ChemistryAnswer answer = new ChemistryAnswer();
#OneToOne
#JsonBackReference
#JoinColumn(name = "question_id")
private Chemistry question;
Here's my stack:
- Micronaut 1.3.2
- Java 8
- Eclipse STS 4
Pom.xml excerpt:
<dependency>
<groupId>io.micronaut.configuration</groupId>
<artifactId>micronaut-jdbc-hikari</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-hibernate-jpa</artifactId>
</dependency>
<dependency>
<groupId>io.micronaut.configuration</groupId>
<artifactId>micronaut-hibernate-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<path>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-processor</artifactId>
<version>1.0.0.M1</version>
</path>
My Repository:
import io.micronaut.data.annotation.Repository;
import io.micronaut.data.repository.CrudRepository;
#Repository
public interface UsuarioRepository extends CrudRepository<UsuarioModel, Long> { }
My Service:
private UsuarioDTO atualizar(UsuarioDTO usuario) {
UsuarioModel modelo = new UsuarioModel();
modelo.setIdade(usuario.idade);
modelo.setNick(usuario.nick);
modelo.setNome(usuario.nome);
modelo.setPassword(usuario.password);
this.usuarioRepository.update(modelo);
return usuario;
}
application.yml:
micronaut:
application:
name: micronaut-chatroom
server:
cors:
enabled: true
datasources:
default:
url: jdbc:h2:mem:devDb
driverClassName: org.h2.Driver
username: sa
password: ''
schema-generate: CREATE_DROP
dialect: H2
jpa:
default:
entity-scan:
packages: 'com.cr.model'
When I'm running Install maven command on my Micronaut project I get the error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project micronaut-chatroom: Compilation failure
[ERROR] Unable to implement Repository method: UsuarioRepository.update(Object entity). No possible implementations found.
Has someone been through it?
Thanks in advance!
EDIT
Here's my UsuarioModel class
#Entity(name = "Usuario")
#Table(name = "usuario")
public class UsuarioModel implements Serializable {
/**
*
*/
private static final long serialVersionUID = -7468837154917949190L;
#Id
#GeneratedValue
private Long id;
private String nome;
private String nick;
private String idade;
private String password;
#OneToMany
private List<MensagemModel> mensagens;
//Getter/Setters ommited
}
And also my MensagemModel class:
#Entity(name = "Mensagem")
#Table(name = "mensagem")
public class MensagemModel implements Serializable {
/**
*
*/
private static final long serialVersionUID = 7634793331670289187L;
#Id
#GeneratedValue
private Long id;
#CreationTimestamp
private LocalDateTime data;
private String conteudo;
#ManyToOne
#JoinColumn(name = "usuario_id")
private UsuarioModel usuario;
//Getter/Setter ommited
}
EDIT:
There are 2 possible sources for the #Id decorator in micronaut:
the "native" annotation package: io.micronaut.data.annotation
the jpa persistance package available as: jakarta.persistence-api dependency in maven
As you are decorating the class with #Entity I suppose you are using the latter set of decorators. or at least a mix of the 2 sources.
As basic debug step you could try removing any reference to javax.persistance and use native only decorators. So, as instance, do not use #Entity but rather #MappedEntity and so on.
simply comment out any import from javax.persistence.* and try with native io.micronaut.data.annotation*.
this would make clearer - I hope- if the issue lies in some twist of the annotation process.
OLD RUBBISH HAS BEEN DELETED - was Kotlin related
I created a project using spring boot 2.1.4.RELEASE with the following dependencies:
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
I have the following entity and repository:
#Entity
public class Person {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column(name = "name")
private String name;
#Column(name = "last_name")
private String lastName;
#Column(name = "age")
private Integer age;
#Column(name = "createdAt")
#Temporal(TemporalType.TIMESTAMP)
private Date createdAt;
...
}
PersonRepository.java
#Repository
public interface PersonRepository extends CrudRepository<Person, Integer> {
}
The following is my Application class:
#SpringBootApplication
public class SpringDataApplication {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(SpringDataApplication.class, args);
PersonRepository personRepository = context.getBean(PersonRepository.class);
Person p1 = new Person("Juan", "Camaney", 55);
Person p2 = new Person("Arturo", "Lopez", 33);
Person p3 = new Person("Pancho", "Coscorin", 22);
personRepository.save(p1);
personRepository.save(p2);
personRepository.save(p3);
Iterator<Person> people = personRepository.findAll().iterator();
while (people.hasNext()) {
Person temp = people.next();
System.out.println(temp);
}
}
}
If i execute my application i get the following error:
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.devs4j.spring.data.repositories.PersonRepository' available
The solution of it is add the following configuration class:
#Configuration
#EnableJpaRepositories("com.devs4j.spring.data.repositories")
public class JpaConfiguration {
}
But I get the error :
EnableJpaRepositories cannot be resolved to a type
If i downgrade to 2.0.5.RELEASE everything works fine.
I'm confused because when i check the following spring documentation https://docs.spring.io/spring-data/jpa/docs/2.1.6.RELEASE/reference/html/ I see that it is still using #EnableJpaRepositories("com.acme.repositories")
Am i doing something wrong ?
In your configuration.java, have you added the import:
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
I have searched on StackOverflow for similar problems, and if I remove
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
everything works. However I need the data-jpa.
I have created 2 POJOs:
#Entity
public class Author {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
#ManyToMany
private Set<Book> books = new HashSet<>();
public Author(){}
}
And the second one:
#Entity
public class Book {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
private String isbn;
private String publisher;
#ManyToMany
private Set<Author> authors = new HashSet<>();
public Book(String title, String isbn, String publisher) {
this.title = title;
this.isbn = isbn;
this.publisher = publisher;
}
}
I have added 2 Entities and as I understand, I should no longer get the error At least one JPA metamodel must be present. Why I get this error?
DemoApplication.class
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
My pom.xml dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.3.Final</version>
</dependency>
</dependencies>
I am to low in reputation so i can't comment the question.
Can you share your full maven pom.xml please? The solution you are referencing is this answer i guess? In this answer they do not recommend to remove spring-boot-starter-data-jpa but to remove an explicit dependency to an "old" spring-data-jpa.
Make sure to share your pom file, i can only guess right now.
Update:
The dependencies hibernate-core and hibernate-entititymanager are already provided by spring-boot-starter-jpa and you override the managed versions with your custom versions which leads to your error.
Try to remove your explicit hibernate-* dependencies.
I'm currently working on an application for an university project. For this project, my team and I are using a Vaadin/Spring/Maven configuration. With this we also include a database connected with repositories.
The repositories are build the following way. First a class for objects:
import javax.persistence.*;
import java.util.Set;
#Entity
#Table (name = "component")
public class Component {
#Id #GeneratedValue(strategy = GenerationType.AUTO)
long id;
String name;
float price;
boolean isVegetarian;
#OneToMany(mappedBy = "component", cascade = {CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.EAGER)
Set<InvoicePosition> Positions;
#ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
ComponentCategory category_id;
String path;
public Component() {
}
public Component(String name, float price, boolean isVegetarian, ComponentCategory category, String path) {
this.name = name;
this.price = price;
this.isVegetarian = isVegetarian;
this.category_id = category;
this.path = path;
}
Then the additional repository:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface ComponentRepository extends JpaRepository<Component, Long>{}
Then there is a repository for component category, in which we should be able to store a list of components:
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
#Entity
#Table(name = "componentCategory")
public class ComponentCategory {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
Long id;
String name;
#OneToMany(mappedBy = "category_id", cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
List<Component> components;
public ComponentCategory() { }
public ComponentCategory(String name) {
this.name = name;
components = new ArrayList<>();
}
public void addComponentToCategory(Component component) {
if(!components.contains(component))
{
components.add(component);
component.category_id = this;
}
}
And then the same repository:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface ComponentCategoryRepository extends JpaRepository<ComponentCategory, Long> {}
I am able to create new entries and they are also properly stored in the database. This is all stored in the Application.Java file to start the Vaadin application
ComponentCategory cat1 = new ComponentCategory("examp1");
Component test1 = new Component("example1", 1.25f, true, cat1, "");
Component test2 = new Component("example2", 0.89f, true, cat1, "");
cat1.addComponentToCategory(test1);
cat1.addComponentToCategory(test2);
compcatrep.saveAndFlush(comp); // compcatrep is the ComponentCategoryRepository, which is passed on to the init() function
Now I'm trying to update the entries in the database with the following logic:
Component test11 = new Component("Test", 2.15f, true, compcatrep.getOne((long)4), "");
ComponentCategory comp = compcatrep.getOne((long)4);
comp.addComponentToCategory(test11);
compcatrep.saveAndFlush(comp);
I made sure that there is an entry with the ID (long) 4. It does work and does not show me any error, when I execute the application. Also if I check the length of the stored list in the ComponentCategory after the saveAndFlush():
System.out.println(comp.getComponents().size());
System.out.println(compcatrep.getOne((long)4).getComponents().size());
I do receive different length of the lists. The second Syso does show one less then the first. I tried a lot and did not find the error. Some more information about my issue:
ApplicationProperties in target folder:
spring.datasource.url=jdbc:h2:file:./mensarioDB;DB_CLOSE_ON_EXIT=TRUE
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.hs-augsburg.bpap</groupId>
<artifactId>mensario</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mensario</name>
<description>mensario management</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<vaadin.version>8.1.0</vaadin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-bom</artifactId>
<version>${vaadin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>