我正在尝试实现 classes 表,以便 classname 文本字段对于每个 termid 必须是唯一的 . 文本字段不区分大小写( utf8_general_ci ) .

MySQL的SQL脚本部分:

CREATE TABLE classes (
    ID INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
    classname VARCHAR(20) NOT NULL,
    classcode VARCHAR(20),
    termid INT(11) NOT NULL DEFAULT 0,  
    CONSTRAINT fk_classes_term
        FOREIGN KEY (termid) REFERENCES terms (id)
        ON DELETE RESTRICT
        ON UPDATE RESTRICT,
    CONSTRAINT uc_classnames UNIQUE (termid, classname)
);

存储库层负责更新 . 我已经尝试过,无异常处理 .

public void updateClass(ClassEntity classEntity)
{
    entityManager.persist(classEntity);
    entityManager.flush();
}

我发现一旦发现冲突就会抛出异常,即 classname 发生了变化,因此它与具有相同 termid 的现有实体使用的异常相同 .

当我改变 classname 中的一个字母的情况时,我一直遇到这样的错误 .

CreateEditClassForm.onSubmit失败,但有异常 . org.springframework.transaction.TransactionSystemException:无法提交JPA事务;嵌套异常是javax.persistence.RollbackException:标记为rollbackOnly的事务

我希望得到像 javax.persistence.EntityExistsException 这样的东西 .

SQL脚本有问题,还是数据库配置有问题?

用户界面使用Java Wicket编码,更新时不必检查数据库 .

编辑:使用一个(23个)方法添加ClassService .

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

// project imports omitted

@Service
@Transactional
public class ClassService
{
    @Autowired
    private ClassRepository classRepository;

    @Autowired
    private ClassUsersRepository classUsersRepository;

    @Autowired
    private TermRepository termRepository;

    @Autowired
    private TermService termService;

    public void updateClass(ClassEntity classEntity)
    {
        classRepository.updateClass(classEntity);
    }
}