通过JPA更新实体

时间:2015-05-30 16:30:37

标签: java hibernate jpa spring-boot

我正在做一个自我教育项目,当我尝试更新绑定到另一个实体的实体时,我遇到了问题。 我使用的是Spring Boot 1.2.3.RELEASE + jpa。

@Component
@Entity
@Table(name = "Person_sql", uniqueConstraints = 
       @UniqueConstraint(columnNames = {"name", "passport"}))

public class Person implements AcademyEntity {

@Id
@GeneratedValue
private long id;

private String name;
private Date birthday;
@Column(unique=true)
private String passport;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "person_id")
private Set<Teacher> teacherSet;

public Person() {
    this.birthday = new Date();
    studentSet = new HashSet<Student>();
    teacherSet = new HashSet<Teacher>();
}

public Person(String name, Date birthday, String passport) {
    this.name = name;
    this.birthday = birthday;
    this.passport = passport;
    studentSet = new HashSet<Student>();
    teacherSet = new HashSet<Teacher>();
}

getters, setters, equals, hashCode, toString
}

教师

@Component
@Entity
@Table(name = "Teacher_sql")

public class Teacher implements AcademyEntity {

@Id
@GeneratedValue
private long id;
private Date start;
private Date finish;

@ManyToOne
@JoinColumn(nullable = false)
private Person person;

... constructors, getters, setters, equals, hashCode, toString
}

JPAPersonDAO

public interface JPAPersonDAO extends JpaRepository<Person, Long> {
}

JPAPersonService

@Service
public class JPAPersonService implements PersonService {
private JPAPersonDAO jpaPersonDAO;
...
@Override
public boolean saveService(Object object) {
    if (object instanceof Person) {
        try {
            if (validatePerson((Person) object)) {
                jpaPersonDAO.save((Person) object);
                return true;
            }
        } catch (RuntimeException e) {
            log.error(e);
        }
    }
    return false;
}
...

当我更新不是老师的人时 - 一切都很好。如果我触摸一个老师的人 - 我会收到这个错误:

Person person = new Person();
person.setId(..);
jpaPersonService.saveService(person);
...
WARN 6308 --- [nio-8181-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 23502, SQLState: 23502
ERROR 6308 --- [nio-8181-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper   : NULL not allowed for column "PERSON_ID"; SQL statement
NULL not allowed for column "PERSON_ID"; SQL statement: update teacher_sql set person_id=null where person_id=? [23502-185]
INFO 6308 --- [nio-8181-exec-6] o.h.e.j.b.internal.AbstractBatchImpl     : HHH000010: On release of batch it still contained JDBC statements

我认为我可能会在这里使用交易(@Transactional),但我不明白'怎么做'。
谢谢你的帮助!

修改
如果我使用纯hibernate(没有spring和jpa)和相同的实体,它可以正常工作。

public class HibernatePersonDAOImpl implements PersonDAO {
...
private boolean saveOrDelete(Person p, boolean delete) {
    Session session = hibernateUtils.getSession();
    try {
        Transaction t = session.beginTransaction();
        if (delete) {
            session.delete(p);
        } else {
            session.merge(p);
        }
        t.commit();
    } catch (HibernateException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

3 个答案:

答案 0 :(得分:1)

错误表明Teacher没有设置Person。在级联时,您需要显式管理层次结构,因此以下内容应该起作用:

Person person = new Person();
Teacher teacher = new Teacher();
person.addTeacher(teacher);
teacher.setPerson(person);
personService.saveService(person);

答案 1 :(得分:1)

我相信你错过了关系中的mappedBy参数。

@OneToMany(cascade = CascadeType.ALL, mappedBy="person")
private Set<Teacher> teacherSet;

答案 2 :(得分:0)

好吧,

ERROR 6308 --- [nio-8181-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper : NULL not allowed for column "PERSON_ID"; SQL statement NULL not allowed for column "PERSON_ID"; SQL statement: update teacher_sql set person_id=null where person_id=? [23502-185]

错误非常明确,即PERSON_ID为空。

你有id的实体人的构造函数吗?