Lazy -Run Few queries
Eager -Run Many queries
Eager -Run Many queries
ackage com.sb.basic.model;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.springframework.data.jpa.domain.AbstractPersistable;
@Entity
public class Address extends AbstractPersistable<Long>{
private String city;
private String state;
private String country;
//We will create one transition for userId
private transient Long userId;
@ManyToOne(targetEntity = User.class)
@JoinColumn(name="user_id") //user_id is table column name
private User user;
//Getter Setter
private String userId;
private String userName;
private String password;
@OneToMany(targetEntity = Address.class, mappedBy = "user",fetch= FetchType.LAZY ,cascade =CascadeType.ALL)
private Set<Address> address; //Instead of Set(Unordered collection and not allow duplicates) we can use list(ordered and allow duplicate values) as well
Getter and setter;
This like normal UserController class
@RequestMapping("/addressList")
public List<Address> addressList(){
return addressService.addressList();
}
This also like UserSImpl class
@Override
public List<Address> addressList() {
return addressRepository.findAll();
}
package com.sb.basic.service;
import java.util.List;
import java.util.Optional;
import com.sb.basic.model.User;
public interface UserService {
List<User> userList();
Optional<User> findOne(Long id);
User addUser(User user);
String deleteUser(Long id);
}
package com.sb.basic.service.impl;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sb.basic.model.User;
import com.sb.basic.repository.UserRepository;
import com.sb.basic.service.UserService;
@Service
public class UserServiceImpl implements UserService {
private UserRepository userRepository;
//Better go with constructor injection so no need to put @Autwired again and again
@Autowired
public UserServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public List<User> userList() {
return userRepository.findAll();
}
@Override
public Optional<User> findOne(Long id) {
return userRepository.findById(id);
}
@Override
public User addUser(User user) {
return userRepository.save(user);
}
@Override
public String deleteUser(Long id) {
userRepository.deleteById(id);;
return "{'message':'User Deleted'}";
}
}
package com.sb.basic.coontroller;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sb.basic.model.User;
import com.sb.basic.service.UserService;
@RestController
@RequestMapping("/user")
public class UserController {
private UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@RequestMapping("/list")
public List<User> userList(){
return userService.userList();
}
@RequestMapping("/id/{id}")
public Optional<User> findOne(@PathVariable Long id){
return userService.findOne(id);
}
@RequestMapping("/add")
public User addUser(@RequestBody User user){
return userService.addUser(user);
}
@RequestMapping("/delete/{id}")
public String deleteUser(@PathVariable Long id){
return userService.deleteUser(id);
}
}
#Hibernate
spring.datasource.url= jdbc:mysql://localhost:3306/springboot
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username= root
spring.datasource.password= root
#JPA
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency>
package com.sb.test.model;
import javax.persistence.Entity;
import org.springframework.data.jpa.domain.AbstractPersistable;
@Entity
public class User extends AbstractPersistable<Long>{
//Don't need to primary key and column annotation.
private String userId;
private String userName;
private String password;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.sb.test.model;
package com.sb.basic.coontroller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sb.basic.model.User;
import com.sb.basic.service.UserService;
@RestController
@RequestMapping("/user")
public class UserController {
private UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@RequestMapping("/list")
public List<User> userList(){
return userService.userList();
}
}
package com.sb.basic.service;
import java.util.List;
import com.sb.basic.model.User;
public interface UserService {
List<User> userList();
}
package com.sb.basic.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sb.basic.model.User;
import com.sb.basic.repository.UserRepository;
import com.sb.basic.service.UserService;
@Service
public class UserServiceImpl implements UserService {
private UserRepository userRepository;
//Better go with constructor injection so no need to put @Autwired again and again
@Autowired
public UserServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public List<User> userList() {
return userRepository.findAll();
}
}
package com.sb.basic.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.sb.basic.model.User;
public interface UserRepository extends JpaRepository<User, Long>{
}
package com.sb.basic.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sb.basic.model.User;
import com.sb.basic.repository.UserRepository;
import com.sb.basic.service.UserService;
@Service
public class UserServiceImpl implements UserService {
private UserRepository userRepository;
//Better go with constructor injection so no need to put @Autwired again and again
@Autowired
public UserServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public List<User> userList() {
return userRepository.findAll();
}
}
package com.sb.basic.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sb.basic.model.User;
import com.sb.basic.repository.UserRepository;
import com.sb.basic.service.UserService;
@Service
public class UserServiceImpl implements UserService {
private UserRepository userRepository;
//Better go with constructor injection so no need to put @Autwired again and again
@Autowired
public UserServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public List<User> userList() {
return userRepository.findAll();
}
}
Now URL : localhost:9090/springbootserver.port = 9090server.context-path = /springboot
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope></dependency>
package com.sbbasiccourse.controller;Now run and call URL - localhost:9090/springboot@RestControllerpublic class WelcomeController {@RequestMapping(name="/")public String index(){return "Spring Boot Basic Course";}}
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
management.security.enabled=false
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"topic": "SsS",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
Create WelcomController classpackage com.example.demo.pojo; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController public class WelcomeController { @RequestMapping(value = "/jsontopojo", method = RequestMethod.POST ) public @ResponseBody Example value(@RequestBody Example example){ return example; } }
package com.example.demo.pojo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WelcomeController {
@RequestMapping(value = "/jsontopojo", method = RequestMethod.POST )
public @ResponseBody String value(@RequestBody Example example){
return example.getGlossary().getTitle();
}
}
{
"car": {
"title": "example glossary",
"bmw": {
"title": "S",
"topic": "SsS"
}
}
}
package com.example.demo.pojo;
import com.fasterxml.jackson.annotation.JsonProperty;
public class JsontoPojo {
@JsonProperty("car")
private Car car;
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
}
package com.example.demo.pojo;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Car {
@JsonProperty("title")
private String title;
@JsonProperty("bmw")
private BMW bmw ;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public BMW getBmw() {
return bmw;
}
public void setBmw(BMW bmw) {
this.bmw = bmw;
}
}
package com.example.demo.pojo;
import com.fasterxml.jackson.annotation.JsonProperty;
public class BMW {
@JsonProperty("title")
private String title;
@JsonProperty("topic")
private String topic;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
}
package com.example.demo.pojo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WelcomeController {
@RequestMapping(value="/car" , method = RequestMethod.POST)
public @ResponseBody JsontoPojo type(@RequestBody JsontoPojo jsontoPojo){
return jsontoPojo;
}
}
Exception in thread "main" java.io.InvalidClassException:
SerializeMe; local class incompatible: stream classdesc
: