【问题标题】:update one table in @onetoone mapping in hibernate在休眠中更新@onetoone 映射中的一张表
【发布时间】:2016-09-11 00:23:04
【问题描述】:

jpa 类是这样的:

public class JpaAlertConfiguration{
...
   @OneToOne(fetch = FetchType.EAGER)
    @Fetch(FetchMode.JOIN)
    @JoinColumn(name = "alert_type_id", nullable = false, referencedColumnName = "id", insertable = false, updatable = false)
    private JpaAlertType         alertType;
}

从这个类方法调用创建实体。

@Override
@Transactional
public JpaAlertConfiguration createEntity(JpaAlertConfiguration entity) throws Exception
{
    getAuthorizer().checkCreate(ENTITY_NAME);
    validateFields(entity);

    getAuthorizer().checkAccountAccess(entity.getAccount().getId(), null);
    entity = attachAssociatedEntities(entity);
    // entity.setStatusColumns(new StatusColumns().setStatus(ResourceStatus.Disabled));
    entity = super.createEntity(entity);
    entity = transformResult(entity);
    return entity;
}

我确保在调用此行之前,entity = super.createEntity(entity); 填充了 JpaAlertType id 值,它只是 alertConfig 表的“alert_type_id”。

我遇到了异常:

原因:org.postgresql.util.PSQLException:错误:“alert_type_id”列中的空值违反非空约束 详细信息:失败行包含 (3, Speed Alert, Speeding Alert, 5, t, null, Enabled, 0, 2016-09-06 17:56:30.556+05:30, 2016-09-06 17:56:30.58+ 05:30, 2016-09-06 17:56:30.556+05:30, null, f, f)。

请推荐

【问题讨论】:

  • JpaAlertType 是什么,entity.alertType 的 vakue 是什么?

标签: java postgresql hibernate jpa


【解决方案1】:

我认为您缺少 JPA 持久订单,请参阅下面有关 JPA one-to-one insert 的链接,我希望这能有所帮助。

【讨论】:

  • 谢谢卡马尔。在发布我的问题之前我检查了那个链接。真的没有帮助。但我设法以某种方式解决了它。也发布了答案。
【解决方案2】:

问题已解决: 实际上,在我的场景中,我正在调用通用 createEntity 方法。不确定,但我刚刚删除了所有复杂的注释 JPAAlertConfiguration 类中的 alertType 变量

 @OneToOne(optional = false)
 @JoinColumn(name = "alert_type_id")
 private JpaAlertType         alertType;

它奏效了。还是谢谢

【讨论】:

    猜你喜欢
    • 2014-12-27
    • 2014-03-12
    • 2017-11-13
    • 2015-02-06
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    相关资源
    最近更新 更多