Java 26374 Identify and move popular Boot articles (#15139)
This commit is contained in:
+77
@@ -0,0 +1,77 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Foo implements Serializable {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
public Foo() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Foo(final String name) {
|
||||
super();
|
||||
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
final Foo other = (Foo) obj;
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("Foo [name=").append(name).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class FooService implements IFooService {
|
||||
@Autowired
|
||||
private IFooDAO dao;
|
||||
|
||||
@Override
|
||||
public Foo create(Foo foo) {
|
||||
return dao.save(foo);
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
public interface IFooDAO extends JpaRepository<Foo, Long> {
|
||||
|
||||
Foo findByName(String name);
|
||||
|
||||
@Query("SELECT f FROM Foo f WHERE LOWER(f.name) = LOWER(:name)")
|
||||
Foo retrieveByName(@Param("name") String name);
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
public interface IFooService {
|
||||
Foo create(Foo foo);
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@Configuration
|
||||
@PropertySource("classpath:persistence.properties")
|
||||
@ComponentScan("com.baeldung.repository")
|
||||
//@ImportResource("classpath*:*springDataConfig.xml")
|
||||
@EnableTransactionManagement
|
||||
@EnableJpaRepositories(basePackages = "com.baeldung.repository")
|
||||
public class PersistenceConfig {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
public PersistenceConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(dataSource());
|
||||
em.setPackagesToScan("com.baeldung.spring.data.persistence.repository");
|
||||
|
||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
em.setJpaVendorAdapter(vendorAdapter);
|
||||
em.setJpaProperties(additionalProperties());
|
||||
|
||||
return em;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
|
||||
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
|
||||
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
|
||||
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
|
||||
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlatformTransactionManager transactionManager() {
|
||||
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
|
||||
|
||||
return transactionManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
||||
return new PersistenceExceptionTranslationPostProcessor();
|
||||
}
|
||||
|
||||
final Properties additionalProperties() {
|
||||
final Properties hibernateProperties = new Properties();
|
||||
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||
|
||||
return hibernateProperties;
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.spring.data.persistence.like;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class LikeApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(LikeApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.spring.data.persistence.like.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Movie {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
private Long id;
|
||||
private String title;
|
||||
private String director;
|
||||
private String rating;
|
||||
private int duration;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getDirector() {
|
||||
return director;
|
||||
}
|
||||
|
||||
public void setDirector(String director) {
|
||||
this.director = director;
|
||||
}
|
||||
|
||||
public String getRating() {
|
||||
return rating;
|
||||
}
|
||||
|
||||
public void setRating(String rating) {
|
||||
this.rating = rating;
|
||||
}
|
||||
|
||||
public int getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public void setDuration(int duration) {
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.spring.data.persistence.like.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import com.baeldung.spring.data.persistence.like.model.Movie;
|
||||
|
||||
public interface MovieRepository extends CrudRepository<Movie, Long> {
|
||||
|
||||
List<Movie> findByTitleContaining(String title);
|
||||
|
||||
List<Movie> findByTitleLike(String title);
|
||||
|
||||
List<Movie> findByTitleContains(String title);
|
||||
|
||||
List<Movie> findByTitleIsContaining(String title);
|
||||
|
||||
List<Movie> findByRatingStartsWith(String rating);
|
||||
|
||||
List<Movie> findByDirectorEndsWith(String director);
|
||||
|
||||
List<Movie> findByTitleContainingIgnoreCase(String title);
|
||||
|
||||
List<Movie> findByRatingNotContaining(String rating);
|
||||
|
||||
List<Movie> findByDirectorNotLike(String director);
|
||||
|
||||
@Query("SELECT m FROM Movie m WHERE m.title LIKE %:title%")
|
||||
List<Movie> searchByTitleLike(@Param("title") String title);
|
||||
|
||||
@Query("SELECT m FROM Movie m WHERE m.rating LIKE ?1%")
|
||||
List<Movie> searchByRatingStartsWith(String rating);
|
||||
|
||||
//Escaping works in SpringBoot >= 2.4.1
|
||||
//@Query("SELECT m FROM Movie m WHERE m.director LIKE %?#{escape([0])} escape ?#{escapeCharacter()}")
|
||||
@Query("SELECT m FROM Movie m WHERE m.director LIKE %:#{[0]}")
|
||||
List<Movie> searchByDirectorEndsWith(String director);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
# jdbc.X
|
||||
jdbc.driverClassName=org.h2.Driver
|
||||
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
|
||||
jdbc.user=sa
|
||||
jdbc.pass=sa
|
||||
|
||||
# hibernate.X
|
||||
hibernate.hbm2ddl.auto=create-drop
|
||||
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/data/jpa
|
||||
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
|
||||
>
|
||||
<jpa:repositories base-package="com.baeldung.repository"/>
|
||||
</beans>
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = PersistenceConfig.class)
|
||||
public class FooServiceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private IFooService service;
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@Test(expected = DataIntegrityViolationException.class)
|
||||
public final void whenInvalidEntityIsCreated_thenDataException() {
|
||||
service.create(new Foo());
|
||||
}
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
package com.baeldung.spring.data.persistence.like;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.spring.data.persistence.like.model.Movie;
|
||||
import com.baeldung.spring.data.persistence.like.repository.MovieRepository;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@Sql(scripts = { "/test-movie-data.sql" })
|
||||
@SpringBootTest(classes = LikeApplication.class)
|
||||
@Sql(scripts = "/test-movie-cleanup.sql", executionPhase = AFTER_TEST_METHOD)
|
||||
public class MovieRepositoryIntegrationTest {
|
||||
@Autowired
|
||||
private MovieRepository movieRepository;
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@Test
|
||||
public void givenPartialTitle_WhenFindByTitleContaining_ThenMoviesShouldReturn() {
|
||||
List<Movie> results = movieRepository.findByTitleContaining("in");
|
||||
assertEquals(3, results.size());
|
||||
|
||||
results = movieRepository.findByTitleLike("%in%");
|
||||
assertEquals(3, results.size());
|
||||
|
||||
results = movieRepository.findByTitleIsContaining("in");
|
||||
assertEquals(3, results.size());
|
||||
|
||||
results = movieRepository.findByTitleContains("in");
|
||||
assertEquals(3, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStartOfRating_WhenFindByRatingStartsWith_ThenMoviesShouldReturn() {
|
||||
List<Movie> results = movieRepository.findByRatingStartsWith("PG");
|
||||
assertEquals(6, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLastName_WhenFindByDirectorEndsWith_ThenMoviesShouldReturn() {
|
||||
List<Movie> results = movieRepository.findByDirectorEndsWith("Burton");
|
||||
assertEquals(1, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPartialTitle_WhenFindByTitleContainingIgnoreCase_ThenMoviesShouldReturn() {
|
||||
List<Movie> results = movieRepository.findByTitleContainingIgnoreCase("the");
|
||||
assertEquals(2, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPartialTitle_WhenSearchByTitleLike_ThenMoviesShouldReturn() {
|
||||
List<Movie> results = movieRepository.searchByTitleLike("in");
|
||||
assertEquals(3, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStartOfRating_SearchFindByRatingStartsWith_ThenMoviesShouldReturn() {
|
||||
List<Movie> results = movieRepository.searchByRatingStartsWith("PG");
|
||||
assertEquals(6, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLastName_WhenSearchByDirectorEndsWith_ThenMoviesShouldReturn() {
|
||||
List<Movie> results = movieRepository.searchByDirectorEndsWith("Burton");
|
||||
assertEquals(1, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPartialRating_findByRatingNotContaining_ThenMoviesShouldReturn() {
|
||||
List<Movie> results = movieRepository.findByRatingNotContaining("PG");
|
||||
assertEquals(1, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPartialDirector_WhenFindByDirectorNotLike_ThenMoviesShouldReturn() {
|
||||
List<Movie> results = movieRepository.findByDirectorNotLike("An%");
|
||||
assertEquals(5, results.size());
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -1,2 +1,2 @@
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.datasource.url=jdbc:h2:mem:jpa3
|
||||
#spring.jpa.hibernate.ddl-auto=update
|
||||
#spring.datasource.url=jdbc:h2:mem:jpa3
|
||||
@@ -0,0 +1 @@
|
||||
DELETE FROM movie;
|
||||
@@ -0,0 +1,7 @@
|
||||
INSERT INTO movie(id,title,director,rating,duration) VALUES(1, 'Godzilla: King of the Monsters', ' Michael Dougherty', 'PG-13', 132);
|
||||
INSERT INTO movie(id,title,director,rating,duration) VALUES(2, 'Avengers: Endgame', 'Anthony Russo', 'PG-13', 181);
|
||||
INSERT INTO movie(id,title,director,rating,duration) VALUES(3, 'Captain Marvel', 'Anna Boden', 'PG-13', 123);
|
||||
INSERT INTO movie(id,title,director,rating,duration) VALUES(4, 'Dumbo', 'Tim Burton', 'PG', 112);
|
||||
INSERT INTO movie(id,title,director,rating,duration) VALUES(5, 'Booksmart', 'Olivia Wilde', 'R', 102);
|
||||
INSERT INTO movie(id,title,director,rating,duration) VALUES(6, 'Aladdin', 'Guy Ritchie', 'PG', 128);
|
||||
INSERT INTO movie(id,title,director,rating,duration) VALUES(7, 'The Sun Is Also a Star', 'Ry Russo-Young', 'PG-13', 100);
|
||||
Reference in New Issue
Block a user