Ciao Andbin, scusa se ritorno sull'argomento, pensavo di aver capito tutto ma in realtà non era così. Ho trovato una seconda opzione per far funzionare a dovere l'applicazione. Invece di creare un metodo per caricare nel DBMS i record in fase di test (vedi Test_00_createRecordBase()) ho modificato l'assegnazione dell'id nel DBMS (vedi strategy=GenerationType.IDENTITY). Cambiando il tipo di assegnazione dell'id (IDENTITY, SEQUENCE, AUTO, TABLE), il metodo di caricamento (Test_00_createRecordBase() oppure data.sql) e la tipologia di database (H2 o MySQL) sono usciti fuori 16 casi.
CASI ANALIZZATI
A) @GeneratedValue(strategy=GenerationType.IDENTITY)
1) H2 con file data.sql >> OK! (6 su 6 sono ok)
2) H2 con metodo in java Test_00_createRecordBase() >> OK! (7 su 7 sono ok)
3) MySQL con file data.sql >> OK! (6 su 6 sono ok)
4) MySQL con metodo in java Test_00_createRecordBase() >> OK! (7 su 7 sono ok)
B) @GeneratedValue(strategy=GenerationType.AUTO)
1) H2 con file data.sql >> NO! (solo 5 su 6 ok sono ok)
2) H2 con metodo in java Test_00_createRecordBase() >> OK! (7 su 7 sono ok)
3) MySQL con file data.sql >> NO! (solo 5 su 6 sono ok)
4) MySQL con metodo in java Test_00_createRecordBase() >> OK! (7 su 7 sono ok)
C) @GeneratedValue(strategy=GenerationType.SEQUENCE)
1) H2 con file data.sql >> NO! (solo 5 su 6 sono ok)
2) H2 con metodo in java Test_00_createRecordBase() >> OK! (7 su 7 sono ok)
3) MySQL con file data.sql >> NO! (solo 5 su 6 sono ok)
4) MySQL con metodo in java Test_00_createRecordBase() >> OK! (7 su 7 sono ok)
D) @GeneratedValue(strategy=GenerationType.TABLE)
1) H2 con file data.sql >> NO! (solo 5 su 6 sono ok)
2) H2 con metodo in java Test_00_createRecordBase() >> OK! (7 su 7 sono ok)
3) MySQL con file data.sql >> NO! (solo 5 su 6 sono ok)
4) MySQL con metodo in java Test_00_createRecordBase() >> NO! (solo 4 su 7 sono ok)
Non so come sia possibile che all'autore funzioni tutto con .AUTO. A me funziona tutto sono con .IDENTITY. Quello che vorrei capire sono le motivazioni del fallimento di alcune di queste strategie. Sapresti rispondere a questa domanda?
SpringbootJPADemoApplicationTests.java
package com.apress.demo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.FixMethodOrder;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes=SpringbootJPADemoApplication.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SpringbootJPADemoApplicationTests
{
	@Autowired
	private UserRepository userRepository;
	// Sostituisce il file data.sql
	/*@Test
	public void Test_00_createRecordBase() {
		User user = new User(null, "John", "john@gmail.com",true);
		User savedUser = userRepository.save(user);
		user = new User(null, "Rod", "rod@gmail.com");
		savedUser = userRepository.save(user);
		user = new User(null, "Becky", "becky@gmail.com");
		savedUser = userRepository.save(user);
		List<User> users = userRepository.findAll();
		// Si verifica che 'users' non sia nullo.
		assertNotNull(users);
		// Si verifica che 'users' non sia vuoto.
		assertTrue(!users.isEmpty());
	}*/
	@Test
	public void Test_01_findAllUsers()  {
		List<User> users = userRepository.findAll();
		assertNotNull(users);
		assertTrue(!users.isEmpty());
		
	}
	@Test
	public void Test_02_findUserById()  {
		User user = userRepository.getOne(1);
		assertNotNull(user);
	}
	@Test //@Ignore
	public void Test_03_createUser() {
		User user = new User(null, "Paul", "paul@gmail.com");
		User savedUser = userRepository.save(user);
		User newUser = userRepository.findById(savedUser.getId()).get();
		assertEquals("Paul", newUser.getName());
		assertEquals("paul@gmail.com", newUser.getEmail());
	}
	@Test
	public void Test_04_getUsersSortByName() {
		Sort sort = new Sort(Direction.ASC, "name");
		List<User> users = userRepository.findAll(sort);
		assertNotNull(users);
	}
	@Test
	public void Test_05_getUsersSortByNameAscAndIdDesc() {
		Order order1 = new Order(Direction.ASC, "name");
		Order order2 = new Order(Direction.DESC, "id");
		Sort sort = Sort.by(order1, order2);
		List<User> users = userRepository.findAll(sort);
		assertNotNull(users);
	}
	@Test
	public void Test_06_getUsersByPage() {
		Sort sort = new Sort(Direction.ASC, "name");
		int size = 25;
		int page = 0; //zero-based page index.
		Pageable pageable = PageRequest.of(page, size, sort);
		Page<User> usersPage = userRepository.findAll(pageable);
		System.out.println(
				"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Stampa in console %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
		);
		System.out.println(usersPage.getTotalElements()); //Returns the total amount of elements.
		System.out.println(usersPage.getTotalPages());//Returns the number of total pages.
		System.out.println(usersPage.hasNext());
		System.out.println(usersPage.hasPrevious());
		List<User> usersList = usersPage.getContent();
		System.out.println(
				"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Fine %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
		);
		assertNotNull(usersList);
	}
}
data.sql
insert into users(id, name, email,disabled) values(1,'John','john@gmail.com', false);
insert into users(id, name, email,disabled) values(2,'Rod','rod@gmail.com', false);
insert into users(id, name, email,disabled) values(3,'Becky','becky@gmail.com', true);
In precedenza hai scritto che se si sceglie AUTO l'ORM (Hibernate) sceglie la strategia di generazione del ID che risulta meglio per il dialect selezionato. Stando a questo livello generico, non si può controllare come viene definita la colonna id. Questo è il punto che proprio non comprendo. Anche se non si riesce a controllare come viene definita la colonna id l'ORM dovrebbe sapere quale approccio ha usato e usare sempre quello, sia per caricare data.sql, sia per caricare il nuovo record relativo a "Paul"!
User.java:
package com.apress.demo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="USERS")
public class User
{
	@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	@Column(nullable=false, length=191)
	private String name;
	@Column(nullable=false, unique=true, length=191)
	private String email;
	private boolean disabled;
	public User()
	{
	}
	public User(Integer id, String name, String email)
	{
		this.id = id;
		this.name = name;
		this.email = email;
	}
	public User(Integer id, String name, String email, boolean disabled)
	{
		this.id = id;
		this.name = name;
		this.email = email;
		this.disabled = disabled;
	}
	public Integer getId()
	{
		return id;
	}
	public void setId(Integer id)
	{
		this.id = id;
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public String getEmail()
	{
		return email;
	}
	public void setEmail(String email)
	{
		this.email = email;
	}
	public boolean isDisabled()
	{
		return disabled;
	}
	public void setDisabled(boolean disabled)
	{
		this.disabled = disabled;
	}
	
}
Grazie anticipatamente