Skip to content

Instantly share code, notes, and snippets.

@gokman
gokman / Spring - Ignore null value in json
Created February 1, 2017 07:38
Spring Tutorial - Ignore Null Value In Json
@JsonInclude(JsonInclude.Include.NON_NULL)
public class TestJson implements Serializable{
...
...
...
}
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class UserRepositoryTests {
@Autowired
private TestEntityManager entityManager;
@Autowired
private UserRepository repository;
@gokman
gokman / Spring data jpa primary key with sequence in postgresql
Created February 3, 2017 11:16
Spring data jpa primary key with sequence in postgresql
//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;
}
@gokman
gokman / Spring Jpa Trigger
Created February 3, 2017 11:19
Spring Jpa Trigger
@Entity
public class User{
.
.
.
@PreUpdate
public void preUpdate(){
System.out.println("pre update executed");
@gokman
gokman / Spring Boot Controller Unit Test
Created February 7, 2017 06:12
Spring Boot Controller Unit Test
@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
@gokman
gokman / Spring Jpa @Where annotation
Created February 8, 2017 06:55
Spring Jpa @where annotation
@Entity
@Where(clause = "is_deleted=0")
public class Customer extends AbstractEntity implements Serializable{
@gokman
gokman / Spring Jpa Query Result To Pojo
Created February 13, 2017 07:57
Spring Jpa - Query Result To Pojo
//first prepare pojo for query
@JsonInclude(JsonInclude.Include.NON_NULL)
public class UserResponseVM implements Serializable{
@JsonProperty("_id")
private UUID id;
private String name;
.
@gokman
gokman / Angularjs - Custom Header Read Problem on Angularjs
Created February 15, 2017 12:37
Angularjs - Custom Header Read Problem on Angularjs
@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
@gokman
gokman / Spring JPA - non-primary auto generated field mapping
Created February 27, 2017 08:37
Spring JPA - non-primary auto generated field mapping
//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
@gokman
gokman / spring jpa - generated non-primary value become zero when update
Created March 2, 2017 06:35
spring jpa - generated non-primary value become zero when update
//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;
}