JAVA-11535 Move spring-data related modules to persistence-modules (#12126)
This commit is contained in:
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung;
|
||||
|
||||
import com.baeldung.controller.repository.AddressRepository;
|
||||
import com.baeldung.controller.repository.UserRepository;
|
||||
import com.baeldung.entity.Address;
|
||||
import com.baeldung.entity.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
/**
|
||||
* Note: In the IDE, remember to generate query type classes before running the Integration Test (e.g. in Eclipse right-click on the project > Run As > Maven generate sources)
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EntityScan("com.baeldung.entity")
|
||||
@EnableJpaRepositories("com.baeldung.controller.repository")
|
||||
@EnableAutoConfiguration
|
||||
public class Application {
|
||||
|
||||
@Autowired
|
||||
private UserRepository personRepository;
|
||||
@Autowired
|
||||
private AddressRepository addressRepository;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void initializeData() {
|
||||
// Create John
|
||||
final User john = new User("John");
|
||||
personRepository.save(john);
|
||||
final Address addressOne = new Address("Fake Street 1", "Spain", john);
|
||||
addressRepository.save(addressOne);
|
||||
// Create Lisa
|
||||
final User lisa = new User("Lisa");
|
||||
personRepository.save(lisa);
|
||||
final Address addressTwo = new Address("Real Street 1", "Germany", lisa);
|
||||
addressRepository.save(addressTwo);
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.controller;
|
||||
|
||||
import com.baeldung.controller.repository.AddressRepository;
|
||||
import com.baeldung.controller.repository.UserRepository;
|
||||
import com.baeldung.entity.Address;
|
||||
import com.baeldung.entity.User;
|
||||
import com.querydsl.core.BooleanBuilder;
|
||||
import com.querydsl.core.types.Predicate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.querydsl.binding.QuerydslPredicate;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class QueryController {
|
||||
|
||||
@Autowired
|
||||
private UserRepository personRepository;
|
||||
@Autowired
|
||||
private AddressRepository addressRepository;
|
||||
|
||||
@GetMapping(value = "/users", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public Iterable<User> queryOverUser(@QuerydslPredicate(root = User.class) Predicate predicate) {
|
||||
final BooleanBuilder builder = new BooleanBuilder();
|
||||
return personRepository.findAll(builder.and(predicate));
|
||||
}
|
||||
|
||||
@GetMapping(value = "/addresses", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public Iterable<Address> queryOverAddress(@QuerydslPredicate(root = Address.class) Predicate predicate) {
|
||||
final BooleanBuilder builder = new BooleanBuilder();
|
||||
return addressRepository.findAll(builder.and(predicate));
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.controller.repository;
|
||||
|
||||
import com.baeldung.entity.Address;
|
||||
import com.baeldung.entity.QAddress;
|
||||
import com.querydsl.core.types.dsl.StringExpression;
|
||||
import com.querydsl.core.types.dsl.StringPath;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
|
||||
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
|
||||
import org.springframework.data.querydsl.binding.QuerydslBindings;
|
||||
import org.springframework.data.querydsl.binding.SingleValueBinding;
|
||||
|
||||
public interface AddressRepository
|
||||
extends JpaRepository<Address, Long>, QuerydslPredicateExecutor<Address>, QuerydslBinderCustomizer<QAddress> {
|
||||
@Override
|
||||
default void customize(final QuerydslBindings bindings, final QAddress root) {
|
||||
bindings.bind(String.class).first((SingleValueBinding<StringPath, String>) StringExpression::eq);
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.controller.repository;
|
||||
|
||||
import com.baeldung.entity.QUser;
|
||||
import com.baeldung.entity.User;
|
||||
import com.querydsl.core.types.dsl.StringExpression;
|
||||
import com.querydsl.core.types.dsl.StringPath;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
|
||||
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
|
||||
import org.springframework.data.querydsl.binding.QuerydslBindings;
|
||||
import org.springframework.data.querydsl.binding.SingleValueBinding;
|
||||
|
||||
public interface UserRepository
|
||||
extends JpaRepository<User, Long>, QuerydslPredicateExecutor<User>, QuerydslBinderCustomizer<QUser> {
|
||||
@Override
|
||||
default void customize(final QuerydslBindings bindings, final QUser root) {
|
||||
bindings.bind(String.class).first((SingleValueBinding<StringPath, String>) StringExpression::eq);
|
||||
}
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.entity;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
|
||||
public class Address {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id", unique = true, nullable = false)
|
||||
private Long id;
|
||||
@Column
|
||||
private String address;
|
||||
@Column
|
||||
private String country;
|
||||
@OneToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "user_id")
|
||||
private User user;
|
||||
|
||||
public Address() {
|
||||
}
|
||||
|
||||
public Address(String address, String country, User user) {
|
||||
this.id = id;
|
||||
this.address = address;
|
||||
this.country = country;
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.entity;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
|
||||
@Entity
|
||||
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id", unique = true, nullable = false)
|
||||
private Long id;
|
||||
@Column
|
||||
private String name;
|
||||
@OneToOne(fetch = FetchType.LAZY, mappedBy = "user")
|
||||
private Address address;
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
spring:
|
||||
datasource:
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
hikari:
|
||||
pool-name: hikari-pool
|
||||
url: jdbc:mysql://localhost:3306/baeldung?verifyServerCertificate=false&useSSL=false&requireSSL=false
|
||||
username: root
|
||||
|
||||
jpa:
|
||||
hibernate:
|
||||
ddl-auto: create
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user