【问题标题】:JPA unable to assign a new persisted entity in a many to one relationshipJPA 无法在多对一关系中分配新的持久实体
【发布时间】:2013-01-30 01:43:56
【问题描述】:

我必须以多对一的双向关系定义 JPA 实体,特此:

@Entity
public class Department implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name="DEPARTAMENTO_ID_GENERATOR",sequenceName="DEPARTAMENTO_SEQ")
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="DEPARTAMENTO_ID_GENERATOR")
    @Column(name="DEP_ID")
    private long id;

    @Column(name="DEP_DESC")
    private String desc;

    //bi-directional many-to-one association to Academico
    @OneToMany(mappedBy="department")
    private Set<Proffesor> proffesors;
//getters and setters
}

@Entity
@Table(name="ACADEMICOS")
public class Proffesor implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name="ACADEMICOS_ID_GENERATOR", sequenceName="ACADEMICOS_SEQ")
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="ACADEMICOS_ID_GENERATOR")
    @Column(name="ACD_ID")
    private long id;
@ManyToOne(cascade={CascadeType.PERSIST,CascadeType.MERGE})
    @JoinColumn(name="ACD_DEPADSCRITO_DEP")
    private Department department;
// getters and setters.
}

在事务性 Spring 服务中,我有下一个代码以这种方式操作实体。

@Transactional (propagation=Propagation.REQUIRED)
    public void createDepartmentWithExistentProffesor(String desc,Long idAvaiableProf) {
        // new department   
        Department dep = new Department();
        dep.setDesc(desc);
        HashSet<Proffesor> proffesors = new HashSet<Proffesor>();
        dep.setProffesors(proffesors);


// I obtain the correct attached Proffesor entity
        Proffesor proffesor=DAOQueryBasic.getProffesorById(idAvaiableProf);

// I asign the relationship beetwen proffesor and department in both directions
                dep.addProffesors(proffesor);
// Persists department      
        DAODataBasic.insertDepartment(dep);
// The id value is not correct then Exception ORA-0221
        System.out.println("SERVICIO: Departamento creado con id: " + dep.getId());

    }

正如我在 cmets 中所说的,持久化的新部门的 id 不是事务中的真实数据库 id,然后会产生异常

Exception in thread "main" org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
........

Caused by: java.sql.BatchUpdateException: ORA-02291: integrity restiction (HIBERNATE_PRB.FK_ACD2DEP) violated - primary key don't found

我在测试中尝试过,持久化与 Proffesor 没有关系的新部门实体,我发现新部门持久化实体的 id 在事务中没有有效值,但在事务之外已经id 具有正确的值。 但我需要交易中的正确值。

有人可以帮我吗? 提前谢谢你。

【问题讨论】:

    标签: spring hibernate jpa


    【解决方案1】:

    试试这个

    @OneToMany(mappedBy="department",cascade = CascadeType.PERSIST)
    private Set<Proffesor> proffesors;
    

    【讨论】:

    • 您是否尝试过合并教授而不是坚持新部门。我知道这没有逻辑意义。但值得一试。在 Proffesor proffesor=DAOQueryBasic.getProffesorById(idAvaiableProf); 之后执行此操作profesor.setDepartment(department); entityManager.merge(professor); 看看是否可行。
    • 就在阅读您的评论之前,我已经完成了,但还没有工作。再次感谢您的帮助。
    • 我们来看看。生成的 SQL 是正确的。从教授中选择,从系中选择(分配给教授),插入系并更新教授(分配新创建的系)。在这最后一句中发生了完整性约束违规,因为数据库中不存在新部门的 id。所以我认为级联策略是可以的,但是生成的 id 丢失了?在当前事务中。
    • 对不起,上面的代码是从西班牙语到英语的翻译,我犯了一个错误。教授是 2 s 而不是 2 f 如上所述。对不起,我的英语不好。
    • 这是我的错。当我以交互方式而不是 DDL 描述表时,Oracle 管理器控制台为插入操作创建了一个触发器,通过序列回答一个新的 Id。因此,未找到子表的主键。禁用或删除触发器它工作正常。 >谢谢你的帮助。
    猜你喜欢
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多