BAEL-3809: Ensure tables are dropped in proper order (#8640)

This commit is contained in:
kwoyke 2020-01-31 21:48:23 +01:00 committed by GitHub
parent d0225e59ce
commit d55081bd7a
9 changed files with 48 additions and 31 deletions

View File

@ -659,6 +659,7 @@
<module>spring-boot-mvc-2</module> <module>spring-boot-mvc-2</module>
<module>spring-boot-nashorn</module> <module>spring-boot-nashorn</module>
<module>spring-boot-parent</module> <module>spring-boot-parent</module>
<module>spring-boot-performance</module>
<module>spring-boot-property-exp</module> <module>spring-boot-property-exp</module>
<module>spring-boot-rest</module> <module>spring-boot-rest</module>
@ -1187,6 +1188,7 @@
<module>spring-boot-mvc-2</module> <module>spring-boot-mvc-2</module>
<module>spring-boot-nashorn</module> <module>spring-boot-nashorn</module>
<module>spring-boot-parent</module> <module>spring-boot-parent</module>
<module>spring-boot-performance</module>
<module>spring-boot-property-exp</module> <module>spring-boot-property-exp</module>
<module>spring-boot-rest</module> <module>spring-boot-rest</module>

View File

@ -22,7 +22,6 @@
<!-- <module>spring-boot-gradle</module> --> <!-- Not a maven project --> <!-- <module>spring-boot-gradle</module> --> <!-- Not a maven project -->
<module>spring-boot-keycloak</module> <module>spring-boot-keycloak</module>
<module>spring-boot-mvc-birt</module> <module>spring-boot-mvc-birt</module>
<module>spring-boot-performance</module>
<module>spring-boot-properties</module> <module>spring-boot-properties</module>
<module>spring-boot-springdoc</module> <module>spring-boot-springdoc</module>
<module>spring-boot-testing</module> <module>spring-boot-testing</module>

View File

@ -11,7 +11,7 @@
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId> <artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath> <relativePath>../parent-boot-2</relativePath>
</parent> </parent>
<dependencies> <dependencies>

View File

@ -3,14 +3,17 @@ package com.baeldung.models;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection; import javax.persistence.ElementCollection;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType; import javax.persistence.FetchType;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Table;
@Entity @Entity
@Table(name = "Tweet")
public class Tweet { public class Tweet {
@Id @Id
@GeneratedValue(strategy = GenerationType.SEQUENCE) @GeneratedValue(strategy = GenerationType.SEQUENCE)
@ -18,7 +21,8 @@ public class Tweet {
private String tweet; private String tweet;
private String owner; private String owner;
@ElementCollection(targetClass = String.class, fetch = FetchType.EAGER) @ElementCollection(targetClass = String.class, fetch = FetchType.EAGER)
private Set<String> likes = new HashSet(); @CollectionTable(name = "Tweet_Likes")
private Set<String> likes = new HashSet<>();
public long getId() { public long getId() {
return id; return id;

View File

@ -1,29 +1,5 @@
package com.baeldung.relationships; package com.baeldung.relationships;
import static org.springframework.util.Assert.isTrue;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletContext;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import com.baeldung.AppConfig; import com.baeldung.AppConfig;
import com.baeldung.data.repositories.TweetRepository; import com.baeldung.data.repositories.TweetRepository;
import com.baeldung.data.repositories.UserRepository; import com.baeldung.data.repositories.UserRepository;
@ -31,6 +7,30 @@ import com.baeldung.models.AppUser;
import com.baeldung.models.Tweet; import com.baeldung.models.Tweet;
import com.baeldung.security.AppUserPrincipal; import com.baeldung.security.AppUserPrincipal;
import com.baeldung.util.DummyContentUtil; import com.baeldung.util.DummyContentUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import javax.servlet.ServletContext;
import java.util.Date;
import java.util.List;
import static org.springframework.util.Assert.isTrue;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@WebAppConfiguration @WebAppConfiguration
@ -54,10 +54,22 @@ public class SpringDataWithSecurityIntegrationTest {
tweetRepository.saveAll(DummyContentUtil.generateDummyTweets(appUsers)); tweetRepository.saveAll(DummyContentUtil.generateDummyTweets(appUsers));
} }
@AfterClass /**
public static void tearDown() { * This is to ensure the tables are dropped in proper order.
tweetRepository.deleteAll(); * After the Spring Boot 2.2.2 upgrade, DDL statements generated automatically try to drop Tweet table first.
userRepository.deleteAll(); * As a result we get org.h2.jdbc.JdbcSQLSyntaxErrorException because Tweet_Likes table depends on Tweet.
*
* @see <a href="https://stackoverflow.com/questions/59364212/integrationtest-isolation-fails-in-springboot-2-2-2-release-error-dopping-table">
* StackOverflow#59364212
* </a>
* @see <a href="https://stackoverflow.com/questions/59561551/hibernate-h2-specify-drop-table-order">
* StackOverflow#59561551
* </a>
*/
@After
public void tearDown() {
JdbcTemplate jdbcTemplate = ctx.getBean(JdbcTemplate.class);
JdbcTestUtils.dropTables(jdbcTemplate, "Tweet_Likes", "Tweet");
} }
@Test @Test