在将实体保存到RDS
上的数据库时遇到了问题。这个问题超出了我的理解范围,因为当我在计算机上执行应用程序时, save
可以正常工作。即来自Intellij(不知道提到Intellij是否值得)。
这是实体类
@Entity
@Table(name = "spr_material")
public class SprMaterial {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@CreationTimestamp
@Column(name = "created_at", nullable = false, updatable = false)
public Timestamp createdAt;
@Column(name = "service_name")
public String serviceName;
@UpdateTimestamp
@Column(name = "updated_at")
public Timestamp updatedAt;
@Column(name = "isDeleted")
private Boolean isDeleted;
@Transient
private int serialNo;
@ManyToOne()
@JoinColumn(name = "dep_id", nullable = true)
private Department department;
@ManyToOne()
@JoinColumn(name = "item_code", nullable = true)
private StockRawMaterial rawMaterial;
// removing get/set methods
@Override
public String toString() {
return "ID: "+this.id+ " Created At: "+this.createdAt+ " Service Name: "+this.serviceName+ " Deleted: "+this.isDeleted
+ " Department Id: "+this.department.getDepId()+ " Raw Material Id: "+this.rawMaterial.getRawMaterialId();
}
}
这是将数据保存到数据库的代码块
在Controller
类中
@PostMapping("/spr/save")
public String sprMaterial_save(@ModelAttribute("sprForm") SprForm sprForm, RedirectAttributes redirectAttributes) {
SprMaterial sprMaterial = new SprMaterial();
sprMaterial.setId(sprMaterialService.getMaxId());
sprMaterial.setServiceName(sprForm.getServiceName());
sprMaterial.setRawMaterial(rawMaterialService.findById(sprForm.getItemCode()));
sprMaterial.setDepartment(departmentService.findByDeptId(sprForm.getDepartmentId()));
//sprMaterial.setDeleted(false);
logger.info("TESTING");
SprMaterial saved = sprMaterialService.save(sprMaterial);
if(saved == null){
System.out.println("Could not saved object");
}else {
System.out.println("Saved Successful");
}
redirectAttributes.addFlashAttribute("addedSuccess", "success");
return "redirect:/spr/accounts";
}
在服务舱中
@Override
public SprMaterial save(SprMaterial sprMaterial) {
logger.info("SAVING SPR MATERIAL: "+sprMaterial.toString());
SprMaterial rm = sprRepository.save(sprMaterial);
logger.info("after saving: "+rm.toString());
return rm;
}
更新:我正在使用jpa默认的save
方法。这是存储库类。
public interface SprRepository extends JpaRepository<SprMaterial, Integer> {
@Query(value = "SELECT spr.* FROM spr_material spr JOIN department d ON d.dep_id = spr.dep_id WHERE d.name LIKE ?1 AND spr.isDeleted = FALSE ORDER BY ?#{#pageable}",
countQuery = "SELECT spr.* FROM spr_material spr JOIN department d ON d.dep_id = spr.dep_id WHERE d.name LIKE ?1 AND spr.isDeleted = FALSE ORDER BY ?#{#pageable}", nativeQuery = true)
Page<SprMaterial> findSprMaterialById(String name, Pageable pageable);
@Modifying
@Transactional
@Query(value = "UPDATE spr_material spr SET spr.`isDeleted` = 1 WHERE spr.`id` = ?1", nativeQuery = true)
void deleteSprMaterial(Integer sprMaterialId);
SprMaterial findById(Integer sprMaterialId);
@Query(value = "SELECT MAX(spr.`id`) + 1 FROM spr_material spr",nativeQuery = true)
Integer getMaxId();
@Query(value = "select * from spr_material spr where spr.`dep_id`=?1",nativeQuery = true)
SprMaterial findByDepId(Integer deptId);
}
在tomcat catalina.out
上,我得到以下日志,并得到一个页面,该页面的错误为504
14-Jun-2019 11:58:45.363 INFO [http-nio-80-exec-34] com.qsa.account.web.SprMaterialController.sprMaterial_save TESTING
14-Jun-2019 11:58:45.374 INFO [http-nio-80-exec-34] com.qsa.account.service.sprMaterial.SprMaterialServiceImpl.save SAVING SPR MATERIAL: ID: 2 Created At: null Service Name: Vilcor Disc Repair and Maintenance Deleted: false Department Id: 7 Raw Material Id: 5350
Hibernate: select sprmateria0_.id as id1_53_0_, sprmateria0_.created_at as created_2_53_0_, sprmateria0_.dep_id as dep_id6_53_0_, sprmateria0_.isDeleted as isDelete3_53_0_, sprmateria0_.item_code as item_cod7_53_0_, sprmateria0_.service_name as service_4_53_0_, sprmateria0_.updated_at as updated_5_53_0_ from spr_material sprmateria0_ where sprmateria0_.id=?
11:58:45,384 TRACE BasicBinder:81 - binding parameter [1] as [INTEGER] - [2]
11:58:45,413 WARN PageNotFound:215 - Request method 'POST' not supported