Привет, у меня есть нормальное рабочее приложение, которое правильно вставляет / удаляет / обновляет. Однако я пытаюсь реализовать получение строки json из ajax и вставку ее в базу данных (PostgreSQL 10.12)
Первые 2–5 записей вставляются правильно, однако затем приложение начинает обновлять записи вместо вставки первичные ключи выходят из строя.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dashboard</groupId>
<artifactId>sp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spring </name>
<description>Spring boot </description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.13.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import java.io.Serializable;
@SuppressWarnings("hiding")
@MappedSuperclass
public abstract class AbstractModel<Long extends Serializable> implements Serializable {
private static final long serialVersionUID = -6323358535657100144L;
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Long id;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AbstractModel<?> other = (AbstractModel<?>) obj;
if (id == null) {
return other.id == null;
} else return id.equals(other.id);
}
}
import lombok.*;
import javax.persistence.Column;
import javax.persistence.Entity;
@Entity
@Data
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class PurchaseOrderProducts extends AbstractModel<Long> {
/**
*
*/
private static final long serialVersionUID = -5927643103467927664L;
@Column(nullable = false)
private Integer productcode;
@Column(nullable = false)
private String manufacturer;
@Column(nullable = false)
private String model;
@Column(nullable = false)
private String supplier;
@Column(nullable = false)
private String supplierproductcode;
@Column(nullable = false)
private Integer orderno;
public Integer getProductcode() {
return productcode;
}
public void setProductcode(Integer productcode) {
this.productcode = productcode;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getSupplier() {
return supplier;
}
public void setSupplier(String supplier) {
this.supplier = supplier;
}
public String getSupplierproductcode() {
return supplierproductcode;
}
public void setSupplierproductcode(String supplierproductcode) {
this.supplierproductcode = supplierproductcode;
}
public Integer getOrderno() {
return orderno;
}
public void setOrderno(Integer orderno) {
this.orderno = orderno;
}
@Override
public String toString() {
return "PurchaseOrderProducts [productcode=" + productcode + ", manufacturer=" + manufacturer + ", model="
+ model + ", supplier=" + supplier + ", supplierproductcode=" + supplierproductcode + ", orderno="
+ orderno + "]";
}
}
@Service
public class PurchaseOrdersProductsService extends AbstractService<PurchaseOrderProducts, Long> {
@Autowired
private PurchaseOrdersProductsRepository purchaseOrdersProductsRepository;
@Override
protected JpaRepository<PurchaseOrderProducts, Long> getRepository() {
return purchaseOrdersProductsRepository;
}
public List<PurchaseOrderProducts> getAllPurchaseOrdersProducts() {
return getRepository().findAll();
}
@Transactional
public boolean checkIfExists(Integer productcode, Integer orderno) {
boolean val = false;
Integer test = purchaseOrdersProductsRepository.checkIfExists(productcode,orderno);
if(test == 0) {
val = false;
} else {
val = true;
}
return val;
}
public void saveAll(List<PurchaseOrderProducts> purchaseOrderProducts) {
purchaseOrdersProductsRepository.saveAll(purchaseOrderProducts);
}
}
@Repository
public interface PurchaseOrdersProductsRepository extends JpaRepository<PurchaseOrderProducts, Long> {
@Query(value = "SELECT count(productcode) FROM purchase_order_products m WHERE m.productcode = ?1 AND m.orderno=?2" , nativeQuery=true)
Integer checkIfExists(long productcode, long orderno);
}
@PostMapping(value = "/submitpoproducts", consumes = "application/json", produces = "application/json")
@ResponseBody
public List<String> savePurchaseOrderProductList(@RequestBody PurchaseOrderProducts[] purchaseOrderProducts) {
List<String> response = new ArrayList<String>();
for (int i = 0; i < purchaseOrderProducts.length; i++) {
// if (purchaseOrderProducts[i].getProductcode() != null) {
// if
// (purchaseOrdersProductsService.checkIfExists(purchaseOrderProducts[i].getProductcode(),
// purchaseOrderProducts[i].getOrderno()) == false) {
purchaseOrdersProductsService.save(purchaseOrderProducts[i]);
System.out.println(purchaseOrderProducts[i].toString());
// }
// }
// response.add("Saved product: " + purchaseOrderProducts[i]);
}
return response;
}
PurchaseOrderProducts [productcode=10000004, manufacturer=FS, model=CL 20cl, supplier=Fs, supplierproductcode=FAR001, orderno=10000003]
id |
----+
1 |
2 |
3 |
4 |
id |
----+--------------
1 |
3 |
4 |
2 |
5 |