I have created a simple table with two fields and called simple CRUD operation to view the data.
Technolgy: Springboot, hibernate(JPA), MySQL
table name = cluster
CREATE DATABASE IF NOT EXISTS `myDb`;
USE `myDb`;
DROP TABLE cluster;
CREATE TABLE IF NOT EXISTS `cluster` (
`
clusterId` bigint(5) NOT NULL AUTO_INCREMENT,
`name` varchar(200) NOT NULL,
PRIMARY KEY (`clusterId`)
) ENGINE=InnoDB;
INSERT INTO `cluster` (`clusterId`, `name`) VALUES
(1, 'C1'),
(2, 'C2'),
(3, 'C3');
but when i call rest
http://localhost:8888/entity/clusters
this creating another field in the table with name "
cluster_id"
Reason:
hibernate , Springboot provides the default naming strategy as the "ImprovedNamingStrategy"
https://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/cfg/ImprovedNamingStrategy.html
This creating names according to the built in strategy
eg:
clusterId --> cluster_id
Cluster --> cluster
Solution:
change naming strategy
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
For Information below is the complete code
--------------
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;
@RestController
@RequestMapping("entity")
public class ClusterController {
@Autowired
private ClusterServiceImpl clusterService;
@GetMapping("clusters")
public ResponseEntity
> getClusterList() {
List
clusList = clusterService.getClusterList();
return new ResponseEntity
>(clusList, HttpStatus.OK);
}
@GetMapping("cluster/{id}")
public ResponseEntity
getClusterById(@PathVariable("id") Integer id) {
Cluster cluster = clusterService.getClusterById(id);
return new ResponseEntity
(cluster, HttpStatus.OK);
}
@PostMapping("cluster")
public ResponseEntity
createCluster(@RequestBody Cluster cluster, UriComponentsBuilder builder) {
boolean flag = clusterService.addCluster(cluster);
if (!flag) {
return new ResponseEntity
(HttpStatus.CONFLICT);
}
HttpHeaders headers = new HttpHeaders();
headers.setLocation(builder.path("/cluster/{id}").buildAndExpand(cluster.getId()).toUri());
return new ResponseEntity
(headers, HttpStatus.CREATED);
}
@PutMapping("cluster")
public ResponseEntity
updateCluster(@RequestBody Cluster cluster) {
clusterService.updateCluster(cluster);
return new ResponseEntity
(cluster, HttpStatus.OK);
}
@DeleteMapping("cluster/{id}")
public ResponseEntity
deleteCluster(@PathVariable("id") Integer id) {
clusterService.deleteCluster(id);
return new ResponseEntity
(HttpStatus.NO_CONTENT);
}
}
----------------
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 = "cluster")
public class Cluster {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "clusterId")
private long id;
@Column(name = "name")
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
------------------------
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ClusterServiceImpl implements ClusterService {
@Autowired
private ClusterRepository clusterRepository;
@Override
public List
getClusterList() {
List
clusterList = new ArrayList<>();
clusterRepository.findAll().forEach(elment -> clusterList.add(elment));
return clusterList;
}
@Override
public Cluster getClusterById(long id) {
Cluster cluster = clusterRepository.findById(id).get();
return cluster;
}
@Override
public boolean addCluster(Cluster cluster) {
Cluster clus = clusterRepository.save(cluster);
return false;
}
@Override
public void updateCluster(Cluster cluster) {
clusterRepository.save(cluster);
}
@Override
public void deleteCluster(long clusterId) {
//
Cluster cluster = new Cluster();
//
cluster.setId(clusterId);
clusterRepository.delete(getClusterById(clusterId));
}
}
---------
import java.util.List;
import org.springframework.stereotype.Service;
@Service
public interface ClusterService {
List
getClusterList();
Cluster getClusterById(long id);
boolean addCluster(Cluster cluster);
void updateCluster(Cluster cluster);
void deleteCluster(long clusterId);
}
--------------------
import org.springframework.data.repository.CrudRepository;
public interface ClusterRepository extends CrudRepository {
}
-----------------
application.properties
spring.jpa.hibernate.ddl-auto=update
#spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.cfg.DefaultComponentSafeNamingStrategy
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.datasource.url=jdbc:mysql://localhost:3306/myDB
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.hikari.connection-timeout=20000
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=12
spring.datasource.hikari.idle-timeout=300000
spring.datasource.hikari.max-lifetime=1200000
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.id.new_generator_mappings = false
spring.jpa.properties.hibernate.format_sql = true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
server.port=8888
eureka.client.serviceUrl.defaultZone= http://localhost:1111/eureka/
eureka.instance.leaseRenewalIntervalInSeconds=5