我想将数据添加到数据库,但是当我添加它们时,我有这个错误
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index, mtc_id, name, region_id) values (345, 234, 1, '123', 1)' at line 1
我通过JPA存储库添加数据
我的存储库
public interface StreetRepository extends JpaRepository<Street,Long> {
}
我的控制器
@RestController
@RequestMapping("/streets")
public class StreetController {
@Autowired
StreetRepository streetRepository;
@Autowired
RegionRepository regionRepository;
@Autowired
MTCRepository mtcRepository;
@RequestMapping("/get")
public List<Street> getStreets(){
return streetRepository.findAll();
}
@PostMapping("/insert")
public Street insertStreet(@RequestBody Street street) {
return streetRepository.save(street);
}
@RequestMapping("/update")
public Street updateStreet(@RequestBody Street street,@RequestParam("id") long id) {
street.setId(id);
return streetRepository.save(street);
}
@RequestMapping("/del")
public void delStreet(@RequestParam("id") int id){
streetRepository.deleteById((long)id);
}
}
我使用了角度js(1)从WEB
添加它我的剧本
this.insertToStreet = function add() {
var name = document.getElementById("StreetName").value;
var index = document.getElementById("StreetIndex").value;
var channels = document.getElementById("StreetChannels").value;
var indexRegion = document.getElementById("StreetRegion").selectedIndex;
var region_id =
document.getElementById("StreetRegion").options[indexRegion].value;
var indexMTC = document.getElementById("StreetMTC").selectedIndex;
var mtc_id =
document.getElementById("StreetMTC").options[indexMTC].value;
var req = {
method: 'POST',
url: '/streets/insert',
data: {
name: name,
region_id: region_id,
index: index,
channels: channels,
mtc_id: mtc_id
}
};
console.log(req);
$http(req).then(function (resp) {
console.log(resp);
window.location.reload();
})
};
我的模特
@Entity
@Table(name = "street")
@EntityListeners(AuditingEntityListener.class)
public class Street {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "name")
private String name;
@Column(name = "index")
private int index;
@Column(name = "channels")
private int channels;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "MTC_id",insertable = false,updatable = false)
private Mtc mtc;
@JsonIgnore
@OneToMany(mappedBy = "street", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Phonenumber> phonenumbers;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "Region_id",insertable = false,updatable = false)
private Region region;
@Column(name = "region_id")
private long region_id;
@Column(name = "mtc_id")
private long mtc_id;
public Street(String name, Region region, long region_id, long mtc_id, int index, int channels, Mtc mtc, List<Phonenumber> phonenumbers) {
this.name = name;
this.region = region;
this.region_id = region_id;
this.mtc_id = mtc_id;
this.index = index;
this.channels = channels;
this.mtc = mtc;
this.phonenumbers = phonenumbers;
}
//seters getters and empty constructor
}
所有的设定者和吸气者都存在
来自调试(控制器)的屏幕
return streetRepository.save(street); //on this line
(抱歉链接,在我的声誉低于10之前无法添加图片)
点击here
Github的here