Jerry's Blog

Recording what I learned everyday

View on GitHub


5 July 2019

Spring Tips

by Jerry Zhang

Day 29:

@RequestBody

If we want to accept an JSON object as a parameter in a controller method, we need to add @RequestBody before the parameter.

@PostMapping(value = "/create")
public CommonReturnType createContact(@RequestBody ContactModel contactModel) {
    ContactModel contact = contactService.createContact(contactModel);
    return CommonReturnType.create(contact);
}

Preventing JPA generating foreign keys

@ManyToOne
@JoinColumn(name="account_id",foreignKey = @ForeignKey(name="none",value= ConstraintMode.NO_CONSTRAINT))
private Account account;

Put getters and setters for both model and data objects

To make the Beans.util method work, we need getters and setters.

Auto generated primary key

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer productId;

Hibernate auto update

spring.jpa.hibernate.ddl-auto=update

Hibernate Dynamic Insert

@Entity
@DynamicInsert
public class Account {}

Never put a service in a component provider! Always put it in the app.module.ts!

Never put a service in a component provider! Always put it in the app.module.ts!

Never put a service in a component provider! Always put it in the app.module.ts!

tags: Spring, - JPA