This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@JsonInclude(JsonInclude.Include.NON_NULL) | |
public class TestJson implements Serializable{ | |
... | |
... | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(SpringRunner.class) | |
@DataJpaTest | |
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) | |
public class UserRepositoryTests { | |
@Autowired | |
private TestEntityManager entityManager; | |
@Autowired | |
private UserRepository repository; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//user_id_seq is the sequence that defined in posgresql database for id column | |
@Id | |
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_id_seq") | |
@SequenceGenerator(name = "user_id_seq", sequenceName = "user_id_seq") | |
@Column(name = "id") | |
public int getId() { | |
return id; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class User{ | |
. | |
. | |
. | |
@PreUpdate | |
public void preUpdate(){ | |
System.out.println("pre update executed"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SpringBootTest(classes = XXXApplication.class) //if we change Application.class we must declare here what the application main class is | |
@RunWith(SpringRunner.class) | |
public class UserControllerTests { | |
private MockMvc mockMvc; | |
@Autowired | |
WebApplicationContext webApplicationContext; | |
@Before |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
@Where(clause = "is_deleted=0") | |
public class Customer extends AbstractEntity implements Serializable{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//first prepare pojo for query | |
@JsonInclude(JsonInclude.Include.NON_NULL) | |
public class UserResponseVM implements Serializable{ | |
@JsonProperty("_id") | |
private UUID id; | |
private String name; | |
. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@EnableWebMvc | |
public class WebConfig extends WebMvcConfigurerAdapter{ | |
@Override | |
public void addCorsMappings(CorsRegistry registry) { | |
registry.addMapping("/**") | |
.allowedOrigins("*") //access from all origin | |
.exposedHeaders("Authorization"); //for reading Authorization header |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//If you have auto generated field and it is not primary key. So you need below mapping for that column: | |
@Basic | |
@Column(name = "id", insertable = false) | |
public long getId() { | |
return id; | |
} | |
//on above the important point is "insertable=false" statement | |
//if you use insertable=false db will generate and insert on its own to that column |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//to accomplish this problem we need to add "updatable=false" to that column | |
//because without this definition when we need update a row automatically hibernate assign default value 0 (zero) | |
@Basic | |
@GeneratedValue | |
@Column(name = "id", insertable = false, updatable = false) | |
public long getId() { | |
return id; | |
} |
OlderNewer