人类
package com.example.springjpa.demojpa.entity;
import java.sql.Timestamp;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Person {
@Id
@GeneratedValue
private int id;
private String name;
private String location;
private Date birth_date;
public Person() {};
public Person(String name, String location, Date birth_date) {
super();
this.name = name;
this.location = location;
this.birth_date = birth_date;
}
public Person(int id, String name, String location, Date birth_date) {
super();
this.id = id;
this.name = name;
this.location = location;
this.birth_date = birth_date;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Date getBirth_date() {
return birth_date;
}
public void setBirth_date(Timestamp birth_date) {
this.birth_date = birth_date;
}
@Override
public String toString() {
return "\n Person [name=" + name + ", location=" + location + ", birth_date=" + birth_date + "]";
}
}
PersonJpaDao班
package com.example.springjpa.demojpa;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import org.springframework.stereotype.Repository;
import com.example.springjpa.demojpa.entity.Person;
@Repository
@Transactional
public class PersonJpaDao {
@PersistenceContext
EntityManager em;
public Person findById(int id) {
return em.find(Person.class, id);
}
public Person insert(Person person){
return em.merge(person);
}
public Person update(Person person){
return em.merge(person);
}
}
主类
package com.example.springjpa.demojpa;
import java.util.Date;
import javax.transaction.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.example.springjpa.demojpa.entity.Person;
@SpringBootApplication
public class DemoJpaApplication implements CommandLineRunner {
private Logger logger = LoggerFactory.getLogger(this.getClass());
public static void main(String[] args) {
SpringApplication.run(DemoJpaApplication.class, args);
}
@Autowired
PersonJpaDao personJpaDao;
@Override
public void run(String... args) throws Exception {
logger.info("Person yBy Id {}", personJpaDao.findById(1));
logger.info("Update 10003 -> {}",
personJpaDao.update(new Person("Pieter", "Utrecht", new Date())));
}
}
当我运行main函数时,find和update工作正常,但是对于与update有相同代码的insert操作,抛出异常如下:
引起:org.springframework.dao.DataIntegrityViolationException:无法执行语句; SQL [不适用];约束[“PRUBLARY KEY ON PUBLIC.PERSON(ID)”; SQL语句: 插入人(birth_date,location,name,id)值(?,?,?,?)[23505-196]];嵌套异常是org.hibernate.exception.ConstraintViolationException:无法执行语句
引起:org.hibernate.exception.ConstraintViolationException:无法执行语句
答案 0 :(得分:0)
我遇到了同样的问题,但是我发现它与主键冲突,就像某些数据已经存在(主键20)并且实体管理器也存在 将其设置为相同的主键(20),因为您正在使用merge方法来更新和创建这两者,因此,这会导致先前存在的和通过merge方法尝试在构造函数中提供id或删除data < / p>