Spring 5 at least one JPA metamodel must be present - java

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.

Related

Bad request in RestController spring boot 2.7

I checked in several different ways to check where is bug but I still do not know the answer.
That is my RestController
#RestController
public class CustomerController {
#PostMapping(value = "/customer")
public ResponseEntity<CustomerResponse> addCustomer(#RequestBody #Valid Customer custRequest) throws Exception {
ModelMapper modelMapper = new ModelMapper();
CustomerDto customerDto = modelMapper.map(custRequest, CustomerDto.class);
CustomerDto addCust = customer.addCustomer(customerDto);
CustomerResponse custResponse = modelMapper.map(addCust, CustomerResponse.class);
return new ResponseEntity<CustomerResponse>(custResponse, HttpStatus.CREATED);
}
}
That is my Model
#Entity
#Table(name = "customers")
public class Customer {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String idCustomer;
private String email;
#OneToMany(mappedBy = "customer",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List<Choice> choices;
// Getter and setter and constructor
}
maven dependencies
<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-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
response of postman
How can i resolve this problem to post a new customer .
#Valid lets the framework check against the parameter on the method invocation and, if the validation fails, an HTTP 400 Bad Request status is thrown. You are having a complex type and that type requires a validation too. Here's a link:
https://medium.com/javarevisited/are-you-using-valid-and-validated-annotations-wrong-b4a35ac1bca4
Try removing the #Valid just to see the difference and the above article gives you an example implementation.
Also, using an Entity as a Request object is a highly discouraged paradigm. You are encouraged to make a DTO object specific to your request as a start. There are situations where you have to inherit, but this question's scope is not that.
The validation of Customer seems to fail.
I would
look at the logs to error details
think about having a separate class for the request object. You do not really want to mix database related and request related concerns.

Spring Boot with JPA does not generate tables in the database

When I run the project, does not create tables in MySQL.
I tried to change the spring.jpa.hibernate.ddl-auto = update to create-drop and also does not to create.
No error appears in the log.
My class
#NoArgsConstructor
#AllArgsConstructor
#Data
#Entity(name = "tb_user")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(length = 75, nullable = false)
private String name;
#Column(length = 75, nullable = false, unique = true)
private String email;
#Column(length = 100, nullable = false)
private String password;
}
My pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
</dependencies>
my application.properties
spring.datasource.url=jdbc:mysql://localhost:3307/db_spring?useTimezone=true&serverTimezone=UTC
spring.datasource.username=sa
spring.datasource.password=1234567
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.data.jpa.repositories.enabled=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
Try adding
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
also check you are using #EnableAutoConfiguration and entity classes are in same package or subpackage

Error processing condition on org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.jpaVendorAdapter

So this is Error Message :
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: java.lang.IllegalStateException:
Error processing condition on org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.jpaVendorAdapter
Caused by: java.lang.IllegalStateException:
Failed to introspect Class [org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration] from ClassLoader [sun.misc.Launcher$AppClassLoader#42a57993]
Caused by: java.lang.NoClassDefFoundError: org/hibernate/boot/model/naming/PhysicalNamingStrategy
Caused by: java.lang.ClassNotFoundException: org.hibernate.boot.model.naming.PhysicalNamingStrategy
<?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.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.test1</groupId>
<artifactId>test1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test1</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-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.3.Final</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.4.Final</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>`
#Entity
#Table(name= "ordertransactions")
public class OrderTransaction {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
#Column(name="order_name",nullable = false)
private String OrderName;
#OneToMany
#JoinColumn(name = "Customer_Id", referencedColumnName = "id", updatable = true,nullable = false)
private Customer customer;
public OrderTransaction(Long id, String OrderName) {
this.id = id;
this.OrderName = OrderName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getOrderName() {
return OrderName;
}
public void setOrderName(String OrderName) {
this.OrderName = OrderName;
}
}
public interface CustomerRepository extends JpaRepository <Customer, Long> {
#Modifying
#Query(value ="SELECT ordertransactions.id, Customer.name" +
"FROM ordertransactions" +
"INNER JOIN Customer ON orderransactions.id = customer.id;" ,nativeQuery = true)
int deleteCustomer(Customer Customer);
#Query(value="SELECT name, city FROM Customer")
Customer findCustomerByName(#Param("nameCustomer") String name);
}
so i want to join CustomerId in table ordertransaction, i had solved by myself but i dont found the solution.. please help me.

Spring boot h2 auto configuration not working

Even though there are already many questions on this topic, non of the answers have helped me so far.
The code of my application.properties:
spring.h2.console.enabled=true
spring.datasource.platform=h2
spring.datasource.url=jdbc:h2:mem:testdb
One example of my entities:
import java.util.Date;
import javax.persistence.*;
import org.springframework.boot.autoconfigure.*;
#Entity
#EnableAutoConfiguration
public class Customer {
#Id
private int customerId;
private String name;
private String street;
private String zip;
private String city;
private Date differenceSince;
private Date lastUpdatedOn;
#OneToOne
private User lastUpdatedBy;
//getters and setters ...
}
The 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-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
I cannot find the issue, but the H2 Console doesn't contain any tables:
Any ideas on what I am missing?
try to set your application.properties as below
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username= (username)
spring.datasource.password=(password)
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

#EnableJpaRepositories is not present in spring boot 2.1.4.RELEASE

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;

Categories

Resources